MCPcopy Create free account
hub / github.com/Dstack-TEE/dstack / sortObject

Function sortObject

sdk/js/src/get-compose-hash.ts:19–35  ·  view source on GitHub ↗

* Recursively sorts object keys lexicographically. * This is crucial for deterministic JSON.stringify in JavaScript. * @param obj The object to sort. * @returns A new object with sorted keys, or the original value if not an object.

(obj: SortableValue)

Source from the content-addressed store, hash-verified

17 * @returns A new object with sorted keys, or the original value if not an object.
18 */
19function sortObject(obj: SortableValue): SortableValue {
20 if (obj === undefined || obj === null) {
21 return obj;
22 }
23 if (Array.isArray(obj)) {
24 return obj.map(sortObject);
25 } else if (obj && typeof obj === "object" && obj.constructor === Object) {
26 return Object.keys(obj)
27 .sort()
28 .reduce((result: SortableObject, key) => {
29 const value = (obj as SortableObject)[key];
30 result[key] = sortObject(value);
31 return result;
32 }, {});
33 }
34 return obj;
35}
36
37export type KeyProviderKind = "none" | "kms" | "local" | "tpm";
38

Callers 1

toDeterministicJsonFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected