Skip to content

Commit

Permalink
Merge pull request #27 from superfaceai/hotfix/incorrect-array-merging
Browse files Browse the repository at this point in the history
Hotfix/incorrect array merging
  • Loading branch information
lukas-valenta authored Jan 21, 2021
2 parents 707c269 + 8c5540d commit 90a9fe4
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 3 deletions.
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

0 comments on commit 90a9fe4

Please sign in to comment.