1 | var aesprim = require('./aesprim');
|
2 | var slice = require('./slice');
|
3 | var _evaluate = require('static-eval');
|
4 | var _uniq = require('underscore').uniq;
|
5 |
|
6 | var Handlers = function() {
|
7 | return this.initialize.apply(this, arguments);
|
8 | }
|
9 |
|
10 | Handlers.prototype.initialize = function() {
|
11 | this.traverse = traverser(true);
|
12 | this.descend = traverser();
|
13 | }
|
14 |
|
15 | Handlers.prototype.keys = Object.keys;
|
16 |
|
17 | Handlers.prototype.resolve = function(component) {
|
18 |
|
19 | var key = [ component.operation, component.scope, component.expression.type ].join('-');
|
20 | var method = this._fns[key];
|
21 |
|
22 | if (!method) throw new Error("couldn't resolve key: " + key);
|
23 | return method.bind(this);
|
24 | };
|
25 |
|
26 | Handlers.prototype.register = function(key, handler) {
|
27 |
|
28 | if (!handler instanceof Function) {
|
29 | throw new Error("handler must be a function");
|
30 | }
|
31 |
|
32 | this._fns[key] = handler;
|
33 | };
|
34 |
|
35 | Handlers.prototype._fns = {
|
36 |
|
37 | 'member-child-identifier': function(component, partial) {
|
38 | var key = component.expression.value;
|
39 | var value = partial.value;
|
40 | if (value instanceof Object && key in value) {
|
41 | return [ { value: value[key], path: partial.path.concat(key) } ]
|
42 | }
|
43 | },
|
44 |
|
45 | 'member-descendant-identifier':
|
46 | _traverse(function(key, value, ref) { return key == ref }),
|
47 |
|
48 | 'subscript-child-numeric_literal':
|
49 | _descend(function(key, value, ref) { return key === ref }),
|
50 |
|
51 | 'subscript-descendant-numeric_literal':
|
52 | _traverse(function(key, value, ref) { return key === ref }),
|
53 |
|
54 | 'member-child-wildcard':
|
55 | _descend(function() { return true }),
|
56 |
|
57 | 'member-descendant-wildcard':
|
58 | _traverse(function() { return true }),
|
59 |
|
60 | 'subscript-descendant-wildcard':
|
61 | _traverse(function() { return true }),
|
62 |
|
63 | 'subscript-child-wildcard':
|
64 | _descend(function() { return true }),
|
65 |
|
66 | 'subscript-child-slice': function(component, partial) {
|
67 | if (is_array(partial.value)) {
|
68 | var args = component.expression.value.split(':');
|
69 | var values = partial.value.map(function(v, i) { return { value: v, path: partial.path.concat(i) } });
|
70 | return slice.apply(null, [values].concat(args));
|
71 | }
|
72 | },
|
73 |
|
74 | 'subscript-child-union': function(component, partial) {
|
75 | var results = [];
|
76 | component.expression.value.forEach(function(component) {
|
77 | var _component = { operation: 'subscript', scope: 'child', expression: component.expression };
|
78 | var handler = this.resolve(_component);
|
79 | var _results = handler(_component, partial);
|
80 | results = results.concat(_results);
|
81 | }, this);
|
82 |
|
83 | return unique(results);
|
84 | },
|
85 |
|
86 | 'subscript-descendant-union': function(component, partial, count) {
|
87 |
|
88 | var jp = require('..');
|
89 | var self = this;
|
90 |
|
91 | var results = [];
|
92 | var nodes = jp.nodes(partial, '$..*').slice(1);
|
93 |
|
94 | nodes.forEach(function(node) {
|
95 | if (results.length >= count) return;
|
96 | component.expression.value.forEach(function(component) {
|
97 | var _component = { operation: 'subscript', scope: 'child', expression: component.expression };
|
98 | var handler = self.resolve(_component);
|
99 | var _results = handler(_component, node);
|
100 | results = results.concat(_results);
|
101 | });
|
102 | });
|
103 |
|
104 | return unique(results);
|
105 | },
|
106 |
|
107 | 'subscript-child-filter_expression': function(component, partial, count) {
|
108 |
|
109 |
|
110 | var src = component.expression.value.slice(2, -1);
|
111 | var ast = aesprim.parse(src).body[0].expression;
|
112 |
|
113 | var passable = function(key, value) {
|
114 | return evaluate(ast, { '@': value });
|
115 | }
|
116 |
|
117 | return this.descend(partial, null, passable, count);
|
118 |
|
119 | },
|
120 |
|
121 | 'subscript-descendant-filter_expression': function(component, partial, count) {
|
122 |
|
123 |
|
124 | var src = component.expression.value.slice(2, -1);
|
125 | var ast = aesprim.parse(src).body[0].expression;
|
126 |
|
127 | var passable = function(key, value) {
|
128 | return evaluate(ast, { '@': value });
|
129 | }
|
130 |
|
131 | return this.traverse(partial, null, passable, count);
|
132 | },
|
133 |
|
134 | 'subscript-child-script_expression': function(component, partial) {
|
135 | var exp = component.expression.value.slice(1, -1);
|
136 | return eval_recurse(partial, exp, '$[{{value}}]');
|
137 | },
|
138 |
|
139 | 'member-child-script_expression': function(component, partial) {
|
140 | var exp = component.expression.value.slice(1, -1);
|
141 | return eval_recurse(partial, exp, '$.{{value}}');
|
142 | },
|
143 |
|
144 | 'member-descendant-script_expression': function(component, partial) {
|
145 | var exp = component.expression.value.slice(1, -1);
|
146 | return eval_recurse(partial, exp, '$..value');
|
147 | }
|
148 | };
|
149 |
|
150 | Handlers.prototype._fns['subscript-child-string_literal'] =
|
151 | Handlers.prototype._fns['member-child-identifier'];
|
152 |
|
153 | Handlers.prototype._fns['member-child-numeric_literal'] =
|
154 | Handlers.prototype._fns['subscript-child-numeric_literal'];
|
155 |
|
156 | Handlers.prototype._fns['member-descendant-numeric_literal'] =
|
157 | Handlers.prototype._fns['subscript-descendant-string_literal'] =
|
158 | Handlers.prototype._fns['member-descendant-identifier'];
|
159 |
|
160 | function eval_recurse(partial, src, template) {
|
161 |
|
162 | var jp = require('./index');
|
163 | var ast = aesprim.parse(src).body[0].expression;
|
164 | var value = evaluate(ast, { '@': partial.value });
|
165 | var path = template.replace(/\{\{\s*value\s*\}\}/g, value);
|
166 |
|
167 | var results = jp.nodes(partial.value, path);
|
168 | results.forEach(function(r) {
|
169 | r.path = partial.path.concat(r.path.slice(1));
|
170 | });
|
171 |
|
172 | return results;
|
173 | }
|
174 |
|
175 | function is_array(val) {
|
176 | return Array.isArray(val);
|
177 | }
|
178 |
|
179 | function is_object(val) {
|
180 |
|
181 | return val && !(val instanceof Array) && val instanceof Object;
|
182 | }
|
183 |
|
184 | function traverser(recurse) {
|
185 |
|
186 | return function(partial, ref, passable, count) {
|
187 |
|
188 | var value = partial.value;
|
189 | var path = partial.path;
|
190 |
|
191 | var results = [];
|
192 |
|
193 | var descend = function(value, path) {
|
194 |
|
195 | if (is_array(value)) {
|
196 | value.forEach(function(element, index) {
|
197 | if (results.length >= count) { return }
|
198 | if (passable(index, element, ref)) {
|
199 | results.push({ path: path.concat(index), value: element });
|
200 | }
|
201 | });
|
202 | value.forEach(function(element, index) {
|
203 | if (results.length >= count) { return }
|
204 | if (recurse) {
|
205 | descend(element, path.concat(index));
|
206 | }
|
207 | });
|
208 | } else if (is_object(value)) {
|
209 | this.keys(value).forEach(function(k) {
|
210 | if (results.length >= count) { return }
|
211 | if (passable(k, value[k], ref)) {
|
212 | results.push({ path: path.concat(k), value: value[k] });
|
213 | }
|
214 | })
|
215 | this.keys(value).forEach(function(k) {
|
216 | if (results.length >= count) { return }
|
217 | if (recurse) {
|
218 | descend(value[k], path.concat(k));
|
219 | }
|
220 | });
|
221 | }
|
222 | }.bind(this);
|
223 | descend(value, path);
|
224 | return results;
|
225 | }
|
226 | }
|
227 |
|
228 | function _descend(passable) {
|
229 | return function(component, partial, count) {
|
230 | return this.descend(partial, component.expression.value, passable, count);
|
231 | }
|
232 | }
|
233 |
|
234 | function _traverse(passable) {
|
235 | return function(component, partial, count) {
|
236 | return this.traverse(partial, component.expression.value, passable, count);
|
237 | }
|
238 | }
|
239 |
|
240 | function evaluate() {
|
241 | try { return _evaluate.apply(this, arguments) }
|
242 | catch (e) { }
|
243 | }
|
244 |
|
245 | function unique(results) {
|
246 | return _uniq(
|
247 | results,
|
248 | function(r) { return r.path.map(function(c) { return String(c).replace('-', '--') }).join('-') }
|
249 | );
|
250 | }
|
251 |
|
252 | module.exports = Handlers;
|