-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathhandlers.js
More file actions
executable file
·266 lines (207 loc) · 7.55 KB
/
handlers.js
File metadata and controls
executable file
·266 lines (207 loc) · 7.55 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
var aesprim = require('./aesprim');
var slice = require('./slice');
var _evaluate = require('static-eval');
var _uniq = require('underscore').uniq;
var Handlers = function() {
return this.initialize.apply(this, arguments);
}
Handlers.prototype.initialize = function() {
this.traverse = traverser(true);
this.descend = traverser();
}
Handlers.prototype.keys = Object.keys;
Handlers.prototype.resolve = function(component) {
var key = [ component.operation, component.scope, component.expression.type ].join('-');
var method = this._fns[key];
if (!method) throw new Error("couldn't resolve key: " + key);
return method.bind(this);
};
Handlers.prototype.register = function(key, handler) {
if (!handler instanceof Function) {
throw new Error("handler must be a function");
}
this._fns[key] = handler;
};
Handlers.prototype._fns = {
'member-child-identifier': function(component, partial) {
var key = component.expression.value;
var value = partial.value;
if (value instanceof Object && key in value) {
return [ { value: value[key], path: partial.path.concat(key) } ]
}
},
'member-descendant-identifier':
_traverse(function(key, value, ref) { return key == ref }),
'subscript-child-numeric_literal':
_descend(function(key, value, ref) { return key === ref }),
'member-child-numeric_literal':
_descend(function(key, value, ref) { return String(key) === String(ref) }),
'subscript-descendant-numeric_literal':
_traverse(function(key, value, ref) { return key === ref }),
'member-child-wildcard':
_descend(function() { return true }),
'member-descendant-wildcard':
_traverse(function() { return true }),
'subscript-descendant-wildcard':
_traverse(function() { return true }),
'subscript-child-wildcard':
_descend(function() { return true }),
'subscript-child-slice': function(component, partial) {
if (is_array(partial.value)) {
var args = component.expression.value.split(':').map(_parse_nullable_int);
var values = partial.value.map(function(v, i) { return { value: v, path: partial.path.concat(i) } });
return slice.apply(null, [values].concat(args));
}
},
'subscript-child-union': function(component, partial) {
var results = [];
component.expression.value.forEach(function(component) {
var _component = { operation: 'subscript', scope: 'child', expression: component.expression };
var handler = this.resolve(_component);
var _results = handler(_component, partial);
if (_results) {
results = results.concat(_results);
}
}, this);
return unique(results);
},
'subscript-descendant-union': function(component, partial, count) {
var jp = require('..');
var self = this;
var results = [];
var nodes = jp.nodes(partial, '$..*').slice(1);
nodes.forEach(function(node) {
if (results.length >= count) return;
component.expression.value.forEach(function(component) {
var _component = { operation: 'subscript', scope: 'child', expression: component.expression };
var handler = self.resolve(_component);
var _results = handler(_component, node);
results = results.concat(_results);
});
});
return unique(results);
},
'subscript-child-filter_expression': function(component, partial, count) {
// slice out the expression from ?(expression)
var src = component.expression.value.slice(2, -1);
var ast = aesprim.parse(src).body[0].expression;
var passable = function(key, value) {
return evaluate(ast, { '@': value });
}
return this.descend(partial, null, passable, count);
},
'subscript-descendant-filter_expression': function(component, partial, count) {
// slice out the expression from ?(expression)
var src = component.expression.value.slice(2, -1);
var ast = aesprim.parse(src).body[0].expression;
var passable = function(key, value) {
return evaluate(ast, { '@': value });
}
return this.traverse(partial, null, passable, count);
},
'subscript-child-script_expression': function(component, partial) {
var exp = component.expression.value.slice(1, -1);
return eval_recurse(partial, exp, '$[{{value}}]');
},
'member-child-script_expression': function(component, partial) {
var exp = component.expression.value.slice(1, -1);
return eval_recurse(partial, exp, '$.{{value}}');
},
'member-descendant-script_expression': function(component, partial) {
var exp = component.expression.value.slice(1, -1);
return eval_recurse(partial, exp, '$..value');
}
};
Handlers.prototype._fns['subscript-child-string_literal'] =
Handlers.prototype._fns['member-child-identifier'];
Handlers.prototype._fns['member-descendant-numeric_literal'] =
Handlers.prototype._fns['subscript-descendant-string_literal'] =
Handlers.prototype._fns['member-descendant-identifier'];
function eval_recurse(partial, src, template) {
var jp = require('./index');
var ast = aesprim.parse(src).body[0].expression;
var value = evaluate(ast, { '@': partial.value });
var path = template.replace(/\{\{\s*value\s*\}\}/g, value);
var results = jp.nodes(partial.value, path);
results.forEach(function(r) {
r.path = partial.path.concat(r.path.slice(1));
});
return results;
}
function is_array(val) {
return Array.isArray(val);
}
function is_object(val) {
// is this a non-array, non-null object?
return val && !(val instanceof Array) && val instanceof Object;
}
function traverser(recurse) {
return function(partial, ref, passable, count) {
var value = partial.value;
var path = partial.path;
var results = [];
var descend = function(value, path, seen) {
seen = (seen || []).slice()
if (is_array(value)) {
value.forEach(function(element, index) {
if (results.length >= count) { return }
if (passable(index, element, ref)) {
results.push({ path: path.concat(index), value: element });
}
});
value.forEach(function(element, index) {
if (results.length >= count) { return }
if (recurse) {
descend(element, path.concat(index), seen);
}
});
} else if (is_object(value)) {
if (seen.indexOf(value) === -1) {
seen.push(value);
} else {
return;
}
this.keys(value).forEach(function(k) {
if (results.length >= count) { return }
if (passable(k, value[k], ref)) {
results.push({ path: path.concat(k), value: value[k] });
}
})
this.keys(value).forEach(function(k) {
if (results.length >= count) { return }
if (recurse) {
descend(value[k], path.concat(k), seen);
}
});
}
}.bind(this);
descend(value, path);
return results;
}
}
function _descend(passable) {
return function(component, partial, count) {
return this.descend(partial, component.expression.value, passable, count);
}
}
function _traverse(passable) {
return function(component, partial, count) {
return this.traverse(partial, component.expression.value, passable, count);
}
}
function evaluate() {
try { return _evaluate.apply(this, arguments) }
catch (e) { }
}
function unique(results) {
results = results.filter(function(d) { return d })
return _uniq(
results,
function(r) { return r.path.map(function(c) { return String(c).replace('-', '--') }).join('-') }
);
}
function _parse_nullable_int(val) {
var sval = String(val);
return sval.match(/^-?[0-9]+$/) ? parseInt(sval) : null;
}
module.exports = Handlers;