-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops.js
381 lines (373 loc) · 12.4 KB
/
ops.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import is from './is.js';
let _cache = {};
/**
* Ops are operations that may be performed within a CalKu expression between two values (to the left and right of the
* operation), such as logical comparisons, value comparisons, and math.
* @module
*/
const ops = {
//Alphabetical order of operations by type.
//!!! If modifying this list, please try to maintain this order for readability, this will be a long list!
//#region logic operators
AND: {
type: 'logic',
symbols: ['and', '&&'],
order: 200,
func: (a, b) => !!(a && b)
},
OR: {
type: 'logic',
symbols: ['or', '||'],
order: 205,
func: (a, b) => !!(a || b)
},
//#endregion
//#region comparison operators
CONTAINS: {
type: 'compare',
symbols: ['contains', '~~'],
order: 330,
params: [
(v) => is(v).instanceOf('array', 'string', 'number', 'boolean', null),
(v) => is(v).instanceOf('string', 'number', 'boolean', null),
],
func: (a, b) => {
if (a === null && b === null) {
return true;
} else if (a === null) {
return false;
}
if (!a?.indexOf) {
a = a?.toString() ?? '';
}
return (a.indexOf(b) > -1);
}
},
DOESNOTCONTAIN: {
type: 'compare',
symbols: ['doesnotcontain', '!~~'],
order: 330,
params: [
(v) => is(v).instanceOf('array', 'string', 'number', 'boolean', null),
(v) => is(v).instanceOf('string', 'number', 'boolean', null),
],
func: (a, b) => {
if (a === null && b === null) {
return false;
} else if (a === null) {
return true;
}
if (!a?.indexOf) {
a = a?.toString() ?? '';
}
return (a.indexOf(b) < 0);
}
},
ENDSWITH: {
type: 'compare',
symbols: ['endswith'],
order: 330,
params: [
(v) => is(v).instanceOf('array', 'string', 'number', 'boolean', null),
(v) => is(v).instanceOf('string', 'number', 'boolean', null),
],
func: (a, b) => {
if (a === null && b === null) {
return true;
} else if (a === null) {
return false;
}
if (Array.isArray(a)) {
return (!!a.length && a[a.length - 1] === b);
} else if (!a?.endsWith) {
a = a?.toString() ?? '';
}
return a.endsWith(b);
}
},
EQUALS: {
type: 'compare',
symbols: ['eq', '=='],
order: 320,
func: (a, b) => a === b
},
GREATERTHAN: {
type: 'compare',
symbols: ['gt', '>'],
order: 310,
func: (a, b) => {
if (typeof a !== typeof b) {
return false;
}
return a > b;
}
},
GREATERTHANOREQUAL: {
type: 'compare',
symbols: ['gte', '>='],
order: 315,
func: (a, b) => {
if (typeof a !== typeof b) {
return false;
}
return a >= b;
}
},
LESSTHAN: {
type: 'compare',
symbols: ['lt', '<'],
order: 300,
func: (a, b) => {
if (typeof a !== typeof b) {
return false;
}
return a < b;
}
},
LESSTHANOREQUAL: {
type: 'compare',
symbols: ['lte', '<='],
order: 305,
func: (a, b) => {
if (typeof a !== typeof b) {
return false;
}
return a <= b;
}
},
NOTEQUALS: {
type: 'compare',
symbols: ['neq', '<>', '!='],
order: 325,
func: (a, b) => a !== b
},
STARTSWITH: {
type: 'compare',
symbols: ['startswith'],
order: 330,
params: [
(v) => is(v).instanceOf('array', 'string', 'number', 'boolean', null),
(v) => is(v).instanceOf('string', 'number', 'boolean', null),
],
func: (a, b) => {
if (a === null && b === null) {
return true;
} else if (a === null) {
return false;
}
if (Array.isArray(a)) {
return (!!a.length && a[0] === b);
} else if (!a?.startsWith) {
a = a?.toString() ?? '';
}
return a.startsWith(b);
}
},
//#endregion
//#region math operators
ADDITION: {
type: 'math',
symbols: ['+'],
order: 120,
params: [
(v) => is(v).instanceOf('number', 'boolean', null),
(v) => is(v).instanceOf('number', 'boolean', null)
],
func: (a, b) => {
return a + b;
}
},
DIVISION: {
type: 'math',
symbols: ['/'],
order: 100,
params: [
(v) => is(v).instanceOf('number', 'boolean', null),
(v) => is(v).instanceOf('number', 'boolean', null)
],
func: (a, b) => a / b
},
EXPONENTIATION: {
type: 'math',
symbols: ['^'],
order: 50,
params: [
(v) => is(v).instanceOf('number', 'boolean', null),
(v) => is(v).instanceOf('number', 'boolean', null)
],
func: (a, b) => a ** b
},
MODULO: {
type: 'math',
symbols: ['%'],
order: 100,
params: [
(v) => is(v).instanceOf('number', 'boolean', null),
(v) => is(v).instanceOf('number', 'boolean', null)
],
func: (a, b) => a % b
},
MULTIPLICATION: {
type: 'math',
symbols: ['*'],
order: 100,
params: [
(v) => is(v).instanceOf('number', 'boolean', null),
(v) => is(v).instanceOf('number', 'boolean', null)
],
func: (a, b) => (a ?? 0) * (b ?? 0)
},
SUBTRACTION: {
type: 'math',
symbols: ['-'],
order: 120,
params: [
(v) => is(v).instanceOf('number', 'boolean', null),
(v) => is(v).instanceOf('number', 'boolean', null)
],
func: (a, b) => (a ?? 0) - (b ?? 0)
},
//#endregion
//#region consolidate operations
CONCATENATE: {
type: 'consolidate',
symbols: ['&'],
params: [
(v) => is(v).instanceOf('string', 'number', 'boolean', Date, null),
(v) => is(v).instanceOf('string', 'number', 'boolean', Date, null),
],
func: (a, b) => {
return (a != null ? a : '').toString() + (b != null ? b : '').toString();
}
},
//#endregion
/**
* Validates a given array of argument values for a specified func(tion). Optionally throws an error instead of
* returning a boolean result.
* @param {String | CalKuFunction} op - The key or instance of a CalKu function.
* @param {Array} args - Array of arguments to be validated.
* @param {Boolean} [throwError] - Optionally, if `true` throw an error if validation fails.
* Errors caused by an invalid func definition do not use this argument and will still be thrown.
* @returns {Boolean}
*/
argsValid(op, args, throwError) {
if (typeof op === 'string') {
op = this[op];
}
if (!op || !op.symbols) {
throw new Error('Argument "op" must be a valid operation key or CalKu operator object.');
}
//validate
if (
(typeof op.params === 'number' && op.params != args.length)
|| ((typeof op.params === 'undefined' || op.params === false) && args.length != 2)
) {
if (throwError) {
throw new Error(`Invalid number of arguments. Expected ${op?.params?.length ?? 2} but found ${args.length}.`);
}
return false;
} else if (op.params && (Array.isArray(op.params) || op.params.validator)) {
let arr = op.params;
if (op.params && typeof op.params.validator === 'function') {
//looks like params is an object literal, convert it to an array
arr = [op.params];
}
if (arr.some(v => v.spread === true)) {
throw new Error('Invalid op parameter definition: A spread parameter is not allowed on operations.');
} else if (arr.length != args.length) {
if (throwError) {
throw new Error(`Invalid number of arguments. Expected ${arr.length} but found ${args.length}.`);
}
return false;
}
//walk all arguments and validate
for (let i = 0; i < arr.length; i++) {
let param = arr[i];
let paramType = typeof param;
let validatorFunc = null;
if (paramType === 'function') {
validatorFunc = param;
} else if (paramType === 'object' && typeof param.validator === 'function') {
validatorFunc = param.validator;
} else {
throw new Error(`Invalid op parameter definition: A parameter validator used for the argument at index ${i} appears to be invalid.`);
}
if (throwError) {
let label = (i === 0 ? 'the left-side argument' : 'the right-side argument');
validatorFunc(args[i]).throw(`Operation with symbol(s) "${op.symbols.join(', ')}" failed validating ${label}.`, true);
} else if (validatorFunc(args[i]).valid() === false) {
return false;
}
}
}
return true;
},
/**
* Removes cached information about ops.
* You should call this method if you modify the ops object (e.g. if you added, changed, or removed ops).
*/
recycle() {
_cache = {};
},
/**
* Returns an array of all op keys in their declared order (if specified). If keys have orders that are the same,
* an Array is returned.
* @returns {Array.<String> | Array.<Array.<String>>}
*/
ordered() {
if (!_cache.orderedKeys) {
let list = [];
for (let p in this) { //build list of only ops defining objects
if (this[p] && this[p].symbols && this[p].type) {
let existing = list.find(v => v.order === this[p].order);
if (existing) {
if (Array.isArray(existing.key) === false) {
existing.key = [existing.key];
}
existing.key.push(p);
} else {
list.push({ key: p, order: this[p].order ?? 99999 });
}
}
}
_cache.orderedKeys = list.sort((a, b) => a.order - b.order).map(i => i.key);
}
return _cache.orderedKeys;
},
/**
* Converts all ops into a `RegExp` matching object for token parsing with the property as the key.
* The regex is looking for a valid match starting with any of the appropriate operation symbols, followed
* by whitespace, a grouping token (parenthesis), or the end of input. The first match will be the symbol matched.
* @param {...String} [types] - Optional selection of types to include in the returned map.
* @returns {Map.<String, RegExp>}
*/
toRegExp(...types) {
let r = _cache.regexp;
if (!_cache.regexp) {
//build map of all
r = new Map();
for (let o in this) {
if (this[o] && this[o].symbols && this[o].type) {
r.set(o, new RegExp(
'^(' + this[o].symbols
.map(v => v.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
.join('|') +
')(?:\\s|\\(|\\)|$)', 'i')
);
}
}
_cache.regexp = r;
}
//prep type constrained map
if (types.length > 0) {
r = new Map();
for (let [k, v] of _cache.regexp) {
if (types.indexOf(this[k].type) >= 0) {
r.set(k, v);
}
}
}
return r;
}
};
export default ops;