diff options
Diffstat (limited to 'engine/utils')
-rw-r--r-- | engine/utils/coding.ts | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/engine/utils/coding.ts b/engine/utils/coding.ts index 3f78889..283844f 100644 --- a/engine/utils/coding.ts +++ b/engine/utils/coding.ts @@ -9,6 +9,18 @@ const replacer = (_key: any, value: any) => { } }; +const sortObj = (obj: any): any => + obj === null || typeof obj !== 'object' + ? obj + : Array.isArray(obj) + ? obj.map(sortObj) + : Object.assign( + {}, + ...Object.entries(obj) + .sort(([keyA], [keyB]) => keyA.localeCompare(keyB)) + .map(([k, v]) => ({ [k]: sortObj(v) })) + ); + const reviver = (_key: any, value: any) => { if (typeof value === 'object' && value !== null) { if (value.dataType === 'Map') { @@ -18,8 +30,9 @@ const reviver = (_key: any, value: any) => { return value; }; +// "deterministic" stringify export const stringify = (obj: any) => { - return JSON.stringify(obj, replacer); + return JSON.stringify(sortObj(obj), replacer); }; export const parse = <T>(str: string) => { |