diff --git a/app/utilities/stableJson.ts b/app/utilities/stableJson.ts index 2e5d486e..a45dc5f3 100644 --- a/app/utilities/stableJson.ts +++ b/app/utilities/stableJson.ts @@ -7,6 +7,11 @@ export function stableJson(json: unknown, keyOrder: string[] = []): unknown { const keyOrder = Object.keys(json[0]); return json.map((c) => stableJson(c, keyOrder)); } + + if (Array.isArray(json)) { + return json.map((c) => stableJson(c)); + } + if (typeof json === "object" && json !== null && keyOrder.length > 0) { const keys = Object.keys(json); const sortedKeys = keys.sort((a, b) => { @@ -26,10 +31,6 @@ export function stableJson(json: unknown, keyOrder: string[] = []): unknown { return result; } - if (Array.isArray(json)) { - return json.map((c) => stableJson(c)); - } - if (typeof json === "object" && json !== null) { const result = {} as Record; for (const key of Object.keys(json)) { diff --git a/tests/stableJson.test.ts b/tests/stableJson.test.ts index 7190264e..a38bc308 100644 --- a/tests/stableJson.test.ts +++ b/tests/stableJson.test.ts @@ -183,3 +183,25 @@ test("It should order object keys in a similar order as the first object in an a }" `); }); + +test("It should not convert an array to an object in nested arrays", () => { + const json = { + data: [ + [1], + [2] + ], + }; + + expect(JSON.stringify(stableJson(json), null, 2)).toMatchInlineSnapshot(` +"{ + \\"data\\": [ + [ + 1 + ], + [ + 2 + ] + ] +}" +`); +});