-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson.js
53 lines (46 loc) · 976 Bytes
/
json.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
52
53
import { Transform } from 'readable-stream'
class JsonParse extends Transform {
constructor() {
super({
writableObjectMode: false,
readableObjectMode: true,
})
}
/**
* @param {*} chunk
* @param {string} encoding
* @param {(error?: Error | null, data?: any) => void} callback
*/
_transform(chunk, encoding, callback) {
callback(null, JSON.parse(chunk.toString()))
}
}
class JsonStringify extends Transform {
constructor() {
super({
writableObjectMode: true,
readableObjectMode: false,
})
}
/**
* @param {*} chunk
* @param {string} encoding
* @param {(error?: Error | null, data?: any) => void} callback
*/
_transform(chunk, encoding, callback) {
callback(null, JSON.stringify(chunk))
}
}
/**
* @return {Transform}
*/
function parse() {
return new JsonParse()
}
/**
* @return {Transform}
*/
function stringify() {
return new JsonStringify()
}
export { parse, stringify }