@aws-lambda-powertools/jmespath 0.0.0-alpha.0 → 2.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 +14 -0
- package/README.md +214 -5
- package/lib/cjs/Expression.d.ts +19 -0
- package/lib/cjs/Expression.d.ts.map +1 -0
- package/lib/cjs/Expression.js +38 -0
- package/lib/cjs/Functions.d.ts +262 -0
- package/lib/cjs/Functions.d.ts.map +1 -0
- package/lib/cjs/Functions.js +619 -0
- package/lib/cjs/Lexer.d.ts +19 -0
- package/lib/cjs/Lexer.d.ts.map +1 -0
- package/lib/cjs/Lexer.js +271 -0
- package/lib/cjs/ParsedResult.d.ts +15 -0
- package/lib/cjs/ParsedResult.d.ts.map +1 -0
- package/lib/cjs/ParsedResult.js +33 -0
- package/lib/cjs/Parser.d.ts +35 -0
- package/lib/cjs/Parser.d.ts.map +1 -0
- package/lib/cjs/Parser.js +506 -0
- package/lib/cjs/PowertoolsFunctions.d.ts +36 -0
- package/lib/cjs/PowertoolsFunctions.d.ts.map +1 -0
- package/lib/cjs/PowertoolsFunctions.js +69 -0
- package/lib/cjs/TreeInterpreter.d.ts +28 -0
- package/lib/cjs/TreeInterpreter.d.ts.map +1 -0
- package/lib/cjs/TreeInterpreter.js +313 -0
- package/lib/cjs/ast.d.ts +185 -0
- package/lib/cjs/ast.d.ts.map +1 -0
- package/lib/cjs/ast.js +275 -0
- package/lib/cjs/constants.d.ts +60 -0
- package/lib/cjs/constants.d.ts.map +1 -0
- package/lib/cjs/constants.js +96 -0
- package/lib/cjs/envelopes.d.ts +69 -0
- package/lib/cjs/envelopes.d.ts.map +1 -0
- package/lib/cjs/envelopes.js +90 -0
- package/lib/cjs/errors.d.ts +183 -0
- package/lib/cjs/errors.d.ts.map +1 -0
- package/lib/cjs/errors.js +206 -0
- package/lib/cjs/index.d.ts +3 -0
- package/lib/cjs/index.d.ts.map +1 -0
- package/lib/cjs/index.js +15 -0
- package/lib/cjs/package.json +1 -0
- package/lib/cjs/search.d.ts +50 -0
- package/lib/cjs/search.d.ts.map +1 -0
- package/lib/cjs/search.js +55 -0
- package/lib/cjs/types.d.ts +84 -0
- package/lib/cjs/types.d.ts.map +1 -0
- package/lib/cjs/types.js +2 -0
- package/lib/cjs/utils.d.ts +87 -0
- package/lib/cjs/utils.d.ts.map +1 -0
- package/lib/cjs/utils.js +282 -0
- package/lib/esm/Expression.d.ts +19 -0
- package/lib/esm/Expression.d.ts.map +1 -0
- package/lib/esm/Expression.js +35 -0
- package/lib/esm/Functions.d.ts +262 -0
- package/lib/esm/Functions.d.ts.map +1 -0
- package/lib/esm/Functions.js +616 -0
- package/lib/esm/Lexer.d.ts +19 -0
- package/lib/esm/Lexer.d.ts.map +1 -0
- package/lib/esm/Lexer.js +268 -0
- package/lib/esm/ParsedResult.d.ts +15 -0
- package/lib/esm/ParsedResult.d.ts.map +1 -0
- package/lib/esm/ParsedResult.js +30 -0
- package/lib/esm/Parser.d.ts +35 -0
- package/lib/esm/Parser.d.ts.map +1 -0
- package/lib/esm/Parser.js +503 -0
- package/lib/esm/PowertoolsFunctions.d.ts +36 -0
- package/lib/esm/PowertoolsFunctions.d.ts.map +1 -0
- package/lib/esm/PowertoolsFunctions.js +66 -0
- package/lib/esm/TreeInterpreter.d.ts +28 -0
- package/lib/esm/TreeInterpreter.d.ts.map +1 -0
- package/lib/esm/TreeInterpreter.js +310 -0
- package/lib/esm/ast.d.ts +185 -0
- package/lib/esm/ast.d.ts.map +1 -0
- package/lib/esm/ast.js +251 -0
- package/lib/esm/constants.d.ts +60 -0
- package/lib/esm/constants.d.ts.map +1 -0
- package/lib/esm/constants.js +88 -0
- package/lib/esm/envelopes.d.ts +69 -0
- package/lib/esm/envelopes.d.ts.map +1 -0
- package/lib/esm/envelopes.js +74 -0
- package/lib/esm/errors.d.ts +183 -0
- package/lib/esm/errors.d.ts.map +1 -0
- package/lib/esm/errors.js +195 -0
- package/lib/esm/index.d.ts +3 -0
- package/lib/esm/index.d.ts.map +1 -0
- package/lib/esm/index.js +2 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/search.d.ts +50 -0
- package/lib/esm/search.d.ts.map +1 -0
- package/lib/esm/search.js +52 -0
- package/lib/esm/types.d.ts +84 -0
- package/lib/esm/types.d.ts.map +1 -0
- package/lib/esm/types.js +1 -0
- package/lib/esm/utils.d.ts +87 -0
- package/lib/esm/utils.d.ts.map +1 -0
- package/lib/esm/utils.js +275 -0
- package/package.json +51 -12
- package/lib/index.d.ts +0 -0
- package/lib/index.js +0 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Functions.d.ts","sourceRoot":"","sources":["../../src/Functions.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,616 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { getType, isNumber, isRecord, } from '@aws-lambda-powertools/commons/typeutils';
|
|
8
|
+
import { JMESPathTypeError } from './errors.js';
|
|
9
|
+
import { arityCheck, typeCheck } from './utils.js';
|
|
10
|
+
/**
|
|
11
|
+
* A class that contains the built-in JMESPath functions.
|
|
12
|
+
*
|
|
13
|
+
* The built-in functions are implemented as methods on the Functions class.
|
|
14
|
+
* Each method is decorated with the `@Function.signature()` decorator to enforce the
|
|
15
|
+
* arity and types of the arguments passed to the function at runtime.
|
|
16
|
+
*
|
|
17
|
+
* You can extend the Functions class to add custom functions by creating a new class
|
|
18
|
+
* that extends Functions and adding new methods to it.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* import { search } from '@aws-lambda-powertools/jmespath';
|
|
23
|
+
* import { Functions } from '@aws-lambda-powertools/jmespath/functions';
|
|
24
|
+
*
|
|
25
|
+
* class MyFunctions extends Functions {
|
|
26
|
+
* @Functions.signature({
|
|
27
|
+
* argumentsSpecs: [['number'], ['number']],
|
|
28
|
+
* variadic: true,
|
|
29
|
+
* })
|
|
30
|
+
* public funcMyMethod(args: Array<number>): unknown {
|
|
31
|
+
* // ...
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
*
|
|
35
|
+
* const myFunctions = new MyFunctions();
|
|
36
|
+
*
|
|
37
|
+
* search('myMethod(@)', {}, { customFunctions: new MyFunctions() });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
class Functions {
|
|
41
|
+
constructor() {
|
|
42
|
+
/**
|
|
43
|
+
* A set of all the custom functions available in this and all child classes.
|
|
44
|
+
*/
|
|
45
|
+
this.methods = new Set();
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get the absolute value of the provided number.
|
|
49
|
+
*
|
|
50
|
+
* @param args The number to get the absolute value of
|
|
51
|
+
*/
|
|
52
|
+
funcAbs(args) {
|
|
53
|
+
return Math.abs(args);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Calculate the average of the numbers in the provided array.
|
|
57
|
+
*
|
|
58
|
+
* @param args The numbers to average
|
|
59
|
+
*/
|
|
60
|
+
funcAvg(args) {
|
|
61
|
+
return args.reduce((a, b) => a + b, 0) / args.length;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Get the ceiling of the provided number.
|
|
65
|
+
*
|
|
66
|
+
* @param args The number to get the ceiling of
|
|
67
|
+
*/
|
|
68
|
+
funcCeil(args) {
|
|
69
|
+
return Math.ceil(args);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Determine if the given value is contained in the provided array or string.
|
|
73
|
+
*
|
|
74
|
+
* @param haystack The array or string to check
|
|
75
|
+
* @param needle The value to check for
|
|
76
|
+
*/
|
|
77
|
+
funcContains(haystack, needle) {
|
|
78
|
+
return haystack.includes(needle);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Determines if the provided string ends with the provided suffix.
|
|
82
|
+
*
|
|
83
|
+
* @param str The string to check
|
|
84
|
+
* @param suffix The suffix to check for
|
|
85
|
+
*/
|
|
86
|
+
funcEndsWith(str, suffix) {
|
|
87
|
+
return str.endsWith(suffix);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Get the floor of the provided number.
|
|
91
|
+
*
|
|
92
|
+
* @param args The number to get the floor of
|
|
93
|
+
*/
|
|
94
|
+
funcFloor(args) {
|
|
95
|
+
return Math.floor(args);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Join the provided array into a single string.
|
|
99
|
+
*
|
|
100
|
+
* @param separator The separator to use
|
|
101
|
+
* @param items The array of itmes to join
|
|
102
|
+
*/
|
|
103
|
+
funcJoin(separator, items) {
|
|
104
|
+
return items.join(separator);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Get the keys of the provided object.
|
|
108
|
+
*
|
|
109
|
+
* @param arg The object to get the keys of
|
|
110
|
+
*/
|
|
111
|
+
funcKeys(arg) {
|
|
112
|
+
return Object.keys(arg);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Get the number of items in the provided item.
|
|
116
|
+
*
|
|
117
|
+
* @param arg The array to get the length of
|
|
118
|
+
*/
|
|
119
|
+
funcLength(arg) {
|
|
120
|
+
if (isRecord(arg)) {
|
|
121
|
+
return Object.keys(arg).length;
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
return arg.length;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Map the provided function over the provided array.
|
|
129
|
+
*
|
|
130
|
+
* @param expression The expression to map over the array
|
|
131
|
+
* @param args The array to map the expression over
|
|
132
|
+
*/
|
|
133
|
+
funcMap(expression, args) {
|
|
134
|
+
return args.map((arg) => {
|
|
135
|
+
return expression.visit(arg) || null;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Get the maximum value in the provided array.
|
|
140
|
+
*
|
|
141
|
+
* @param arg The array to get the maximum value of
|
|
142
|
+
*/
|
|
143
|
+
funcMax(arg) {
|
|
144
|
+
if (arg.length === 0) {
|
|
145
|
+
return null;
|
|
146
|
+
// The signature decorator already enforces that all elements are of the same type
|
|
147
|
+
}
|
|
148
|
+
else if (isNumber(arg[0])) {
|
|
149
|
+
return Math.max(...arg);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
// local compare function to handle string comparison
|
|
153
|
+
return arg.reduce((a, b) => (a > b ? a : b));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Get the item in the provided array that has the maximum value when the provided expression is evaluated.
|
|
158
|
+
*
|
|
159
|
+
* @param args The array of items to get the maximum value of
|
|
160
|
+
* @param expression The expression to evaluate for each item in the array
|
|
161
|
+
*/
|
|
162
|
+
funcMaxBy(args, expression) {
|
|
163
|
+
if (args.length === 0) {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
const visitedArgs = args.map((arg) => ({
|
|
167
|
+
arg,
|
|
168
|
+
visited: expression.visit(arg),
|
|
169
|
+
}));
|
|
170
|
+
const max = visitedArgs.reduce((max, current) => {
|
|
171
|
+
const type = getType(current.visited);
|
|
172
|
+
if (type !== 'string' && type !== 'number') {
|
|
173
|
+
throw new JMESPathTypeError({
|
|
174
|
+
currentValue: current.visited,
|
|
175
|
+
expectedTypes: ['string'],
|
|
176
|
+
actualType: type,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
if (max.visited === current.visited) {
|
|
180
|
+
return max;
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
// We can safely cast visited to number | string here because we've already
|
|
184
|
+
// checked the type at runtime above and we know that it's either a number or a string
|
|
185
|
+
return max.visited >
|
|
186
|
+
current.visited
|
|
187
|
+
? max
|
|
188
|
+
: current;
|
|
189
|
+
}
|
|
190
|
+
}, visitedArgs[0]);
|
|
191
|
+
return max.arg;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Merge the provided objects into a single object.
|
|
195
|
+
*
|
|
196
|
+
* Note that this is a shallow merge and will not merge nested objects.
|
|
197
|
+
*
|
|
198
|
+
* @param args The objects to merge
|
|
199
|
+
*/
|
|
200
|
+
funcMerge(...args) {
|
|
201
|
+
return args.reduce((a, b) => ({ ...a, ...b }), {});
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Get the minimum value in the provided array.
|
|
205
|
+
*
|
|
206
|
+
* @param arg The array to get the minimum value of
|
|
207
|
+
*/
|
|
208
|
+
funcMin(arg) {
|
|
209
|
+
if (arg.length === 0) {
|
|
210
|
+
return null;
|
|
211
|
+
// The signature decorator already enforces that all elements are of the same type
|
|
212
|
+
}
|
|
213
|
+
else if (isNumber(arg[0])) {
|
|
214
|
+
return Math.min(...arg);
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
return arg.reduce((a, b) => (a < b ? a : b));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Get the item in the provided array that has the minimum value when the provided expression is evaluated.
|
|
222
|
+
*
|
|
223
|
+
* @param args The array of items to get the minimum value of
|
|
224
|
+
* @param expression The expression to evaluate for each item in the array
|
|
225
|
+
*/
|
|
226
|
+
funcMinBy(args, expression) {
|
|
227
|
+
if (args.length === 0) {
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
const visitedArgs = args.map((arg) => ({
|
|
231
|
+
arg,
|
|
232
|
+
visited: expression.visit(arg),
|
|
233
|
+
}));
|
|
234
|
+
const min = visitedArgs.reduce((min, current) => {
|
|
235
|
+
const type = getType(current.visited);
|
|
236
|
+
if (type !== 'string' && type !== 'number') {
|
|
237
|
+
throw new JMESPathTypeError({
|
|
238
|
+
currentValue: current.visited,
|
|
239
|
+
expectedTypes: ['string'],
|
|
240
|
+
actualType: type,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
if (min.visited === current.visited) {
|
|
244
|
+
return min;
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
// We can safely cast visited to number | string here because we've already
|
|
248
|
+
// checked the type at runtime above and we know that it's either a number or a string
|
|
249
|
+
return min.visited <
|
|
250
|
+
current.visited
|
|
251
|
+
? min
|
|
252
|
+
: current;
|
|
253
|
+
}
|
|
254
|
+
}, visitedArgs[0]);
|
|
255
|
+
return min.arg;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Get the first argument that does not evaluate to null.
|
|
259
|
+
* If all arguments evaluate to null, then null is returned.
|
|
260
|
+
*
|
|
261
|
+
* @param args The keys of the items to check
|
|
262
|
+
*/
|
|
263
|
+
funcNotNull(...args) {
|
|
264
|
+
return args.find((arg) => !Object.is(arg, null)) || null;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Reverses the provided string or array.
|
|
268
|
+
*
|
|
269
|
+
* @param arg The string or array to reverse
|
|
270
|
+
*/
|
|
271
|
+
funcReverse(arg) {
|
|
272
|
+
return Array.isArray(arg)
|
|
273
|
+
? arg.reverse()
|
|
274
|
+
: arg.split('').reverse().join('');
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Sort the provided array.
|
|
278
|
+
*
|
|
279
|
+
* @param arg The array to sort
|
|
280
|
+
*/
|
|
281
|
+
funcSort(arg) {
|
|
282
|
+
return arg.sort((a, b) => {
|
|
283
|
+
if (typeof a === 'string') {
|
|
284
|
+
// We can safely cast a and b to string here because the signature decorator
|
|
285
|
+
// already enforces that all elements are of the same type
|
|
286
|
+
return a.localeCompare(b);
|
|
287
|
+
}
|
|
288
|
+
// We can safely cast a and b to number here because the signature decorator
|
|
289
|
+
// already enforces that all elements are of the same type, so if they're not strings
|
|
290
|
+
// then they must be numbers
|
|
291
|
+
return a - b;
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Sort the provided array by the provided expression.
|
|
296
|
+
*
|
|
297
|
+
* @param args The array to sort
|
|
298
|
+
* @param expression The expression to sort by
|
|
299
|
+
*/
|
|
300
|
+
funcSortBy(args, expression) {
|
|
301
|
+
return args
|
|
302
|
+
.map((value, index) => {
|
|
303
|
+
const visited = expression.visit(value);
|
|
304
|
+
const type = getType(visited);
|
|
305
|
+
if (type !== 'string' && type !== 'number') {
|
|
306
|
+
throw new JMESPathTypeError({
|
|
307
|
+
currentValue: visited,
|
|
308
|
+
expectedTypes: ['string'],
|
|
309
|
+
actualType: getType(visited),
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
return {
|
|
313
|
+
value,
|
|
314
|
+
index,
|
|
315
|
+
visited,
|
|
316
|
+
};
|
|
317
|
+
})
|
|
318
|
+
.sort((a, b) => {
|
|
319
|
+
if (a.visited === b.visited) {
|
|
320
|
+
return a.index - b.index; // Make the sort stable
|
|
321
|
+
}
|
|
322
|
+
else {
|
|
323
|
+
// We can safely cast visited to number | string here because we've already
|
|
324
|
+
// checked the type at runtime above and we know that it's either a number or a string
|
|
325
|
+
return a.visited > b.visited
|
|
326
|
+
? 1
|
|
327
|
+
: -1;
|
|
328
|
+
}
|
|
329
|
+
})
|
|
330
|
+
.map(({ value }) => value); // Extract the original values
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Determines if the provided string starts with the provided prefix.
|
|
334
|
+
*
|
|
335
|
+
* @param str The string to check
|
|
336
|
+
* @param prefix The prefix to check for
|
|
337
|
+
*/
|
|
338
|
+
funcStartsWith(str, prefix) {
|
|
339
|
+
return str.startsWith(prefix);
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Sum the provided numbers.
|
|
343
|
+
*
|
|
344
|
+
* @param args The numbers to sum
|
|
345
|
+
*/
|
|
346
|
+
funcSum(args) {
|
|
347
|
+
return args.reduce((a, b) => a + b, 0);
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Convert the provided value to an array.
|
|
351
|
+
*
|
|
352
|
+
* If the provided value is an array, then it is returned.
|
|
353
|
+
* Otherwise, the value is wrapped in an array and returned.
|
|
354
|
+
*
|
|
355
|
+
* @param arg The items to convert to an array
|
|
356
|
+
*/
|
|
357
|
+
funcToArray(arg) {
|
|
358
|
+
return Array.isArray(arg) ? arg : [arg];
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Convert the provided value to a number.
|
|
362
|
+
*
|
|
363
|
+
* If the provided value is a number, then it is returned.
|
|
364
|
+
* Otherwise, the value is converted to a number and returned.
|
|
365
|
+
*
|
|
366
|
+
* If the value cannot be converted to a number, then null is returned.
|
|
367
|
+
*
|
|
368
|
+
* @param arg The value to convert to a number
|
|
369
|
+
*/
|
|
370
|
+
funcToNumber(arg) {
|
|
371
|
+
if (typeof arg === 'number') {
|
|
372
|
+
return arg;
|
|
373
|
+
}
|
|
374
|
+
else if (typeof arg === 'string') {
|
|
375
|
+
const num = Number(arg);
|
|
376
|
+
return Number.isNaN(num) ? null : num;
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
return null;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Convert the provided value to a string.
|
|
384
|
+
*
|
|
385
|
+
* If the provided value is a string, then it is returned.
|
|
386
|
+
* Otherwise, the value is converted to a string and returned.
|
|
387
|
+
*
|
|
388
|
+
* @param arg The value to convert to a string
|
|
389
|
+
*/
|
|
390
|
+
funcToString(arg) {
|
|
391
|
+
return typeof arg === 'string' ? arg : JSON.stringify(arg);
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Get the type of the provided value.
|
|
395
|
+
*
|
|
396
|
+
* @param arg The value to check the type of
|
|
397
|
+
*/
|
|
398
|
+
funcType(arg) {
|
|
399
|
+
return getType(arg);
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Get the values of the provided object.
|
|
403
|
+
*
|
|
404
|
+
* @param arg The object to get the values of
|
|
405
|
+
*/
|
|
406
|
+
funcValues(arg) {
|
|
407
|
+
return Object.values(arg);
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Lazily introspects the methods of the class instance and all child classes
|
|
411
|
+
* to get the names of the methods that correspond to JMESPath functions.
|
|
412
|
+
*
|
|
413
|
+
* This method is used to get the names of the custom functions that are available
|
|
414
|
+
* in the class instance and all child classes. The names of the functions are used
|
|
415
|
+
* to create the custom function map that is passed to the JMESPath search function.
|
|
416
|
+
*
|
|
417
|
+
* The method traverses the inheritance chain going from the leaf class to the root class
|
|
418
|
+
* and stops when it reaches the `Functions` class, which is the root class.
|
|
419
|
+
*
|
|
420
|
+
* In doing so, it collects the names of the methods that start with `func` and adds them
|
|
421
|
+
* to the `methods` set. Finally, when the recursion collects back to the current instance,
|
|
422
|
+
* it adds the collected methods to the `this.methods` set so that they can be accessed later.
|
|
423
|
+
*
|
|
424
|
+
* @param scope The scope of the class instance to introspect
|
|
425
|
+
*/
|
|
426
|
+
introspectMethods(scope) {
|
|
427
|
+
const prototype = Object.getPrototypeOf(this);
|
|
428
|
+
const methods = new Set();
|
|
429
|
+
if (this instanceof Functions) {
|
|
430
|
+
for (const method of prototype.introspectMethods(scope)) {
|
|
431
|
+
methods.add(method);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
else {
|
|
435
|
+
return methods;
|
|
436
|
+
}
|
|
437
|
+
// This block is executed for every class in the inheritance chain
|
|
438
|
+
for (const method of Object.getOwnPropertyNames(Object.getPrototypeOf(this))) {
|
|
439
|
+
method !== 'constructor' &&
|
|
440
|
+
method.startsWith('func') &&
|
|
441
|
+
methods.add(method);
|
|
442
|
+
}
|
|
443
|
+
// This block will be executed only if the scope is the outermost class
|
|
444
|
+
if (this.methods) {
|
|
445
|
+
for (const method of methods) {
|
|
446
|
+
this.methods.add(method);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return methods;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Decorator to enforce the signature of a function at runtime.
|
|
453
|
+
*
|
|
454
|
+
* The signature decorator enforces the arity and types of the arguments
|
|
455
|
+
* passed to a function at runtime. If the arguments do not match the
|
|
456
|
+
* expected arity or types errors are thrown.
|
|
457
|
+
*
|
|
458
|
+
* @example
|
|
459
|
+
* ```typescript
|
|
460
|
+
* import { PowertoolsFunctions } from '@aws-lambda-powertools/jmespath/functions';
|
|
461
|
+
*
|
|
462
|
+
* class MyFunctions extends Functions {
|
|
463
|
+
* @Functions.signature({
|
|
464
|
+
* argumentsSpecs: [['number'], ['number']],
|
|
465
|
+
* variadic: true,
|
|
466
|
+
* })
|
|
467
|
+
* public funcMyMethod(args: Array<number>): unknown {
|
|
468
|
+
* // ...
|
|
469
|
+
* }
|
|
470
|
+
* }
|
|
471
|
+
* ```
|
|
472
|
+
*
|
|
473
|
+
* @param options The options for the signature decorator
|
|
474
|
+
*/
|
|
475
|
+
static signature(options) {
|
|
476
|
+
return (_target, _propertyKey, descriptor) => {
|
|
477
|
+
const originalMethod = descriptor.value;
|
|
478
|
+
// Use a function() {} instead of an () => {} arrow function so that we can
|
|
479
|
+
// access `myClass` as `this` in a decorated `myClass.myMethod()`.
|
|
480
|
+
descriptor.value = function (args) {
|
|
481
|
+
const { variadic, argumentsSpecs } = options;
|
|
482
|
+
arityCheck(args, argumentsSpecs, variadic);
|
|
483
|
+
typeCheck(args, argumentsSpecs);
|
|
484
|
+
return originalMethod.apply(this, args);
|
|
485
|
+
};
|
|
486
|
+
return descriptor;
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
__decorate([
|
|
491
|
+
Functions.signature({ argumentsSpecs: [['number']] })
|
|
492
|
+
], Functions.prototype, "funcAbs", null);
|
|
493
|
+
__decorate([
|
|
494
|
+
Functions.signature({
|
|
495
|
+
argumentsSpecs: [['array-number']],
|
|
496
|
+
})
|
|
497
|
+
], Functions.prototype, "funcAvg", null);
|
|
498
|
+
__decorate([
|
|
499
|
+
Functions.signature({ argumentsSpecs: [['number']] })
|
|
500
|
+
], Functions.prototype, "funcCeil", null);
|
|
501
|
+
__decorate([
|
|
502
|
+
Functions.signature({
|
|
503
|
+
argumentsSpecs: [['array', 'string'], ['any']],
|
|
504
|
+
})
|
|
505
|
+
], Functions.prototype, "funcContains", null);
|
|
506
|
+
__decorate([
|
|
507
|
+
Functions.signature({
|
|
508
|
+
argumentsSpecs: [['string'], ['string']],
|
|
509
|
+
})
|
|
510
|
+
], Functions.prototype, "funcEndsWith", null);
|
|
511
|
+
__decorate([
|
|
512
|
+
Functions.signature({ argumentsSpecs: [['number']] })
|
|
513
|
+
], Functions.prototype, "funcFloor", null);
|
|
514
|
+
__decorate([
|
|
515
|
+
Functions.signature({
|
|
516
|
+
argumentsSpecs: [['string'], ['array-string']],
|
|
517
|
+
})
|
|
518
|
+
], Functions.prototype, "funcJoin", null);
|
|
519
|
+
__decorate([
|
|
520
|
+
Functions.signature({
|
|
521
|
+
argumentsSpecs: [['object']],
|
|
522
|
+
})
|
|
523
|
+
], Functions.prototype, "funcKeys", null);
|
|
524
|
+
__decorate([
|
|
525
|
+
Functions.signature({
|
|
526
|
+
argumentsSpecs: [['array', 'string', 'object']],
|
|
527
|
+
})
|
|
528
|
+
], Functions.prototype, "funcLength", null);
|
|
529
|
+
__decorate([
|
|
530
|
+
Functions.signature({
|
|
531
|
+
argumentsSpecs: [['any'], ['array']],
|
|
532
|
+
})
|
|
533
|
+
], Functions.prototype, "funcMap", null);
|
|
534
|
+
__decorate([
|
|
535
|
+
Functions.signature({
|
|
536
|
+
argumentsSpecs: [['array-number', 'array-string']],
|
|
537
|
+
})
|
|
538
|
+
], Functions.prototype, "funcMax", null);
|
|
539
|
+
__decorate([
|
|
540
|
+
Functions.signature({
|
|
541
|
+
argumentsSpecs: [['array'], ['expression']],
|
|
542
|
+
})
|
|
543
|
+
], Functions.prototype, "funcMaxBy", null);
|
|
544
|
+
__decorate([
|
|
545
|
+
Functions.signature({
|
|
546
|
+
argumentsSpecs: [['object']],
|
|
547
|
+
variadic: true,
|
|
548
|
+
})
|
|
549
|
+
], Functions.prototype, "funcMerge", null);
|
|
550
|
+
__decorate([
|
|
551
|
+
Functions.signature({
|
|
552
|
+
argumentsSpecs: [['array-number', 'array-string']],
|
|
553
|
+
})
|
|
554
|
+
], Functions.prototype, "funcMin", null);
|
|
555
|
+
__decorate([
|
|
556
|
+
Functions.signature({
|
|
557
|
+
argumentsSpecs: [['array'], ['expression']],
|
|
558
|
+
})
|
|
559
|
+
], Functions.prototype, "funcMinBy", null);
|
|
560
|
+
__decorate([
|
|
561
|
+
Functions.signature({
|
|
562
|
+
argumentsSpecs: [[]],
|
|
563
|
+
variadic: true,
|
|
564
|
+
})
|
|
565
|
+
], Functions.prototype, "funcNotNull", null);
|
|
566
|
+
__decorate([
|
|
567
|
+
Functions.signature({
|
|
568
|
+
argumentsSpecs: [['string', 'array']],
|
|
569
|
+
})
|
|
570
|
+
], Functions.prototype, "funcReverse", null);
|
|
571
|
+
__decorate([
|
|
572
|
+
Functions.signature({
|
|
573
|
+
argumentsSpecs: [['array-number', 'array-string']],
|
|
574
|
+
})
|
|
575
|
+
], Functions.prototype, "funcSort", null);
|
|
576
|
+
__decorate([
|
|
577
|
+
Functions.signature({
|
|
578
|
+
argumentsSpecs: [['array'], ['expression']],
|
|
579
|
+
})
|
|
580
|
+
], Functions.prototype, "funcSortBy", null);
|
|
581
|
+
__decorate([
|
|
582
|
+
Functions.signature({
|
|
583
|
+
argumentsSpecs: [['string'], ['string']],
|
|
584
|
+
})
|
|
585
|
+
], Functions.prototype, "funcStartsWith", null);
|
|
586
|
+
__decorate([
|
|
587
|
+
Functions.signature({
|
|
588
|
+
argumentsSpecs: [['array-number']],
|
|
589
|
+
})
|
|
590
|
+
], Functions.prototype, "funcSum", null);
|
|
591
|
+
__decorate([
|
|
592
|
+
Functions.signature({
|
|
593
|
+
argumentsSpecs: [['any']],
|
|
594
|
+
})
|
|
595
|
+
], Functions.prototype, "funcToArray", null);
|
|
596
|
+
__decorate([
|
|
597
|
+
Functions.signature({
|
|
598
|
+
argumentsSpecs: [['any']],
|
|
599
|
+
})
|
|
600
|
+
], Functions.prototype, "funcToNumber", null);
|
|
601
|
+
__decorate([
|
|
602
|
+
Functions.signature({
|
|
603
|
+
argumentsSpecs: [['any']],
|
|
604
|
+
})
|
|
605
|
+
], Functions.prototype, "funcToString", null);
|
|
606
|
+
__decorate([
|
|
607
|
+
Functions.signature({
|
|
608
|
+
argumentsSpecs: [['any']],
|
|
609
|
+
})
|
|
610
|
+
], Functions.prototype, "funcType", null);
|
|
611
|
+
__decorate([
|
|
612
|
+
Functions.signature({
|
|
613
|
+
argumentsSpecs: [['object']],
|
|
614
|
+
})
|
|
615
|
+
], Functions.prototype, "funcValues", null);
|
|
616
|
+
export { Functions };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Token } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* A lexer for JMESPath expressions.
|
|
4
|
+
*
|
|
5
|
+
* This lexer tokenizes a JMESPath expression into a sequence of tokens.
|
|
6
|
+
*/
|
|
7
|
+
declare class Lexer {
|
|
8
|
+
#private;
|
|
9
|
+
/**
|
|
10
|
+
* Tokenize a JMESPath expression.
|
|
11
|
+
*
|
|
12
|
+
* This method is a generator that yields tokens for the given expression.
|
|
13
|
+
*
|
|
14
|
+
* @param expression The JMESPath expression to tokenize.
|
|
15
|
+
*/
|
|
16
|
+
tokenize(expression: string): Generator<Token>;
|
|
17
|
+
}
|
|
18
|
+
export { Lexer };
|
|
19
|
+
//# sourceMappingURL=Lexer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Lexer.d.ts","sourceRoot":"","sources":["../../src/Lexer.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC;;;;GAIG;AACH,cAAM,KAAK;;IAOT;;;;;;OAMG;IACK,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC,KAAK,CAAC;CAgVvD;AAED,OAAO,EAAE,KAAK,EAAE,CAAC"}
|