-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
51 lines (44 loc) · 1.23 KB
/
test.js
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
const { createParser } = require('./dist/index.js');
const parser = createParser();
describe('parser', () => {
test('parse', () => {
const parsed = parser.parse('{"typedArray": "Int8Array:AA=="}');
expect(parsed).toEqual({ typedArray: new Int8Array([0]) });
});
test('stringify', () => {
const str = parser.stringify({
typedArray: new Int8Array([0]),
});
expect(str).toEqual('{"typedArray":"Int8Array:AA=="}');
});
test('parse with custom reviver', () => {
const parsed = parser.parse(
'{"typedArray": "Int8Array:AA=="}',
(key, value) => {
if (value instanceof Int8Array) {
return value.toString();
}
return value;
}
);
expect(parsed).toEqual({ typedArray: '0' });
});
test('stringify with custom replacer', () => {
const str = parser.stringify(
{ typedArray: new Int8Array([0]) },
(key, value) => {
if (value instanceof Int8Array) {
return value.toString();
}
return value;
}
);
expect(str).toEqual('{"typedArray":"0"}');
});
test('stringify and parse 1 million Float64Array', () => {
let arr = new Float64Array(Array(1e6).fill(Math.random()));
let encoded = parser.stringify({ arr });
let arr2 = parser.parse(encoded).arr;
expect(arr2).toEqual(arr);
});
});