@jmespath-community/jmespath 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/README.md +76 -4
  2. package/dist/cli.d.ts +2 -0
  3. package/dist/cli.mjs +2181 -0
  4. package/dist/cli.mjs.map +1 -0
  5. package/dist/index.cjs +2118 -0
  6. package/dist/index.cjs.map +1 -0
  7. package/dist/index.d.cts +412 -0
  8. package/dist/index.d.ts +412 -0
  9. package/dist/index.esm.js +2099 -0
  10. package/dist/index.esm.js.map +1 -0
  11. package/dist/index.esm.min.js +2 -0
  12. package/dist/index.esm.min.js.map +1 -0
  13. package/dist/index.mjs +2089 -0
  14. package/dist/index.mjs.map +1 -0
  15. package/dist/index.umd.js +2133 -0
  16. package/dist/index.umd.js.map +1 -0
  17. package/dist/index.umd.min.js +2 -0
  18. package/dist/index.umd.min.js.map +1 -0
  19. package/package.json +28 -6
  20. package/dist/jmespath.esm.js +0 -2369
  21. package/dist/jmespath.esm.min.js +0 -2
  22. package/dist/jmespath.esm.min.js.map +0 -1
  23. package/dist/jmespath.umd.js +0 -2398
  24. package/dist/jmespath.umd.min.js +0 -2
  25. package/dist/jmespath.umd.min.js.map +0 -1
  26. package/dist/lib/bin/jp.js +0 -127
  27. package/dist/lib/bin/jp.js.map +0 -1
  28. package/dist/lib/package.json +0 -70
  29. package/dist/lib/src/AST.type.js +0 -3
  30. package/dist/lib/src/AST.type.js.map +0 -1
  31. package/dist/lib/src/JSON.type.js +0 -3
  32. package/dist/lib/src/JSON.type.js.map +0 -1
  33. package/dist/lib/src/Lexer.js +0 -327
  34. package/dist/lib/src/Lexer.js.map +0 -1
  35. package/dist/lib/src/Lexer.type.js +0 -46
  36. package/dist/lib/src/Lexer.type.js.map +0 -1
  37. package/dist/lib/src/Parser.js +0 -468
  38. package/dist/lib/src/Parser.js.map +0 -1
  39. package/dist/lib/src/Parser.type.js +0 -3
  40. package/dist/lib/src/Parser.type.js.map +0 -1
  41. package/dist/lib/src/Runtime.js +0 -924
  42. package/dist/lib/src/Runtime.js.map +0 -1
  43. package/dist/lib/src/Scope.js +0 -29
  44. package/dist/lib/src/Scope.js.map +0 -1
  45. package/dist/lib/src/TreeInterpreter.js +0 -319
  46. package/dist/lib/src/TreeInterpreter.js.map +0 -1
  47. package/dist/lib/src/index.js +0 -65
  48. package/dist/lib/src/index.js.map +0 -1
  49. package/dist/lib/src/utils/index.js +0 -145
  50. package/dist/lib/src/utils/index.js.map +0 -1
  51. package/dist/lib/src/utils/strings.js +0 -92
  52. package/dist/lib/src/utils/strings.js.map +0 -1
  53. package/dist/lib/src/utils/text.js +0 -45
  54. package/dist/lib/src/utils/text.js.map +0 -1
  55. package/dist/types/bin/jp.d.ts +0 -2
  56. package/dist/types/src/AST.type.d.ts +0 -104
  57. package/dist/types/src/JSON.type.d.ts +0 -10
  58. package/dist/types/src/Lexer.d.ts +0 -19
  59. package/dist/types/src/Lexer.type.d.ts +0 -51
  60. package/dist/types/src/Parser.d.ts +0 -33
  61. package/dist/types/src/Parser.type.d.ts +0 -4
  62. package/dist/types/src/Runtime.d.ts +0 -87
  63. package/dist/types/src/Scope.d.ts +0 -8
  64. package/dist/types/src/TreeInterpreter.d.ts +0 -22
  65. package/dist/types/src/index.d.ts +0 -45
  66. package/dist/types/src/utils/index.d.ts +0 -16
  67. package/dist/types/src/utils/strings.d.ts +0 -11
  68. package/dist/types/src/utils/text.d.ts +0 -11
@@ -1,924 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Runtime = exports.InputArgument = void 0;
4
- const strings_1 = require("./utils/strings");
5
- const text_1 = require("./utils/text");
6
- var InputArgument;
7
- (function (InputArgument) {
8
- InputArgument[InputArgument["TYPE_NUMBER"] = 0] = "TYPE_NUMBER";
9
- InputArgument[InputArgument["TYPE_ANY"] = 1] = "TYPE_ANY";
10
- InputArgument[InputArgument["TYPE_STRING"] = 2] = "TYPE_STRING";
11
- InputArgument[InputArgument["TYPE_ARRAY"] = 3] = "TYPE_ARRAY";
12
- InputArgument[InputArgument["TYPE_OBJECT"] = 4] = "TYPE_OBJECT";
13
- InputArgument[InputArgument["TYPE_BOOLEAN"] = 5] = "TYPE_BOOLEAN";
14
- InputArgument[InputArgument["TYPE_EXPREF"] = 6] = "TYPE_EXPREF";
15
- InputArgument[InputArgument["TYPE_NULL"] = 7] = "TYPE_NULL";
16
- InputArgument[InputArgument["TYPE_ARRAY_NUMBER"] = 8] = "TYPE_ARRAY_NUMBER";
17
- InputArgument[InputArgument["TYPE_ARRAY_STRING"] = 9] = "TYPE_ARRAY_STRING";
18
- InputArgument[InputArgument["TYPE_ARRAY_OBJECT"] = 10] = "TYPE_ARRAY_OBJECT";
19
- InputArgument[InputArgument["TYPE_ARRAY_ARRAY"] = 11] = "TYPE_ARRAY_ARRAY";
20
- })(InputArgument = exports.InputArgument || (exports.InputArgument = {}));
21
- class Runtime {
22
- constructor(interpreter) {
23
- this.TYPE_NAME_TABLE = {
24
- [InputArgument.TYPE_NUMBER]: 'number',
25
- [InputArgument.TYPE_ANY]: 'any',
26
- [InputArgument.TYPE_STRING]: 'string',
27
- [InputArgument.TYPE_ARRAY]: 'array',
28
- [InputArgument.TYPE_OBJECT]: 'object',
29
- [InputArgument.TYPE_BOOLEAN]: 'boolean',
30
- [InputArgument.TYPE_EXPREF]: 'expression',
31
- [InputArgument.TYPE_NULL]: 'null',
32
- [InputArgument.TYPE_ARRAY_NUMBER]: 'Array<number>',
33
- [InputArgument.TYPE_ARRAY_OBJECT]: 'Array<object>',
34
- [InputArgument.TYPE_ARRAY_STRING]: 'Array<string>',
35
- [InputArgument.TYPE_ARRAY_ARRAY]: 'Array<Array<any>>',
36
- };
37
- this.functionAbs = ([inputValue]) => {
38
- return Math.abs(inputValue);
39
- };
40
- this.functionAvg = ([inputArray]) => {
41
- if (!inputArray || inputArray.length == 0) {
42
- return null;
43
- }
44
- let sum = 0;
45
- for (let i = 0; i < inputArray.length; i += 1) {
46
- sum += inputArray[i];
47
- }
48
- return sum / inputArray.length;
49
- };
50
- this.functionCeil = ([inputValue]) => {
51
- return Math.ceil(inputValue);
52
- };
53
- this.functionContains = ([searchable, searchValue,]) => {
54
- if (Array.isArray(searchable)) {
55
- const array = searchable;
56
- return array.includes(searchValue);
57
- }
58
- if (typeof searchable === 'string') {
59
- const text = searchable;
60
- if (typeof searchValue === 'string') {
61
- return text.includes(searchValue);
62
- }
63
- }
64
- return null;
65
- };
66
- this.functionEndsWith = resolvedArgs => {
67
- const [searchStr, suffix] = resolvedArgs;
68
- return searchStr.includes(suffix, searchStr.length - suffix.length);
69
- };
70
- this.functionFindFirst = resolvedArgs => {
71
- const subject = resolvedArgs[0];
72
- const search = resolvedArgs[1];
73
- const start = (resolvedArgs.length > 2 && resolvedArgs[2]) || undefined;
74
- const end = (resolvedArgs.length > 3 && resolvedArgs[3]) || undefined;
75
- return (0, strings_1.findFirst)(subject, search, start, end);
76
- };
77
- this.functionFindLast = resolvedArgs => {
78
- const subject = resolvedArgs[0];
79
- const search = resolvedArgs[1];
80
- const start = (resolvedArgs.length > 2 && resolvedArgs[2]) || undefined;
81
- const end = (resolvedArgs.length > 3 && resolvedArgs[3]) || undefined;
82
- return (0, strings_1.findLast)(subject, search, start, end);
83
- };
84
- this.functionFloor = ([inputValue]) => {
85
- return Math.floor(inputValue);
86
- };
87
- this.functionFromItems = ([array]) => {
88
- array.map((pair) => {
89
- if (pair.length != 2 || typeof pair[0] !== 'string') {
90
- throw new Error('invalid value, each array must contain two elements, a pair of string and value');
91
- }
92
- });
93
- return Object.fromEntries(array);
94
- };
95
- this.functionGroupBy = ([array, exprefNode]) => {
96
- const keyFunction = this.createKeyFunction(exprefNode, [InputArgument.TYPE_STRING]);
97
- return array.reduce((acc, cur) => {
98
- const k = keyFunction(cur !== null && cur !== void 0 ? cur : {});
99
- const target = (acc[k] = acc[k] || []);
100
- target.push(cur);
101
- return acc;
102
- }, {});
103
- };
104
- this.functionItems = ([inputValue]) => {
105
- return Object.entries(inputValue);
106
- };
107
- this.functionJoin = resolvedArgs => {
108
- const [joinChar, listJoin] = resolvedArgs;
109
- return listJoin.join(joinChar);
110
- };
111
- this.functionKeys = ([inputObject]) => {
112
- return Object.keys(inputObject);
113
- };
114
- this.functionLength = ([inputValue]) => {
115
- if (typeof inputValue === 'string') {
116
- return new text_1.Text(inputValue).length;
117
- }
118
- if (Array.isArray(inputValue)) {
119
- return inputValue.length;
120
- }
121
- return Object.keys(inputValue).length;
122
- };
123
- this.functionLower = ([subject]) => {
124
- return (0, strings_1.lower)(subject);
125
- };
126
- this.functionMap = ([exprefNode, elements]) => {
127
- if (!this._interpreter) {
128
- return [];
129
- }
130
- const mapped = [];
131
- const interpreter = this._interpreter;
132
- for (let i = 0; i < elements.length; i += 1) {
133
- mapped.push(interpreter.visit(exprefNode, elements[i]));
134
- }
135
- return mapped;
136
- };
137
- this.functionMax = ([inputValue]) => {
138
- if (!inputValue.length) {
139
- return null;
140
- }
141
- const typeName = this.getTypeName(inputValue[0]);
142
- if (typeName === InputArgument.TYPE_NUMBER) {
143
- return Math.max(...inputValue);
144
- }
145
- const elements = inputValue;
146
- let maxElement = elements[0];
147
- for (let i = 1; i < elements.length; i += 1) {
148
- if (maxElement.localeCompare(elements[i]) < 0) {
149
- maxElement = elements[i];
150
- }
151
- }
152
- return maxElement;
153
- };
154
- this.functionMaxBy = resolvedArgs => {
155
- const exprefNode = resolvedArgs[1];
156
- const resolvedArray = resolvedArgs[0];
157
- const keyFunction = this.createKeyFunction(exprefNode, [InputArgument.TYPE_NUMBER, InputArgument.TYPE_STRING]);
158
- let maxNumber = -Infinity;
159
- let maxRecord;
160
- let current;
161
- for (let i = 0; i < resolvedArray.length; i += 1) {
162
- current = keyFunction && keyFunction(resolvedArray[i]);
163
- if (current !== undefined && current > maxNumber) {
164
- maxNumber = current;
165
- maxRecord = resolvedArray[i];
166
- }
167
- }
168
- return maxRecord || null;
169
- };
170
- this.functionMerge = resolvedArgs => {
171
- let merged = {};
172
- for (let i = 0; i < resolvedArgs.length; i += 1) {
173
- const current = resolvedArgs[i];
174
- merged = Object.assign(merged, current);
175
- }
176
- return merged;
177
- };
178
- this.functionMin = ([inputValue]) => {
179
- if (!inputValue.length) {
180
- return null;
181
- }
182
- const typeName = this.getTypeName(inputValue[0]);
183
- if (typeName === InputArgument.TYPE_NUMBER) {
184
- return Math.min(...inputValue);
185
- }
186
- const elements = inputValue;
187
- let minElement = elements[0];
188
- for (let i = 1; i < elements.length; i += 1) {
189
- if (elements[i].localeCompare(minElement) < 0) {
190
- minElement = elements[i];
191
- }
192
- }
193
- return minElement;
194
- };
195
- this.functionMinBy = resolvedArgs => {
196
- const exprefNode = resolvedArgs[1];
197
- const resolvedArray = resolvedArgs[0];
198
- const keyFunction = this.createKeyFunction(exprefNode, [InputArgument.TYPE_NUMBER, InputArgument.TYPE_STRING]);
199
- let minNumber = Infinity;
200
- let minRecord;
201
- let current;
202
- for (let i = 0; i < resolvedArray.length; i += 1) {
203
- current = keyFunction && keyFunction(resolvedArray[i]);
204
- if (current !== undefined && current < minNumber) {
205
- minNumber = current;
206
- minRecord = resolvedArray[i];
207
- }
208
- }
209
- return minRecord || null;
210
- };
211
- this.functionNotNull = resolvedArgs => {
212
- for (let i = 0; i < resolvedArgs.length; i += 1) {
213
- if (this.getTypeName(resolvedArgs[i]) !== InputArgument.TYPE_NULL) {
214
- return resolvedArgs[i];
215
- }
216
- }
217
- return null;
218
- };
219
- this.functionPadLeft = resolvedArgs => {
220
- const subject = resolvedArgs[0];
221
- const width = resolvedArgs[1];
222
- const padding = (resolvedArgs.length > 2 && resolvedArgs[2]) || undefined;
223
- return (0, strings_1.padLeft)(subject, width, padding);
224
- };
225
- this.functionPadRight = resolvedArgs => {
226
- const subject = resolvedArgs[0];
227
- const width = resolvedArgs[1];
228
- const padding = (resolvedArgs.length > 2 && resolvedArgs[2]) || undefined;
229
- return (0, strings_1.padRight)(subject, width, padding);
230
- };
231
- this.functionReplace = resolvedArgs => {
232
- const subject = resolvedArgs[0];
233
- const string = resolvedArgs[1];
234
- const by = resolvedArgs[2];
235
- return (0, strings_1.replace)(subject, string, by, resolvedArgs.length > 3 ? resolvedArgs[3] : undefined);
236
- };
237
- this.functionSplit = resolvedArgs => {
238
- const subject = resolvedArgs[0];
239
- const search = resolvedArgs[1];
240
- return (0, strings_1.split)(subject, search, resolvedArgs.length > 2 ? resolvedArgs[2] : undefined);
241
- };
242
- this.functionReverse = ([inputValue]) => {
243
- const typeName = this.getTypeName(inputValue);
244
- if (typeName === InputArgument.TYPE_STRING) {
245
- return new text_1.Text(inputValue).reverse();
246
- }
247
- const reversedArray = inputValue.slice(0);
248
- reversedArray.reverse();
249
- return reversedArray;
250
- };
251
- this.functionSort = ([inputValue]) => {
252
- if (inputValue.length == 0) {
253
- return inputValue;
254
- }
255
- if (typeof inputValue[0] === 'string') {
256
- return [...inputValue].sort(text_1.Text.comparer);
257
- }
258
- return [...inputValue].sort();
259
- };
260
- this.functionSortBy = resolvedArgs => {
261
- const sortedArray = resolvedArgs[0].slice(0);
262
- if (sortedArray.length === 0) {
263
- return sortedArray;
264
- }
265
- const interpreter = this._interpreter;
266
- const exprefNode = resolvedArgs[1];
267
- const requiredType = this.getTypeName(interpreter.visit(exprefNode, sortedArray[0]));
268
- if (requiredType !== undefined && ![InputArgument.TYPE_NUMBER, InputArgument.TYPE_STRING].includes(requiredType)) {
269
- throw new Error(`Invalid type: unexpected type (${this.TYPE_NAME_TABLE[requiredType]})`);
270
- }
271
- function throwInvalidTypeError(rt, item) {
272
- throw new Error(`Invalid type: expected (${rt.TYPE_NAME_TABLE[requiredType]}), received ${rt.TYPE_NAME_TABLE[rt.getTypeName(item)]}`);
273
- }
274
- return sortedArray.sort((a, b) => {
275
- const exprA = interpreter.visit(exprefNode, a);
276
- const exprB = interpreter.visit(exprefNode, b);
277
- if (this.getTypeName(exprA) !== requiredType) {
278
- throwInvalidTypeError(this, exprA);
279
- }
280
- else if (this.getTypeName(exprB) !== requiredType) {
281
- throwInvalidTypeError(this, exprB);
282
- }
283
- if (requiredType === InputArgument.TYPE_STRING) {
284
- return text_1.Text.comparer(exprA, exprB);
285
- }
286
- return exprA - exprB;
287
- });
288
- };
289
- this.functionStartsWith = ([searchable, searchStr]) => {
290
- return searchable.startsWith(searchStr);
291
- };
292
- this.functionSum = ([inputValue]) => {
293
- return inputValue.reduce((x, y) => x + y, 0);
294
- };
295
- this.functionToArray = ([inputValue]) => {
296
- if (this.getTypeName(inputValue) === InputArgument.TYPE_ARRAY) {
297
- return inputValue;
298
- }
299
- return [inputValue];
300
- };
301
- this.functionToNumber = ([inputValue]) => {
302
- const typeName = this.getTypeName(inputValue);
303
- let convertedValue;
304
- if (typeName === InputArgument.TYPE_NUMBER) {
305
- return inputValue;
306
- }
307
- if (typeName === InputArgument.TYPE_STRING) {
308
- convertedValue = +inputValue;
309
- if (!isNaN(convertedValue)) {
310
- return convertedValue;
311
- }
312
- }
313
- return null;
314
- };
315
- this.functionToString = ([inputValue]) => {
316
- if (this.getTypeName(inputValue) === InputArgument.TYPE_STRING) {
317
- return inputValue;
318
- }
319
- return JSON.stringify(inputValue);
320
- };
321
- this.functionTrim = resolvedArgs => {
322
- const subject = resolvedArgs[0];
323
- return (0, strings_1.trim)(subject, resolvedArgs.length > 1 ? resolvedArgs[1] : undefined);
324
- };
325
- this.functionTrimLeft = resolvedArgs => {
326
- const subject = resolvedArgs[0];
327
- return (0, strings_1.trimLeft)(subject, resolvedArgs.length > 1 ? resolvedArgs[1] : undefined);
328
- };
329
- this.functionTrimRight = resolvedArgs => {
330
- const subject = resolvedArgs[0];
331
- return (0, strings_1.trimRight)(subject, resolvedArgs.length > 1 ? resolvedArgs[1] : undefined);
332
- };
333
- this.functionType = ([inputValue]) => {
334
- switch (this.getTypeName(inputValue)) {
335
- case InputArgument.TYPE_NUMBER:
336
- return 'number';
337
- case InputArgument.TYPE_STRING:
338
- return 'string';
339
- case InputArgument.TYPE_ARRAY:
340
- return 'array';
341
- case InputArgument.TYPE_OBJECT:
342
- return 'object';
343
- case InputArgument.TYPE_BOOLEAN:
344
- return 'boolean';
345
- case InputArgument.TYPE_NULL:
346
- return 'null';
347
- default:
348
- throw new Error('invalid-type');
349
- }
350
- };
351
- this.functionUpper = ([subject]) => {
352
- return (0, strings_1.upper)(subject);
353
- };
354
- this.functionValues = ([inputObject]) => {
355
- return Object.values(inputObject);
356
- };
357
- this.functionZip = array => {
358
- const length = Math.min(...array.map(arr => arr.length));
359
- const result = Array(length)
360
- .fill(null)
361
- .map((_, index) => array.map(arr => arr[index]));
362
- return result;
363
- };
364
- this.functionTable = {
365
- abs: {
366
- _func: this.functionAbs,
367
- _signature: [
368
- {
369
- types: [InputArgument.TYPE_NUMBER],
370
- },
371
- ],
372
- },
373
- avg: {
374
- _func: this.functionAvg,
375
- _signature: [
376
- {
377
- types: [InputArgument.TYPE_ARRAY_NUMBER],
378
- },
379
- ],
380
- },
381
- ceil: {
382
- _func: this.functionCeil,
383
- _signature: [
384
- {
385
- types: [InputArgument.TYPE_NUMBER],
386
- },
387
- ],
388
- },
389
- contains: {
390
- _func: this.functionContains,
391
- _signature: [
392
- {
393
- types: [InputArgument.TYPE_STRING, InputArgument.TYPE_ARRAY],
394
- },
395
- {
396
- types: [InputArgument.TYPE_ANY],
397
- },
398
- ],
399
- },
400
- ends_with: {
401
- _func: this.functionEndsWith,
402
- _signature: [
403
- {
404
- types: [InputArgument.TYPE_STRING],
405
- },
406
- {
407
- types: [InputArgument.TYPE_STRING],
408
- },
409
- ],
410
- },
411
- find_first: {
412
- _func: this.functionFindFirst,
413
- _signature: [
414
- {
415
- types: [InputArgument.TYPE_STRING],
416
- },
417
- {
418
- types: [InputArgument.TYPE_STRING],
419
- },
420
- {
421
- types: [InputArgument.TYPE_NUMBER],
422
- optional: true,
423
- },
424
- {
425
- types: [InputArgument.TYPE_NUMBER],
426
- optional: true,
427
- },
428
- ],
429
- },
430
- find_last: {
431
- _func: this.functionFindLast,
432
- _signature: [
433
- {
434
- types: [InputArgument.TYPE_STRING],
435
- },
436
- {
437
- types: [InputArgument.TYPE_STRING],
438
- },
439
- {
440
- types: [InputArgument.TYPE_NUMBER],
441
- optional: true,
442
- },
443
- {
444
- types: [InputArgument.TYPE_NUMBER],
445
- optional: true,
446
- },
447
- ],
448
- },
449
- floor: {
450
- _func: this.functionFloor,
451
- _signature: [
452
- {
453
- types: [InputArgument.TYPE_NUMBER],
454
- },
455
- ],
456
- },
457
- from_items: {
458
- _func: this.functionFromItems,
459
- _signature: [
460
- {
461
- types: [InputArgument.TYPE_ARRAY_ARRAY],
462
- },
463
- ],
464
- },
465
- group_by: {
466
- _func: this.functionGroupBy,
467
- _signature: [{ types: [InputArgument.TYPE_ARRAY] }, { types: [InputArgument.TYPE_EXPREF] }],
468
- },
469
- items: {
470
- _func: this.functionItems,
471
- _signature: [
472
- {
473
- types: [InputArgument.TYPE_OBJECT],
474
- },
475
- ],
476
- },
477
- join: {
478
- _func: this.functionJoin,
479
- _signature: [
480
- {
481
- types: [InputArgument.TYPE_STRING],
482
- },
483
- {
484
- types: [InputArgument.TYPE_ARRAY_STRING],
485
- },
486
- ],
487
- },
488
- keys: {
489
- _func: this.functionKeys,
490
- _signature: [
491
- {
492
- types: [InputArgument.TYPE_OBJECT],
493
- },
494
- ],
495
- },
496
- length: {
497
- _func: this.functionLength,
498
- _signature: [
499
- {
500
- types: [InputArgument.TYPE_STRING, InputArgument.TYPE_ARRAY, InputArgument.TYPE_OBJECT],
501
- },
502
- ],
503
- },
504
- lower: {
505
- _func: this.functionLower,
506
- _signature: [
507
- {
508
- types: [InputArgument.TYPE_STRING],
509
- },
510
- ],
511
- },
512
- map: {
513
- _func: this.functionMap,
514
- _signature: [
515
- {
516
- types: [InputArgument.TYPE_EXPREF],
517
- },
518
- {
519
- types: [InputArgument.TYPE_ARRAY],
520
- },
521
- ],
522
- },
523
- max: {
524
- _func: this.functionMax,
525
- _signature: [
526
- {
527
- types: [InputArgument.TYPE_ARRAY_NUMBER, InputArgument.TYPE_ARRAY_STRING],
528
- },
529
- ],
530
- },
531
- max_by: {
532
- _func: this.functionMaxBy,
533
- _signature: [
534
- {
535
- types: [InputArgument.TYPE_ARRAY],
536
- },
537
- {
538
- types: [InputArgument.TYPE_EXPREF],
539
- },
540
- ],
541
- },
542
- merge: {
543
- _func: this.functionMerge,
544
- _signature: [
545
- {
546
- types: [InputArgument.TYPE_OBJECT],
547
- variadic: true,
548
- },
549
- ],
550
- },
551
- min: {
552
- _func: this.functionMin,
553
- _signature: [
554
- {
555
- types: [InputArgument.TYPE_ARRAY_NUMBER, InputArgument.TYPE_ARRAY_STRING],
556
- },
557
- ],
558
- },
559
- min_by: {
560
- _func: this.functionMinBy,
561
- _signature: [
562
- {
563
- types: [InputArgument.TYPE_ARRAY],
564
- },
565
- {
566
- types: [InputArgument.TYPE_EXPREF],
567
- },
568
- ],
569
- },
570
- not_null: {
571
- _func: this.functionNotNull,
572
- _signature: [
573
- {
574
- types: [InputArgument.TYPE_ANY],
575
- variadic: true,
576
- },
577
- ],
578
- },
579
- pad_left: {
580
- _func: this.functionPadLeft,
581
- _signature: [
582
- {
583
- types: [InputArgument.TYPE_STRING],
584
- },
585
- {
586
- types: [InputArgument.TYPE_NUMBER],
587
- },
588
- {
589
- types: [InputArgument.TYPE_STRING],
590
- optional: true,
591
- },
592
- ],
593
- },
594
- pad_right: {
595
- _func: this.functionPadRight,
596
- _signature: [
597
- {
598
- types: [InputArgument.TYPE_STRING],
599
- },
600
- {
601
- types: [InputArgument.TYPE_NUMBER],
602
- },
603
- {
604
- types: [InputArgument.TYPE_STRING],
605
- optional: true,
606
- },
607
- ],
608
- },
609
- replace: {
610
- _func: this.functionReplace,
611
- _signature: [
612
- {
613
- types: [InputArgument.TYPE_STRING],
614
- },
615
- {
616
- types: [InputArgument.TYPE_STRING],
617
- },
618
- {
619
- types: [InputArgument.TYPE_STRING],
620
- },
621
- {
622
- types: [InputArgument.TYPE_NUMBER],
623
- optional: true,
624
- },
625
- ],
626
- },
627
- split: {
628
- _func: this.functionSplit,
629
- _signature: [
630
- {
631
- types: [InputArgument.TYPE_STRING],
632
- },
633
- {
634
- types: [InputArgument.TYPE_STRING],
635
- },
636
- {
637
- types: [InputArgument.TYPE_NUMBER],
638
- optional: true,
639
- },
640
- ],
641
- },
642
- reverse: {
643
- _func: this.functionReverse,
644
- _signature: [
645
- {
646
- types: [InputArgument.TYPE_STRING, InputArgument.TYPE_ARRAY],
647
- },
648
- ],
649
- },
650
- sort: {
651
- _func: this.functionSort,
652
- _signature: [
653
- {
654
- types: [InputArgument.TYPE_ARRAY_STRING, InputArgument.TYPE_ARRAY_NUMBER],
655
- },
656
- ],
657
- },
658
- sort_by: {
659
- _func: this.functionSortBy,
660
- _signature: [
661
- {
662
- types: [InputArgument.TYPE_ARRAY],
663
- },
664
- {
665
- types: [InputArgument.TYPE_EXPREF],
666
- },
667
- ],
668
- },
669
- starts_with: {
670
- _func: this.functionStartsWith,
671
- _signature: [
672
- {
673
- types: [InputArgument.TYPE_STRING],
674
- },
675
- {
676
- types: [InputArgument.TYPE_STRING],
677
- },
678
- ],
679
- },
680
- sum: {
681
- _func: this.functionSum,
682
- _signature: [
683
- {
684
- types: [InputArgument.TYPE_ARRAY_NUMBER],
685
- },
686
- ],
687
- },
688
- to_array: {
689
- _func: this.functionToArray,
690
- _signature: [
691
- {
692
- types: [InputArgument.TYPE_ANY],
693
- },
694
- ],
695
- },
696
- to_number: {
697
- _func: this.functionToNumber,
698
- _signature: [
699
- {
700
- types: [InputArgument.TYPE_ANY],
701
- },
702
- ],
703
- },
704
- to_string: {
705
- _func: this.functionToString,
706
- _signature: [
707
- {
708
- types: [InputArgument.TYPE_ANY],
709
- },
710
- ],
711
- },
712
- trim: {
713
- _func: this.functionTrim,
714
- _signature: [
715
- {
716
- types: [InputArgument.TYPE_STRING],
717
- },
718
- {
719
- types: [InputArgument.TYPE_STRING],
720
- optional: true,
721
- },
722
- ],
723
- },
724
- trim_left: {
725
- _func: this.functionTrimLeft,
726
- _signature: [
727
- {
728
- types: [InputArgument.TYPE_STRING],
729
- },
730
- {
731
- types: [InputArgument.TYPE_STRING],
732
- optional: true,
733
- },
734
- ],
735
- },
736
- trim_right: {
737
- _func: this.functionTrimRight,
738
- _signature: [
739
- {
740
- types: [InputArgument.TYPE_STRING],
741
- },
742
- {
743
- types: [InputArgument.TYPE_STRING],
744
- optional: true,
745
- },
746
- ],
747
- },
748
- type: {
749
- _func: this.functionType,
750
- _signature: [
751
- {
752
- types: [InputArgument.TYPE_ANY],
753
- },
754
- ],
755
- },
756
- upper: {
757
- _func: this.functionUpper,
758
- _signature: [
759
- {
760
- types: [InputArgument.TYPE_STRING],
761
- },
762
- ],
763
- },
764
- values: {
765
- _func: this.functionValues,
766
- _signature: [
767
- {
768
- types: [InputArgument.TYPE_OBJECT],
769
- },
770
- ],
771
- },
772
- zip: {
773
- _func: this.functionZip,
774
- _signature: [
775
- {
776
- types: [InputArgument.TYPE_ARRAY],
777
- variadic: true,
778
- },
779
- ],
780
- },
781
- };
782
- this._interpreter = interpreter;
783
- this._functionTable = this.functionTable;
784
- }
785
- registerFunction(name, customFunction, signature) {
786
- if (name in this._functionTable) {
787
- throw new Error(`Function already defined: ${name}()`);
788
- }
789
- this._functionTable[name] = {
790
- _func: customFunction.bind(this),
791
- _signature: signature,
792
- };
793
- }
794
- callFunction(name, resolvedArgs) {
795
- const functionEntry = this._functionTable[name];
796
- if (functionEntry === undefined) {
797
- throw new Error(`Unknown function: ${name}()`);
798
- }
799
- this.validateArgs(name, resolvedArgs, functionEntry._signature);
800
- return functionEntry._func.call(this, resolvedArgs);
801
- }
802
- validateInputSignatures(name, signature) {
803
- for (let i = 0; i < signature.length; i += 1) {
804
- if ('variadic' in signature[i] && i !== signature.length - 1) {
805
- throw new Error(`Invalid arity: ${name}() 'variadic' argument ${i + 1} must occur last`);
806
- }
807
- }
808
- }
809
- validateArgs(name, args, signature) {
810
- var _a, _b;
811
- let pluralized;
812
- this.validateInputSignatures(name, signature);
813
- const numberOfRequiredArgs = signature.filter(argSignature => { var _a; return !((_a = argSignature.optional) !== null && _a !== void 0 ? _a : false); }).length;
814
- const lastArgIsVariadic = (_b = (_a = signature[signature.length - 1]) === null || _a === void 0 ? void 0 : _a.variadic) !== null && _b !== void 0 ? _b : false;
815
- const tooFewArgs = args.length < numberOfRequiredArgs;
816
- const tooManyArgs = args.length > signature.length;
817
- const tooFewModifier = tooFewArgs && ((!lastArgIsVariadic && numberOfRequiredArgs > 1) || lastArgIsVariadic) ? 'at least ' : '';
818
- if ((lastArgIsVariadic && tooFewArgs) || (!lastArgIsVariadic && (tooFewArgs || tooManyArgs))) {
819
- pluralized = signature.length > 1;
820
- throw new Error(`Invalid arity: ${name}() takes ${tooFewModifier}${numberOfRequiredArgs} argument${(pluralized && 's') || ''} but received ${args.length}`);
821
- }
822
- let currentSpec;
823
- let actualType;
824
- let typeMatched;
825
- for (let i = 0; i < signature.length; i += 1) {
826
- typeMatched = false;
827
- currentSpec = signature[i].types;
828
- actualType = this.getTypeName(args[i]);
829
- let j;
830
- for (j = 0; j < currentSpec.length; j += 1) {
831
- if (actualType !== undefined && this.typeMatches(actualType, currentSpec[j], args[i])) {
832
- typeMatched = true;
833
- break;
834
- }
835
- }
836
- if (!typeMatched && actualType !== undefined) {
837
- const expected = currentSpec
838
- .map((typeIdentifier) => {
839
- return this.TYPE_NAME_TABLE[typeIdentifier];
840
- })
841
- .join(' | ');
842
- throw new Error(`Invalid type: ${name}() expected argument ${i + 1} to be type (${expected}) but received type ${this.TYPE_NAME_TABLE[actualType]} instead.`);
843
- }
844
- }
845
- }
846
- typeMatches(actual, expected, argValue) {
847
- if (expected === InputArgument.TYPE_ANY) {
848
- return true;
849
- }
850
- if (expected === InputArgument.TYPE_ARRAY_STRING ||
851
- expected === InputArgument.TYPE_ARRAY_NUMBER ||
852
- expected === InputArgument.TYPE_ARRAY_OBJECT ||
853
- expected === InputArgument.TYPE_ARRAY_ARRAY ||
854
- expected === InputArgument.TYPE_ARRAY) {
855
- if (expected === InputArgument.TYPE_ARRAY) {
856
- return actual === InputArgument.TYPE_ARRAY;
857
- }
858
- if (actual === InputArgument.TYPE_ARRAY) {
859
- let subtype;
860
- if (expected === InputArgument.TYPE_ARRAY_NUMBER) {
861
- subtype = InputArgument.TYPE_NUMBER;
862
- }
863
- else if (expected === InputArgument.TYPE_ARRAY_OBJECT) {
864
- subtype = InputArgument.TYPE_OBJECT;
865
- }
866
- else if (expected === InputArgument.TYPE_ARRAY_STRING) {
867
- subtype = InputArgument.TYPE_STRING;
868
- }
869
- else if (expected === InputArgument.TYPE_ARRAY_ARRAY) {
870
- subtype = InputArgument.TYPE_ARRAY;
871
- }
872
- const array = argValue;
873
- for (let i = 0; i < array.length; i += 1) {
874
- const typeName = this.getTypeName(array[i]);
875
- if (typeName !== undefined && subtype !== undefined && !this.typeMatches(typeName, subtype, array[i])) {
876
- return false;
877
- }
878
- }
879
- return true;
880
- }
881
- }
882
- else {
883
- return actual === expected;
884
- }
885
- return false;
886
- }
887
- getTypeName(obj) {
888
- switch (Object.prototype.toString.call(obj)) {
889
- case '[object String]':
890
- return InputArgument.TYPE_STRING;
891
- case '[object Number]':
892
- return InputArgument.TYPE_NUMBER;
893
- case '[object Array]':
894
- return InputArgument.TYPE_ARRAY;
895
- case '[object Boolean]':
896
- return InputArgument.TYPE_BOOLEAN;
897
- case '[object Null]':
898
- return InputArgument.TYPE_NULL;
899
- case '[object Object]':
900
- if (obj.expref) {
901
- return InputArgument.TYPE_EXPREF;
902
- }
903
- return InputArgument.TYPE_OBJECT;
904
- default:
905
- return;
906
- }
907
- }
908
- createKeyFunction(exprefNode, allowedTypes) {
909
- const interpreter = this._interpreter;
910
- const keyFunc = (x) => {
911
- const current = interpreter.visit(exprefNode, x);
912
- if (!allowedTypes.includes(this.getTypeName(current))) {
913
- const msg = `Invalid type: expected one of (${allowedTypes
914
- .map(t => this.TYPE_NAME_TABLE[t])
915
- .join(' | ')}), received ${this.TYPE_NAME_TABLE[this.getTypeName(current)]}`;
916
- throw new Error(msg);
917
- }
918
- return current;
919
- };
920
- return keyFunc;
921
- }
922
- }
923
- exports.Runtime = Runtime;
924
- //# sourceMappingURL=Runtime.js.map