Skip to content

Commit ff6afbb

Browse files
committed
fix: add sorting to JSON keys to make them consistent
1 parent 90d23ec commit ff6afbb

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/components/databrowser/hooks/use-fetch-simple-key.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,26 @@ export const useFetchSimpleKey = (dataKey: string, type: DataType) => {
1919
else if (type === "json") result = (await redis.json.get(dataKey)) as string | null
2020
else throw new Error(`Invalid type when fetching simple key: ${type}`)
2121

22+
if (type === "json" && result !== null)
23+
result = JSON.stringify(sortObject(JSON.parse(result)))
24+
2225
if (result === null) deleteKeyCache(dataKey)
2326

2427
return result
2528
},
2629
})
2730
}
31+
32+
// Add recursive key sorting to a JSON object
33+
const sortObject = (obj: unknown): unknown => {
34+
if (typeof obj !== "object" || obj === null) return obj
35+
return Object.fromEntries(
36+
Object.entries(obj)
37+
.sort((a, b) => a[0].localeCompare(b[0]))
38+
.map(([key, value]) =>
39+
typeof value === "object" && !Array.isArray(value) && value !== null
40+
? [key, sortObject(value)]
41+
: [key, value]
42+
)
43+
)
44+
}

0 commit comments

Comments
 (0)