-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
190 lines (163 loc) · 3.44 KB
/
index.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
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
/**
* Expose `extract`.
*/
module.exports = extract;
/**
* Char codes.
*/
var comma = code(',');
var obrace = code('{');
var cbrace = code('}');
var obracket = code('[');
var cbracket = code(']');
var colon = code(':');
var mark = code('"');
var backslash = code('\\');
/**
* Extract the value of `keys` in the json `buf`.
*
* If `keys` is a single key, returns the value.
* If `keys` is an array of keys, returns an array of values.
*
* @param {Buffer} buf
* @param {Array|String} keys
* @return {Mixed}
* @api public
*/
function extract(buf, keys){
var multi = Array.isArray(keys);
if (!multi) keys = [keys];
var values = [];
var matched = {};
var isKey = true;
var inString = false;
var level = 0;
var chars = keys.map(strToCharCodes);
var c;
var match;
var start;
var end;
for (var i = 0; i < buf.length; i++) {
c = buf[i];
if (c == backslash) {
i++;
continue;
}
if (c == mark) {
inString = !inString;
continue;
}
if (!inString) {
if (c == colon) isKey = false;
else if (c == comma) isKey = true;
else if (c == obrace) level++;
else if (c == cbrace) level--;
}
if (!isKey || level > 1) continue;
for (var j = 0; j < keys.length; j++) {
if (!matched[keys[j]] && isMatch(buf, i, chars[j])) {
match = {
key: keys[j],
chars: chars[j],
idx: j
};
matched[keys[i]] = true;
break;
};
}
if (!match) continue;
start = i + match.key.length + 2;
end = findEnd(buf, start);
values[match.idx] = parse(buf, start, end);
match = null;
if (values.length == keys.length) break;
}
return multi
? values
: values[0];
}
/**
* Get the char code of `str`.
*
* @param {String} str
* @return {Number}
* @api private
*/
function code(str) {
return str.charCodeAt(0);
}
/**
* Convert `str` to an array of char codes.
*
* @param {String} str
* @return {Array[Number]}
* @api private
*/
function strToCharCodes(str) {
var chars = [];
for (var i = 0; i < str.length; i++) {
chars[i] = str.charCodeAt(i);
}
return chars;
}
/**
* Check if `buf[i-1] - buf[i+n]` equals `"chars"`.
*
* @param {Array[Number]} chars
* @param {Buffer} buf
* @param {Number} i
* @return {Boolean}
* @api private
*/
function isMatch(buf, i, chars){
if (buf[i - 1] != mark) return false;
for (var j = 0; j < chars.length; j++) {
if (buf[i + j] != chars[j]) return false;
}
if (buf[i + chars.length] != mark) return false;
return true;
}
/**
* Find the end index of the object
* that starts at `start` in `buf`.
*
* @param {Buffer} buf
* @param {Number} start
* @return {Number}
* @api private
*/
function findEnd(buf, start) {
var level = 0;
var s = buf[start];
var c;
for (var i = start; i < buf.length; i++) {
c = buf[i];
if (c == obrace || c == obracket) {
level++;
continue;
} else if (c == cbrace || c == cbracket) {
if (--level > 0) continue;
}
if (
level < 0
|| level == 0 && (c == comma || c == cbrace || c == cbracket)
) {
return s == obrace || s == obracket
? i + 1
: i;
}
}
}
/**
* Parse the json in `buf` from `start` to `end`.
*
* @param {Buffer} buf
* @param {Number} start
* @param {Number} end
* @return {Mixed}
* @api private
*/
function parse(buf, start, end) {
var json = buf.toString('utf8', start, end);
return JSON.parse(json);
}