diff --git a/libraries/deno-commons-mod.ts b/libraries/deno-commons-mod.ts index ffc4dd9..42905ad 100644 --- a/libraries/deno-commons-mod.ts +++ b/libraries/deno-commons-mod.ts @@ -597,6 +597,25 @@ export function getCurrentScriptDirectory(): string { return dirname(getCurrentScriptFile()); } +export function stringifySorted>( + record: T, + space?: string | number, +): string { + return JSON.stringify(record, (key, value) => { + if ( + value !== null && typeof value === "object" && !Array.isArray(value) + ) { + const sortedKeys = Object.keys(value).sort(); + const sortedObj: Record = {}; + for (const k of sortedKeys) { + sortedObj[k] = value[k]; + } + return sortedObj; + } + return value; + }, space); +} + Deno.test("isOn", () => { assertEquals(false, isOn(undefined)); assertEquals(false, isOn(""));