canonicalJson

tcmts.cli.publish.canonicalJson · #dvcqjcxb · function · runs anywhere

A value as JSON with its keys in order: two records saying the same read alike, whatever order their keys came in.

export function canonicalJson(v: unknown): string {
  return JSON.stringify(v, (_, x: unknown) =>
    x && typeof x === "object" && !Array.isArray(x)
      ? Object.fromEntries(
          Object.entries(x as Record<string, unknown>).sort(([a], [b]) => (a < b ? -1 : 1)),
        )
      : x,
  );
}

used by