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

fix: fixed issues handling empty keys + separator characters in the key #234

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
80 changes: 80 additions & 0 deletions __tests__/key.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {escapeKey, splitKey} from '../src/key';

describe('escapeKey()', () => {
it.each([
[
'key does not contain a separator or slash',
'object_property_name',
'~',
'object_property_name',
],
[
'key contains the separator character',
'object~property~name',
'~',
'object\\~property\\~name',
],
[
'key contains the separator character, using a multi-character separator',
'object$$property$$name',
'$$',
'object\\$$property\\$$name',
],
[
'key contains the escape character',
'object\\property\\name',
'~',
'object\\\\property\\\\name',
],
])(
'handles when %s',
(description: string, key: string, separator: string, expected: string) => {
expect(escapeKey(key, separator)).toEqual(expected);
}
);
});

describe('splitKey()', () => {
it.each([
[
'key does not contain any escape sequences',
'object~property~name',
'~',
['object', 'property', 'name'],
],
[
'key does not contain any escape sequences, using a multi-character separator',
'object$$property$$name',
'$$',
['object', 'property', 'name'],
],
[
'key contains an escaped separator',
'object~property\\~name',
'~',
['object', 'property~name'],
],
[
'keys contains an escaped separator, using a multi-character separator',
'object$$property\\$$name',
'$$',
['object', 'property$$name'],
],
[
'it contains an escaped slash',
'object~property\\\\name',
'~',
['object', 'property\\name'],
],
])(
'handles when %s',
(
description: string,
key: string,
separator: string,
expected: Array<string>
) => {
expect(splitKey(key, separator)).toEqual(expected);
}
);
});
128 changes: 115 additions & 13 deletions __tests__/order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,53 @@ describe('order()', () => {
const expectObject = <T extends object>(
obj: T,
map: PropertyMap | null,
res: T
) => expect(order(obj, map, '.')).toEqual(res);
res: string
) => expect(JSON.stringify(order(obj, map, '.'))).toBe(res);

it('returns nothing for a blank JSON string', () => expectObject({}, {}, {}));
it('returns nothing for a blank JSON string', () =>
expectObject({}, {}, '{}'));

it('throws error if separator is an empty string', () => {
expect(() => order({}, {}, '')).toThrowError(
'Separator should not be an empty string.'
);
});

it('throws error if separator is a slash', () => {
expect(() => order({}, {}, '\\')).toThrowError('Separator cannot be "\\".');
});

it('ignores properties not found in source', () =>
expectObject({}, {$: ['a']}, {}));
expectObject({}, {$: ['a']}, '{}'));

it('returns regular json string if map is undefined', () =>
expectObject({a: '1', b: '2'}, null, {a: '1', b: '2'}));
expectObject({a: '1', b: '2'}, null, '{"a":"1","b":"2"}'));

it('ignores properties not found in map', () =>
expectObject({a: '1', b: '2'}, {$: ['b']}, {b: '2'}));
expectObject({a: '1', b: '2'}, {$: ['b']}, '{"b":"2"}'));

it('returns first level object properties in order', () =>
expectObject({a: 2, b: 1}, {$: ['b', 'a']}, {b: 1, a: 2}));
expectObject({a: 2, b: 1}, {$: ['b', 'a']}, '{"b":1,"a":2}'));

it('returns first level array value in order', () =>
expectObject({a: ['2', 1, true]}, {$: ['a']}, {a: ['2', 1, true]}));
expectObject({a: ['2', 1, true]}, {$: ['a']}, '{"a":["2",1,true]}'));

it('returns nested [array] > [object] properties in expected order', () =>
expectObject(
{a: [1, {c: '3', d: '2'}]},
{'$': ['a'], '$.a.1': ['d', 'c']},
{a: [1, {d: '2', c: '3'}]}
'{"a":[1,{"d":"2","c":"3"}]}'
));

it('ignores nested [array] > [object] properties not found in map', () =>
expectObject(
{a: [1, {b: 2, c: 3}, 4]},
{'$': ['a'], '$.a.1': ['c']},
{a: [1, {c: 3}, 4]}
'{"a":[1,{"c":3},4]}'
));

it('ignores nested [array] > [object] properties not found in map', () =>
expectObject({a: [1, {b: 2, c: 3}, 4]}, {$: ['a']}, {a: [1, {}, 4]}));
expectObject({a: [1, {b: 2, c: 3}, 4]}, {$: ['a']}, '{"a":[1,{},4]}'));

it('handles multi-character prefix', () => {
expect(
Expand Down Expand Up @@ -127,7 +132,7 @@ describe('order()', () => {
'$.a.e': ['g', 'f'],
'$.a.b': ['d', 'c'],
},
{i: 7, a: {e: {g: 5, f: 4}, h: 6, b: {d: 4, c: 3}}}
'{"i":7,"a":{"e":{"g":5,"f":4},"h":6,"b":{"d":4,"c":3}}}'
));

it('returns nested [object] > [array] > [object] > [array] > [object] properties in expected order', () =>
Expand Down Expand Up @@ -161,6 +166,103 @@ describe('order()', () => {
'$.a.b.1.d.0': ['f', 'e'],
'$.a.b.1.d.0.f': ['h', 'g'],
},
{i: 7, a: {b: [8, {d: [{f: {h: 'h', g: true}, e: 12}, 10], c: 9}, 11]}}
'{"i":7,"a":{"b":[8,{"d":[{"f":{"h":"h","g":true},"e":12},10],"c":9},11]}}'
));

it('handles keys with no name', () => {
expectObject(
{
'': {
b: 'str',
a: 'str',
c: 'str',
},
},
{
'$': [''],
'$.': ['c', 'b', 'a'],
},
'{"":{"c":"str","b":"str","a":"str"}}'
);
});

it('handles escape sequences in the object', () => {
expectObject(
{
'.a': {
b: {t: 'str'},
c: {u: 'str'},
a: {s: 'str'},
},
'\\': {
a: {v: 'str'},
},
'\\.': {
a: {w: 'str'},
b: {x: 'str'},
},
'.': {
b: {y: 'str'},
a: {z: 'str'},
},
},
{
'$': ['.', '\\.', '\\', '.a'],
'$.\\.': ['a', 'b'],
'$.\\..a': ['z'],
'$.\\..b': ['y'],
'$.\\\\\\.': ['b', 'a'],
'$.\\\\\\..b': ['x'],
'$.\\\\\\..a': ['w'],
'$.\\\\': ['a'],
'$.\\\\.a': ['v'],
'$.\\.a': ['c', 'b', 'a'],
'$.\\.a.c': ['u'],
'$.\\.a.b': ['t'],
'$.\\.a.a': ['s'],
},
'{".":{"a":{"z":"str"},"b":{"y":"str"}},' +
'"\\\\.":{"b":{"x":"str"},"a":{"w":"str"}},' +
'"\\\\":{"a":{"v":"str"}},' +
'".a":{"c":{"u":"str"},"b":{"t":"str"},"a":{"s":"str"}}}'
);
});

it('handles escape sequences in child properties of the object', () => {
expectObject(
{
property: {
'..': {'.': 4, '..': 3},
'.': {'..': 0, '...': 2, '.': 1},
'...': {'.': 5},
},
},
{
'$': ['property'],
'$.property': ['.', '..', '...'],
'$.property.\\.': ['..', '.', '...'],
'$.property.\\.\\.': ['..', '.'],
'$.property.\\.\\.\\.': ['.'],
},
'{"property":{".":{"..":0,".":1,"...":2},"..":{"..":3,".":4},"...":{".":5}}}'
);
});

it('numeric key order defined in map is lost', () => {
// Numeric keys aren't ordered per map but instead appear first in ascending order.
// See: https://tc39.es/ecma262/#sec-ordinaryownpropertykeys
expectObject(
{
4: 'str',
a: 'str',
3: 'str',
b: 'str',
2: 'str',
},
{
$: ['a', '4', 'b', '3', '2'],
},
'{"2":"str","3":"str","4":"str","a":"str","b":"str"}'
);
});
});
86 changes: 86 additions & 0 deletions __tests__/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ describe('parse ', () => {
);
});

it('throws error if separator is a slash', () => {
expect(() => parse('', '$', '\\')).toThrowError(
'Separator cannot be "\\".'
);
});

it('handles top level values for of primitive types', () => {
const input = `
{
Expand Down Expand Up @@ -220,4 +226,84 @@ describe('parse ', () => {

expectMap(input, map);
});

it('handles keys with no name', () => {
const input = `
{
"": {
"c": "str",
"b": "str",
"a": "str"
}
}`;

const map = {
'$': [''],
'$.': ['c', 'b', 'a'],
};

expectMap(input, map);
});

it('escapes slashes as well as the separator when it exists in the object keys', () => {
// slashes in the encoded JSON are double escapes, so "\\\\" is actually equivalent to "\".
const input = `
{
".": {
"a": {"z": "str"},
"b": {"y": "str"}
},
"\\\\.": {
"b": {"x": "str"},
"a": {"w": "str"}
},
"\\\\": {
"a": {"v": "str"}
},
".a": {
"c": {"u": "str"},
"b": {"t": "str"},
"a": {"s": "str"}
}
}`;

// all below slashes are escaped so "\\" is actually equivalent to "\".
const map = {
'$': ['.', '\\.', '\\', '.a'],
'$.\\.': ['a', 'b'],
'$.\\..a': ['z'],
'$.\\..b': ['y'],
'$.\\\\\\.': ['b', 'a'],
'$.\\\\\\..b': ['x'],
'$.\\\\\\..a': ['w'],
'$.\\\\': ['a'],
'$.\\\\.a': ['v'],
'$.\\.a': ['c', 'b', 'a'],
'$.\\.a.c': ['u'],
'$.\\.a.b': ['t'],
'$.\\.a.a': ['s'],
};

expectMap(input, map);
});

it('handles keys with different types of values', () => {
const input = `
{
"a": "a",
"b": 2,
"c": 2.3,
"d": true,
"e": false,
"f": null,
"g": {},
"h": []
}`;

const map = {
$: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
};

expectMap(input, map);
});
});
18 changes: 18 additions & 0 deletions __tests__/stringify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,22 @@ describe('stringify ', () => {
},
'{"i":7,"a":{"b":[8,{"d":[{"f":{"h":"h","g":true},"e":12},10],"c":9},11]}}'
));

it('handles keys with different types of values', () =>
expectString(
{
a: 'a',
b: 2,
c: 2.3,
d: true,
e: false,
f: null,
g: {},
h: [],
},
{
$: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
},
'{"a":"a","b":2,"c":2.3,"d":true,"e":false,"f":null,"g":{},"h":[]}'
));
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"test:watch": "jest --watch",
"test:ci": "jest --ci --coverage",
"compile": "tsc -p tsconfig.build.json",
"lint": "eslint **/*.{t,j}s",
"lint": "eslint \"**/*.{t,j}s\"",
"lint:fix": "yarn lint --fix",
"semantic-release": "semantic-release"
},
Expand Down Expand Up @@ -65,6 +65,7 @@
"typescript": "4.1.3"
},
"dependencies": {
"escape-string-regexp": "^4.0.0",
"lodash.clonedeep": "^4.5.0"
}
}
Loading