/
index.ts
205 lines (175 loc) · 4.37 KB
/
index.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import through2 from 'through2';
import Parser from 'jsonparse';
/**
* Check whether a x and y are equal, or x matches y, or x(y) is truthy.
* @param {boolean | string | RegExp | (args: any[]) => boolean} x
* @param {*} y
* @returns {boolean}
*/
const check = (x, y): boolean => {
if (typeof x === 'string') {
return y === x;
}
if (x && typeof x.exec === 'function') {
return x.exec(y);
}
if (typeof x === 'boolean' || typeof x === 'object') {
return x;
}
if (typeof x === 'function') {
return x(y);
}
return false;
};
export function parse(path: string | any[], map = null) {
let header, footer;
const parser = new Parser();
const stream = through2.obj(
(chunk, enc, cb) => {
if (typeof chunk === 'string') {
chunk = Buffer.from(chunk);
}
parser.write(chunk);
cb();
},
cb => {
if (header) {
stream.emit('header', header);
}
if (footer) {
stream.emit('footer', footer);
}
if (parser.tState !== Parser.C.START || parser.stack.length > 0) {
// @ts-ignore
cb(new Error('Incomplete JSON'));
return;
}
cb();
}
);
if (typeof path === 'string') {
path = path.split('.').map(e => {
if (e === '$*') {
return { emitKey: true };
}
if (e === '*') {
return true;
}
if (e === '') {
// '..'.split('.') returns an empty string
return { recurse: true };
}
return e;
});
}
if (!path || !path.length) {
path = null;
}
parser.onValue = function(value) {
// @ts-ignore
if (!this.root) { stream.root = value; }
if (!path) return;
let i = 0; // iterates on path
let j = 0; // iterates on stack
let emitKey = false;
let emitPath = false;
while (i < path.length) {
const key = path[i];
let c;
j++;
if (key && !key.recurse) {
c = j === this.stack.length ? this : this.stack[j];
if (!c) return;
if (!check(key, c.key)) {
setHeaderFooter(c.key, value);
return;
}
emitKey = !!key.emitKey;
emitPath = !!key.emitPath;
i++;
} else {
i++;
const nextKey = path[i];
if (!nextKey) return;
// eslint-disable-next-line no-constant-condition
while (true) {
c = j === this.stack.length ? this : this.stack[j];
if (!c) return;
if (check(nextKey, c.key)) {
i++;
if (!Object.isFrozen(this.stack[j])) {
this.stack[j].value = null;
}
break;
} else {
setHeaderFooter(c.key, value);
}
j++;
}
}
}
// emit header
if (header) {
stream.emit('header', header);
header = false;
}
if (j !== this.stack.length) return;
const actualPath = this.stack.slice(1).map(element => element.key);
actualPath.push(this.key);
let data = this.value[this.key];
if (data != null) {
if ((data = map ? map(data, actualPath) : data) != null) {
if (emitKey || emitPath) {
data = {
value: data
};
if (emitKey) {
data.key = this.key;
}
if (emitPath) {
data.path = actualPath;
}
}
stream.push(data);
}
}
delete this.value[this.key];
for (const k in this.stack) {
if (!Object.isFrozen(this.stack[k])) {
this.stack[k].value = null;
}
}
};
parser._onToken = parser.onToken;
parser.onToken = function(token, value) {
parser._onToken(token, value);
if (this.stack.length === 0) {
// @ts-ignore
if (stream.root) {
// @ts-ignore
if (!path) { stream.push(stream.root); }
// @ts-ignore
stream.root = null;
}
}
};
parser.onError = function(err) {
if (err.message.includes('at position')) {
err.message = 'Invalid JSON (' + err.message + ')';
}
stream.destroy(err);
};
return stream;
function setHeaderFooter(key, value) {
// header has not been emitted yet
if (header !== false) {
header = header || {};
header[key] = value;
}
// footer has not been emitted yet but header has
if (footer !== false && header === false) {
footer = footer || {};
footer[key] = value;
}
}
}