Open History to see the last commit
1// Shared by the MultiSet consolidation oracle and its ordinary tests: the
2// tagged encoding of record contents that detects a replaced input record.
3import type { MultiSetArray } from '../src/multiset.js'
4
5export type Data = unknown
6
7// A tagged encoding of every record's contents. Reusing identities across
8// snapshots exposes replacement by a distinct reference with equal contents.
9// Symbol and function identities remain distinct; tags keep `-0`, `NaN`, and
10// `undefined` distinct.
11export function contentsOf(
12 records: MultiSetArray<Data>,
13 ids: Map<object | symbol, number>,
14): string {
15 const encode = (value: Data): Data => {
16 if (typeof value === `symbol` || typeof value === `function`) {
17 if (!ids.has(value)) ids.set(value, ids.size)
18 return [typeof value, ids.get(value)]
19 }
20 if (value === null || typeof value !== `object`)
21 return [typeof value, Object.is(value, -0) ? `-0` : String(value)]
22 if (!ids.has(value)) ids.set(value, ids.size)
23 return [
24 ids.get(value),
25 Object.entries(value).map(([key, item]) => [key, encode(item)]),
26 ]
27 }
28 return JSON.stringify(records.map(([data, m]) => [encode(data), m]))
29}