Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix/incorrect array merging #27

Merged
merged 3 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## [Unreleased]
### Fixed
* Array handling in mergeVariables function

## [0.0.6] - 2021-01-11
### Changed
* Updated AST version

## [0.0.5] - 2020-12-22
### Changed
* Enhanced logging of HTTP Errors

Expand Down
108 changes: 108 additions & 0 deletions src/internal/interpreter/variables.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import {
assertIsVariables,
isNonPrimitive,
isPrimitive,
mergeVariables,
} from './variables';

describe('Variables', () => {
test('assertIsVariables works correctly', () => {
expect(() => assertIsVariables('string')).not.toThrow();
expect(() => assertIsVariables(123)).not.toThrow();
expect(() => assertIsVariables({ x: 1 })).not.toThrow();
expect(() => assertIsVariables(true)).not.toThrow();
expect(() => assertIsVariables(undefined)).not.toThrow();
expect(() => assertIsVariables(['heeelo'])).not.toThrow();
expect(() => assertIsVariables(() => 'boom!')).toThrow();
});

test('isPrimitive works correctly', () => {
expect(isPrimitive('string')).toBe(true);
expect(isPrimitive(123)).toBe(true);
expect(isPrimitive(false)).toBe(true);
expect(isPrimitive(['heeeelo'])).toBe(true);
expect(isPrimitive({ x: 1 })).toBe(false);
});

test('isNonPrimitive works correctly', () => {
expect(isNonPrimitive('string')).toBe(false);
expect(isNonPrimitive(123)).toBe(false);
expect(isNonPrimitive(false)).toBe(false);
expect(isNonPrimitive(['heeeelo'])).toBe(false);
expect(isNonPrimitive({ x: 1 })).toBe(true);
});

describe('mergeVariables', () => {
it('should correctly merge two simple objects', () => {
{
const left = {};
const right = { x: 1 };
const result = mergeVariables(left, right);

expect(result).toEqual({ x: 1 });
}
{
const left = { y: 2 };
const right = {};
const result = mergeVariables(left, right);

expect(result).toEqual({ y: 2 });
}
{
const left = { y: 2 };
const right = { x: 1 };
const result = mergeVariables(left, right);

expect(result).toEqual({ x: 1, y: 2 });
}
});

it('should correctly merge complex objects', () => {
{
const left = {};
const right = { ne: { st: 'ed' } };
const result = mergeVariables(left, right);

expect(result).toEqual({ ne: { st: 'ed' } });
}
{
const left = { not: 'nested' };
const right = { ne: { st: 'ed' } };
const result = mergeVariables(left, right);

expect(result).toEqual({ ne: { st: 'ed' }, not: 'nested' });
}
{
const left = { ne: { est: 'eed' } };
const right = { ne: { st: 'ed' } };
const result = mergeVariables(left, right);

expect(result).toEqual({ ne: { st: 'ed', est: 'eed' } });
}
});

it('should overwrite from left to right', () => {
{
const left = { overwritten: false };
const right = { overwritten: true };
const result = mergeVariables(left, right);

expect(result).toEqual({ overwritten: true });
}
{
const left = { overwritten: { yes: false, no: false } };
const right = { overwritten: { yes: true } };
const result = mergeVariables(left, right);

expect(result).toEqual({ overwritten: { yes: true, no: false } });
}
{
const left = { overwritten: 7 };
const right = { overwritten: ['seven'] };
const result = mergeVariables(left, right);

expect(result).toEqual({ overwritten: ['seven'] });
}
});
});
});
10 changes: 7 additions & 3 deletions src/internal/interpreter/variables.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type Primitive = string | boolean | number;
// Arrays should be considered opaque value and therefore act as a primitive
export type Primitive = string | boolean | number | unknown[];
export type NonPrimitive = {
[key: string]: Primitive | NonPrimitive | undefined;
};
Expand All @@ -23,11 +24,14 @@ export function castToVariables(input: unknown): Variables | undefined {
}

export function isPrimitive(input: Variables): input is Primitive {
return ['string', 'number', 'boolean'].includes(typeof input);
return (
['string', 'number', 'boolean'].includes(typeof input) ||
Array.isArray(input)
);
}

export function isNonPrimitive(input: Variables): input is NonPrimitive {
return typeof input === 'object';
return typeof input === 'object' && !Array.isArray(input);
}

export const mergeVariables = (
Expand Down