Skip to content

Commit 976a7ab

Browse files
committed
fix: 🐛 handle the array use case first in the stableJson util
1 parent 9d63124 commit 976a7ab

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

app/utilities/stableJson.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export function stableJson(json: unknown, keyOrder: string[] = []): unknown {
77
const keyOrder = Object.keys(json[0]);
88
return json.map((c) => stableJson(c, keyOrder));
99
}
10+
11+
if (Array.isArray(json)) {
12+
return json.map((c) => stableJson(c));
13+
}
14+
1015
if (typeof json === "object" && json !== null && keyOrder.length > 0) {
1116
const keys = Object.keys(json);
1217
const sortedKeys = keys.sort((a, b) => {
@@ -26,10 +31,6 @@ export function stableJson(json: unknown, keyOrder: string[] = []): unknown {
2631
return result;
2732
}
2833

29-
if (Array.isArray(json)) {
30-
return json.map((c) => stableJson(c));
31-
}
32-
3334
if (typeof json === "object" && json !== null) {
3435
const result = {} as Record<string, unknown>;
3536
for (const key of Object.keys(json)) {

tests/stableJson.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,25 @@ test("It should order object keys in a similar order as the first object in an a
183183
}"
184184
`);
185185
});
186+
187+
test("It should not convert an array to an object in nested arrays", () => {
188+
const json = {
189+
data: [
190+
[1],
191+
[2]
192+
],
193+
};
194+
195+
expect(JSON.stringify(stableJson(json), null, 2)).toMatchInlineSnapshot(`
196+
"{
197+
\\"data\\": [
198+
[
199+
1
200+
],
201+
[
202+
2
203+
]
204+
]
205+
}"
206+
`);
207+
});

0 commit comments

Comments
 (0)