-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuffer.ts
60 lines (54 loc) · 1.33 KB
/
Buffer.ts
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
import { CustomParse, CustomStringify, parse, stringify, StringifyType } from '../src/index';
type MyType = StringifyType | 'Buffer';
const customStringify: CustomStringify<MyType> = (obj) => {
if (obj instanceof Buffer) {
return { useResult: true, result: { t: 'Buffer', v: obj.toString('base64') } };
}
return { useResult: false };
};
const customParse: CustomParse<MyType> = (obj) => {
const { t, v } = obj;
if (t === 'Buffer') {
if (v === undefined) {
throw new Error('No value');
}
return { useResult: true, result: Buffer.from(v, 'base64') };
}
return { useResult: false };
};
const obj = {
a: 'hello',
b: [
Buffer.from('123456789a', 'hex'),
Buffer.from('abcdefabcdef', 'hex'),
Buffer.from('123456789abcdef123456789abcdef', 'hex'),
],
};
console.log(obj);
/*
{
a: 'hello',
b: [
<Buffer 12 34 56 78 9a>,
<Buffer ab cd ef ab cd ef>,
<Buffer 12 34 56 78 9a bc de f1 23 45 67 89 ab cd ef>
]
}
*/
const s = stringify(obj, { customStringify });
console.log(s);
/*
{"a":{"t":"string","v":"hello"},"b":[{"t":"Buffer","v":"EjRWeJo="},{"t":"Buffer","v":"q83vq83v"},{"t":"Buffer","v":"EjRWeJq83vEjRWeJq83v"}]}
*/
const d = parse(s, { customParse });
console.log(d);
/*
{
a: 'hello',
b: [
<Buffer 12 34 56 78 9a>,
<Buffer ab cd ef ab cd ef>,
<Buffer 12 34 56 78 9a bc de f1 23 45 67 89 ab cd ef>
]
}
*/