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