-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
282 lines (235 loc) · 7.54 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
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
277
278
279
280
281
282
'use strict';
const babel = require('@babel/parser');
const expression = require('eval-estree-expression');
const { evaluate } = expression;
const isAST = value => isObject(value) && hasOwnProperty.call(value, 'type');
const isObject = value => {
return value !== null && typeof value === 'object' && !Array.isArray(value);
};
const isPrimitive = value => {
return value == null || (typeof value !== 'object' && typeof value !== 'function');
};
const LITERALS = {
'undefined': undefined,
'null': null,
'true': true,
'false': false
};
/**
* Returns true if the given value is truthy, or the `value` ("left") is
* equal to or contained within the `context` ("right") value. This method is
* used by the `whence()` function (the main export), but you can use this
* method directly if you don't want the values to be evaluated.
*
* @name equal
* @param {any} `value` The value to test.
* @param {Object} `context` The value to compare against.
* @param {[type]} `parent`
* @return {Boolean} Returns true or false.
* @api public
*/
const equal = (value, context, options = {}) => {
const eq = (a, b, parent, depth = 0) => {
if (a === b) return true;
if (a === 'undefined' || a === 'null') {
return a === b;
}
if (typeof a === 'boolean' && (!parent || parent === context)) {
return typeof b === 'boolean' ? a === b : a;
}
if ((a === 'true' || a === 'false') && (!parent || parent === context)) {
return a === 'true';
}
// only call function values at the root
if (typeof a === 'function' && depth === 0) {
return a.call(b, b, options);
}
if (typeof a === 'string' && (isObject(b) && b === context || (depth === 0 && context === undefined))) {
if (options.castBoolean === false || (b && hasOwnProperty.call(b, a))) {
return Boolean(b[a]);
}
return whence.sync(a, b, options);
}
if (isPrimitive(a) && isObject(b)) {
return Boolean(b[a]);
}
if (isPrimitive(a) && Array.isArray(b)) {
return b.includes(a);
}
if (a instanceof RegExp) {
return !(b instanceof RegExp) ? false : a.toString() === b.toString();
}
if (a instanceof Date) {
return !(b instanceof Date) ? false : a.toString() === b.toString();
}
if (a instanceof Set) {
return b instanceof Set && eq([...a], [...b], a, depth + 1);
}
if (a instanceof Map) {
return b instanceof Map && [...a].every(([k, v]) => eq(v, b.get(k), a, depth + 1));
}
if (Array.isArray(a)) {
if (isObject(b)) {
return a.every(ele => eq(ele, b, a, depth + 1));
}
if (Array.isArray(b)) {
return a.every((ele, i) => eq(ele, b[i], a, depth + 1));
}
}
if (isObject(a)) {
return isObject(b) && Object.entries(a).every(([k, v]) => eq(v, b[k], a, depth + 1));
}
return false;
};
return eq(value, context);
};
/**
* Parses the given expression string with [@babel/parser][] and returns and AST. You may also
* an [estree][]-compatible expression AST.
*
* ```js
* const { parse } = require('whence');
*
* console.log(parse('platform === "darwin"'));
* // Resuls in something like this:
* // Node {
* // type: 'BinaryExpression',
* // value: Node { type: 'Identifier', name: 'platform' },
* // operator: '===',
* // context: Node {
* // type: 'StringLiteral',
* // extra: { rawValue: 'darwin', raw: '"darwin"' },
* // value: 'darwin'
* // }
* // }
* ```
* @name parse
* @param {String} `source` Expression string or an [estree][]-compatible expression AST.
* @param {Object} `options`
* @return {Object}
* @api public
*/
const parse = (source, options = {}) => {
if (typeof source === 'string') {
return babel.parseExpression(source, options);
}
if (isObject(source) && source.type === 'Program') {
return source.body?.[0]?.expression;
}
if (isObject(source) && source.type) {
return source;
}
return null;
};
/**
* Asynchronously evaluates the given expression and returns a boolean.
*
* ```js
* const whence = require('whence');
*
* console.log(await whence('10 < 20')); //=> true
* console.log(whence.sync('10 < 20')); //=> true
* ```
* @name whence
* @param {String|Object} `source` Expression string or an [estree][]-compatible expression AST.
* @param {Object} `context`
* @param {Object} `options`
* @return {Boolean}
* @api public
*/
const whence = async (source, context, options = {}) => {
if (isAST(source)) {
return compile(source, options)(context);
}
if (typeof source !== 'string' || (isPrimitive(context) && context !== undefined)) {
return equal(source, context, options);
}
if (hasOwnProperty.call(LITERALS, source)) {
return options.castBoolean !== false ? Boolean(LITERALS[source]) : LITERALS[source];
}
const result = compile(source, options)(context);
return options.castBoolean !== false ? Boolean(await result) : result;
};
/**
* Synchronous version of [whence](#whence). Aliased as `whence.sync()`.
*
* ```js
* const { whenceSync } = require('whence');
*
* console.log(whenceSync('10 < 20')); //=> true
* ```
* @name whenceSync
* @param {String|Object} `source` Expression string or an [estree][]-compatible expression AST.
* @param {Object} `context`
* @param {Object} `options`
* @return {Boolean}
* @api public
*/
const whenceSync = (source, context, options = {}) => {
if (isAST(source)) {
return compile.sync(source, options)(context);
}
if (typeof source !== 'string' || (isPrimitive(context) && context !== undefined)) {
return equal(source, context, options);
}
if (hasOwnProperty.call(LITERALS, source)) {
return options.castBoolean !== false ? Boolean(LITERALS[source]) : LITERALS[source];
}
const result = compile.sync(source, options)(context);
return options.castBoolean !== false ? Boolean(result) : result;
};
/**
* Compiles the given expression and returns an async function.
*
* ```js
* const { compile } = require('whence');
* const fn = compile('type === "foo"');
*
* console.log(await fn({ type: 'foo' })); //=> true
* console.log(await fn({ type: 'bar' })); //=> false
* ```
* @name compile
* @param {String|Object} `source` Expression string or an [estree][]-compatible expression AST.
* @param {Object} `options`
* @return {Function} Returns a function that takes a `context` object.
* @api public
*/
const compile = (source, options) => {
const opts = { strictVariables: false, booleanLogicalOperators: true, ...options };
const ast = parse(source, opts);
return context => {
return evaluate(ast, context, opts);
};
};
/**
* Synchronous version of [compile](#compile). This method is also alias as `.compile.sync()`.
*
* ```js
* const { compile } = require('whence');
* const fn = compile.sync('type === "foo"');
*
* console.log(fn({ type: 'foo' })); //=> true
* console.log(fn({ type: 'bar' })); //=> false
* ```
* @name compileSync
* @param {String|Object} `source` Expression string or an [estree][]-compatible expression AST.
* @param {Object} `options`
* @return {Function} Returns a function that takes a `context` object.
* @api public
*/
const compileSync = (source, options) => {
const opts = { strictVariables: false, booleanLogicalOperators: true, ...options };
const ast = parse(source, opts);
return context => {
return evaluate.sync(ast, context, opts);
};
};
compile.sync = compileSync;
whence.compile = compile;
whence.compileSync = compileSync;
whence.equal = equal;
whence.evaluate = evaluate;
whence.expression = expression;
whence.parse = parse;
whence.sync = whenceSync;
module.exports = whence;