From df355978353fe6b5b35cc5042c41cfd2d5ef67ba Mon Sep 17 00:00:00 2001 From: Josh McCullough Date: Wed, 4 Oct 2023 17:07:14 -0400 Subject: [PATCH 1/2] fix: need to actually compare stringified JSON, not just the object object-only comparison does not care about order --- __tests__/order.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__tests__/order.test.ts b/__tests__/order.test.ts index 38a2425..b09e1ab 100644 --- a/__tests__/order.test.ts +++ b/__tests__/order.test.ts @@ -7,7 +7,7 @@ describe('order()', () => { obj: T, map: PropertyMap | null, res: T - ) => expect(order(obj, map, '.')).toEqual(res); + ) => expect(JSON.stringify(order(obj, map, '.'))).toEqual(JSON.stringify(res)); it('returns nothing for a blank JSON string', () => expectObject({}, {}, {})); From 5b39d16eb86d107b82f1d309ae4133a2478411ba Mon Sep 17 00:00:00 2001 From: Josh McCullough Date: Wed, 4 Oct 2023 17:07:30 -0400 Subject: [PATCH 2/2] test: add test --- __tests__/order.test.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/__tests__/order.test.ts b/__tests__/order.test.ts index b09e1ab..2db1477 100644 --- a/__tests__/order.test.ts +++ b/__tests__/order.test.ts @@ -163,4 +163,45 @@ describe('order()', () => { }, {i: 7, a: {b: [8, {d: [{f: {h: 'h', g: true}, e: 12}, 10], c: 9}, 11]}} )); + + it('sorts keys with special characters', () => { + const obj = { + 'lint-staged': { + '**/src/**/*.{js,jsx,ts,tsx}': 'eslint --fix' + }, + dependencies: { + a: '1.2.3', + z: '1.2.3', + '@types/a': '1.2.3', + '@types/z': '1.2.3', + '@types/b': '1.2.3', + }, + }; + const map = { + '$': ['dependencies', 'lint-staged'], + '$.dependencies':(() => { + const keys = Object.keys(obj.dependencies); + + // sort keys naturally + keys.sort(); + + return keys; + })(), + '$.lint-staged': Object.keys(obj['lint-staged']), + }; + const expected = { + dependencies: { + '@types/a': '1.2.3', + '@types/b': '1.2.3', + '@types/z': '1.2.3', + a: '1.2.3', + z: '1.2.3', + }, + 'lint-staged': { + '**/src/**/*.{js,jsx,ts,tsx}': 'eslint --fix' + }, + }; + + expectObject(obj, map, expected); + }); });