-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathexample-extended.js
More file actions
122 lines (100 loc) · 3.79 KB
/
Copy pathexample-extended.js
File metadata and controls
122 lines (100 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
cborg/extended provides built-in support for extended JavaScript types.
Compare this to example-bytestrings.js which manually implements TypedArray support
in ~150 lines. With cborg/extended, it's just import and use.
The type support is similar to the browser's structured clone algorithm - Date, Map,
Set, RegExp, BigInt, Error, and all TypedArrays round-trip with full type fidelity.
*/
import { encode, decode } from 'cborg/extended'
// All these types "just work" - no configuration needed
const original = {
// Date with millisecond precision
timestamp: new Date('2024-01-15T12:30:45.123Z'),
// RegExp with flags
pattern: /hello\s+world/gi,
// Map with non-string keys (impossible in JSON)
lookup: new Map([
['string-key', 'string value'],
[42, 'number key'],
[true, 'boolean key']
]),
// Set (not just array)
uniqueIds: new Set([1, 2, 3, 2, 1]), // duplicates removed
// BigInt (JSON throws on these)
bigNumber: 9007199254740993n, // beyond Number.MAX_SAFE_INTEGER
// Error types preserve their class and message
error: new TypeError('something went wrong'),
// TypedArrays preserve their exact type
floats: new Float32Array([1.5, 2.5, 3.5]),
integers: new Int16Array([-32768, 0, 32767]),
bigInts: new BigUint64Array([0n, 18446744073709551615n]),
// Negative zero is preserved (JSON and base cborg lose the sign)
negativeZero: -0
}
console.log('Original:')
console.log(original)
console.log()
// Encode to CBOR bytes
const encoded = encode(original)
console.log('Encoded size:', encoded.length, 'bytes')
console.log()
// Decode back - all types preserved
// By default, plain objects stay as plain objects (useMaps: false)
const decoded = decode(encoded)
console.log('Decoded:')
console.log(decoded)
console.log()
// Verify types are preserved
console.log('Type checks:')
console.log(' timestamp is Date:', decoded.timestamp instanceof Date)
console.log(' pattern is RegExp:', decoded.pattern instanceof RegExp)
console.log(' lookup is Map:', decoded.lookup instanceof Map)
console.log(' uniqueIds is Set:', decoded.uniqueIds instanceof Set)
console.log(' bigNumber is bigint:', typeof decoded.bigNumber === 'bigint')
console.log(' error is TypeError:', decoded.error instanceof TypeError)
console.log(' floats is Float32Array:', decoded.floats instanceof Float32Array)
console.log(' integers is Int16Array:', decoded.integers instanceof Int16Array)
console.log(' bigInts is BigUint64Array:', decoded.bigInts instanceof BigUint64Array)
console.log(' negativeZero is -0:', Object.is(decoded.negativeZero, -0))
console.log()
// Maps with non-string keys work correctly
console.log('Map key types preserved:')
console.log(' lookup.get(42):', decoded.lookup.get(42))
console.log(' lookup.get(true):', decoded.lookup.get(true))
/* Expected output:
Original:
{
timestamp: 2024-01-15T12:30:45.123Z,
pattern: /hello\s+world/gi,
lookup: Map(3) { 'string-key' => 'string value', 42 => 'number key', true => 'boolean key' },
uniqueIds: Set(3) { 1, 2, 3 },
bigNumber: 9007199254740993n,
error: TypeError: something went wrong,
floats: Float32Array(3) [ 1.5, 2.5, 3.5 ],
integers: Int16Array(3) [ -32768, 0, 32767 ],
bigInts: BigUint64Array(2) [ 0n, 18446744073709551615n ],
negativeZero: -0
}
Encoded size: ~230 bytes
Decoded:
{
timestamp: 2024-01-15T12:30:45.123Z,
pattern: /hello\s+world/gi,
lookup: Map(3) { 'string-key' => 'string value', 42 => 'number key', true => 'boolean key' },
...
}
Type checks:
timestamp is Date: true
pattern is RegExp: true
lookup is Map: true
uniqueIds is Set: true
bigNumber is bigint: true
error is TypeError: true
floats is Float32Array: true
integers is Int16Array: true
bigInts is BigUint64Array: true
negativeZero is -0: true
Map key types preserved:
lookup.get(42): number key
lookup.get(true): boolean key
*/