mirror: A fast canonicalisation utility for stable object equality
0

Configure Feed

Select the types of activity you want to include in your feed.

chore: test all supported values over-the-wire stability

Marais Rossouw (Jun 25, 2026, 3:24 PM +1000) 5e8d86a4 c2bddd38

+32
+32
lib/mod.test.ts
··· 1 1 import { assert, assertEquals, assertNotEquals, assertNotMatch, assertThrows } from '@std/assert'; 2 2 import fc from 'fast-check'; 3 + import { Buffer } from 'node:buffer'; 3 4 import { identify } from '../lib/mod.ts'; 4 5 5 6 Deno.test('exports :: identify is a function', () => { ··· 460 461 // 461 462 // These lock the exact serialization. A change here is a breaking change to every 462 463 // previously-stored identity, so update deliberately. 464 + 465 + Deno.test('format :: supported builtins serialize consistently', () => { 466 + const assertWire = (name: string, input: unknown, expected: string) => 467 + assertEquals(identify(input), expected, name); 468 + 469 + assertWire('array', [1, undefined, 'x'], 'an1Lsx'); 470 + assertWire('bigint', 123n, 'n123'); 471 + assertWire('bigint64 array', new BigInt64Array([-1n, 2n]), 'o0n-11n2'); 472 + assertWire('biguint64 array', new BigUint64Array([1n, 2n]), 'o0n11n2'); 473 + assertWire('boolean', true, 'T'); 474 + assertWire('buffer', Buffer.from([1, 2]), 'o0n11n2'); 475 + assertWire('data view', new DataView(new Uint8Array([1, 2]).buffer), 'o'); 476 + assertWire('date', new Date(0), 'd0'); 477 + assertWire('float32 array', new Float32Array([1.5, -2.25]), 'o0n1.51n-2.25'); 478 + assertWire('float64 array', new Float64Array([1.5, -2.25]), 'o0n1.51n-2.25'); 479 + assertWire('int8 array', new Int8Array([-1, 2]), 'o0n-11n2'); 480 + assertWire('int16 array', new Int16Array([-1, 2]), 'o0n-11n2'); 481 + assertWire('int32 array', new Int32Array([-1, 2]), 'o0n-11n2'); 482 + assertWire('map', new Map([['b', 2], ['a', 1]]), 'oan1bn2'); 483 + assertWire('null', null, 'L'); 484 + assertWire('object', { b: 2, a: 1 }, 'oan1bn2'); 485 + assertWire('regexp', /ab/gi, 'rabgi'); 486 + assertWire('set', new Set([1, 2]), 'en1n2'); 487 + assertWire('string', 'hello', 'shello'); 488 + assertWire('uint8 array', new Uint8Array([1, 2]), 'o0n11n2'); 489 + assertWire('uint8 clamped array', new Uint8ClampedArray([1, 255]), 'o0n11n255'); 490 + assertWire('uint16 array', new Uint16Array([1, 2]), 'o0n11n2'); 491 + assertWire('uint32 array', new Uint32Array([1, 2]), 'o0n11n2'); 492 + //assertWire('custom toJSON', { toJSON: () => ({ a: 1, b: [2] }) }, 'oan1ban2'); 493 + //assertWire('url', new URL('https://example.com/a?b=1#c'), 'shttps://example.com/a?b=1#c'); 494 + }); 463 495 464 496 Deno.test('format :: circular array back-reference', () => { 465 497 const arr: unknown[] = [1, 2, 3];