-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·276 lines (195 loc) · 6.94 KB
/
index.js
File metadata and controls
executable file
·276 lines (195 loc) · 6.94 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
var assert = require('assert');
var dict = require('./dict');
var Parser = require('./parser');
var Handlers = require('./handlers');
var JSONPath = function() {
this.initialize.apply(this, arguments);
};
JSONPath.prototype.initialize = function() {
this.parser = new Parser();
this.handlers = new Handlers();
};
JSONPath.prototype.parse = function(string) {
assert.ok(_is_string(string), "we need a path");
return this.parser.parse(string);
};
JSONPath.prototype.parent = function(obj, string) {
assert.ok(obj instanceof Object, "obj needs to be an object");
assert.ok(string, "we need a path");
var node = this.nodes(obj, string)[0];
var key = node.path.pop(); /* jshint unused:false */
return this.value(obj, node.path);
}
JSONPath.prototype.filter = function(obj, string, fn) {
assert.ok(obj instanceof Object, "obj needs to be an object");
assert.ok(string, "we need a path");
assert.equal(typeof fn, "function", "fn needs to be function")
var nodes = this.nodes(obj, string).sort(function(a, b) {
// sort nodes so we apply from the bottom up
return b.path.length - a.path.length;
});
nodes.forEach(function(node) {
var key = node.path.pop();
var parent = this.value(obj, this.stringify(node.path));
var val = node.value = fn.call(obj, parent[key], {parent: parent, key: key});
if (!val) return;
if (Array.isArray(parent)) {
parent.splice(key, 1);
return;
}
delete parent[key];
}, this);
return nodes;
}
JSONPath.prototype.apply = function(obj, string, fn) {
assert.ok(obj instanceof Object, "obj needs to be an object");
assert.ok(string, "we need a path");
assert.equal(typeof fn, "function", "fn needs to be function")
var nodes = this.nodes(obj, string).sort(function(a, b) {
// sort nodes so we apply from the bottom up
return b.path.length - a.path.length;
});
nodes.forEach(function(node) {
var key = node.path.pop();
var parent = this.value(obj, this.stringify(node.path));
var val = node.value = fn.call(obj, parent[key], {parent: parent, key: key});
parent[key] = val;
}, this);
return nodes;
}
JSONPath.prototype.value = function(obj, path, value) {
assert.ok(obj instanceof Object, "obj needs to be an object");
assert.ok(path, "we need a path");
if (arguments.length >= 3) {
var node = this.nodes(obj, path).shift();
if (!node) return this._vivify(obj, path, value);
var key = node.path.slice(-1).shift();
var parent = this.parent(obj, this.stringify(node.path));
parent[key] = value;
}
return this.query(obj, this.stringify(path), 1).shift();
}
JSONPath.prototype._vivify = function(obj, string, value) {
var self = this;
assert.ok(obj instanceof Object, "obj needs to be an object");
assert.ok(string, "we need a path");
var path = this.parser.parse(string)
.map(function(component) { return component.expression.value });
var setValue = function(path, value) {
var key = path.pop();
var node = self.value(obj, path);
if (!node) {
setValue(path.concat(), typeof key === 'string' ? {} : []);
node = self.value(obj, path);
}
node[key] = value;
}
setValue(path, value);
return this.query(obj, string)[0];
}
JSONPath.prototype.query = function(obj, string, count) {
assert.ok(obj instanceof Object, "obj needs to be an object");
assert.ok(_is_string(string), "we need a path");
var results = this.nodes(obj, string, count)
.map(function(r) { return r.value });
return results;
};
JSONPath.prototype.paths = function(obj, string, count) {
assert.ok(obj instanceof Object, "obj needs to be an object");
assert.ok(string, "we need a path");
var results = this.nodes(obj, string, count)
.map(function(r) { return r.path });
return results;
};
JSONPath.prototype.nodes = function(obj, string, count) {
assert.ok(obj instanceof Object, "obj needs to be an object");
assert.ok(string, "we need a path");
if (count === 0) return [];
var path = this.parser.parse(string);
var handlers = this.handlers;
var partials = [ { path: ['$'], value: obj } ];
var matches = [];
if (path.length && path[0].expression.type == 'root') path.shift();
if (!path.length) return partials;
path.forEach(function(component, index) {
if (matches.length >= count) return;
var handler = handlers.resolve(component);
var _partials = [];
partials.forEach(function(p) {
if (matches.length >= count) return;
var results = handler(component, p, count);
if (index == path.length - 1) {
// if we're through the components we're done
matches = matches.concat(results || []);
} else {
// otherwise accumulate and carry on through
_partials = _partials.concat(results || []);
}
});
partials = _partials;
});
return count ? matches.slice(0, count) : matches;
};
JSONPath.prototype.stringify = function(path) {
assert.ok(path, "we need a path");
var string = '$';
var templates = {
'descendant-member': '..{{value}}',
'child-member': '.{{value}}',
'descendant-subscript': '..[{{value}}]',
'child-subscript': '[{{value}}]'
};
path = this._normalize(path);
path.forEach(function(component) {
if (component.expression.type == 'root') return;
var key = [component.scope, component.operation].join('-');
var template = templates[key];
var value;
if (component.expression.type == 'string_literal') {
value = JSON.stringify(component.expression.value)
} else {
value = component.expression.value;
}
if (!template) throw new Error("couldn't find template " + key);
string += template.replace(/{{value}}/, value);
});
return string;
}
JSONPath.prototype._normalize = function(path) {
assert.ok(path, "we need a path");
if (typeof path == "string") {
return this.parser.parse(path);
} else if (Array.isArray(path) && typeof path[0] == "string") {
var _path = [ { expression: { type: "root", value: "$" } } ];
path.forEach(function(component, index) {
if (component == '$' && index === 0) return;
if (typeof component == "string" && component.match("^" + dict.identifier + "$")) {
_path.push({
operation: 'member',
scope: 'child',
expression: { value: component, type: 'identifier' }
});
} else {
var type = typeof component == "number" ?
'numeric_literal' : 'string_literal';
_path.push({
operation: 'subscript',
scope: 'child',
expression: { value: component, type: type }
});
}
});
return _path;
} else if (Array.isArray(path) && typeof path[0] == "object") {
return path
}
throw new Error("couldn't understand path " + path);
}
function _is_string(obj) {
return Object.prototype.toString.call(obj) == '[object String]';
}
JSONPath.Handlers = Handlers;
JSONPath.Parser = Parser;
var instance = new JSONPath;
instance.JSONPath = JSONPath;
module.exports = instance;