All files / web/src/lib stable-stringify.ts

100% Statements 17/17
100% Branches 9/9
100% Functions 1/1
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 182x 2x 2x 2x 2x 2x 2x 2x 40251x 40251x 40251x 1765x 1765x 1765x 1765x 1765x 1765x  
/**
 * Deterministic, key-order-independent serialization — two objects that differ
 * only in property insertion order serialize identically. Shared by the print
 * idempotency signature (React re-renders that rebuild state objects must not
 * spuriously rotate the key) and the abacus design-snapshot content hash
 * (identical designs submitted twice must converge on one stored row).
 */
export function stableStringify(value: unknown): string {
  if (value === undefined) return 'undefined'
  if (value === null || typeof value !== 'object') return JSON.stringify(value)
  if (Array.isArray(value)) return `[${value.map(stableStringify).join(',')}]`
  const obj = value as Record<string, unknown>
  return `{${Object.keys(obj)
    .sort()
    .map((k) => `${JSON.stringify(k)}:${stableStringify(obj[k])}`)
    .join(',')}}`
}