@aws-lambda-powertools/jmespath 0.0.0-alpha.0 → 2.0.4

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 (97) hide show
  1. package/LICENSE +14 -0
  2. package/README.md +214 -5
  3. package/lib/cjs/Expression.d.ts +19 -0
  4. package/lib/cjs/Expression.d.ts.map +1 -0
  5. package/lib/cjs/Expression.js +38 -0
  6. package/lib/cjs/Functions.d.ts +242 -0
  7. package/lib/cjs/Functions.d.ts.map +1 -0
  8. package/lib/cjs/Functions.js +597 -0
  9. package/lib/cjs/Lexer.d.ts +19 -0
  10. package/lib/cjs/Lexer.d.ts.map +1 -0
  11. package/lib/cjs/Lexer.js +271 -0
  12. package/lib/cjs/ParsedResult.d.ts +15 -0
  13. package/lib/cjs/ParsedResult.d.ts.map +1 -0
  14. package/lib/cjs/ParsedResult.js +33 -0
  15. package/lib/cjs/Parser.d.ts +35 -0
  16. package/lib/cjs/Parser.d.ts.map +1 -0
  17. package/lib/cjs/Parser.js +506 -0
  18. package/lib/cjs/PowertoolsFunctions.d.ts +36 -0
  19. package/lib/cjs/PowertoolsFunctions.d.ts.map +1 -0
  20. package/lib/cjs/PowertoolsFunctions.js +69 -0
  21. package/lib/cjs/TreeInterpreter.d.ts +28 -0
  22. package/lib/cjs/TreeInterpreter.d.ts.map +1 -0
  23. package/lib/cjs/TreeInterpreter.js +313 -0
  24. package/lib/cjs/ast.d.ts +185 -0
  25. package/lib/cjs/ast.d.ts.map +1 -0
  26. package/lib/cjs/ast.js +275 -0
  27. package/lib/cjs/constants.d.ts +60 -0
  28. package/lib/cjs/constants.d.ts.map +1 -0
  29. package/lib/cjs/constants.js +96 -0
  30. package/lib/cjs/envelopes.d.ts +69 -0
  31. package/lib/cjs/envelopes.d.ts.map +1 -0
  32. package/lib/cjs/envelopes.js +90 -0
  33. package/lib/cjs/errors.d.ts +183 -0
  34. package/lib/cjs/errors.d.ts.map +1 -0
  35. package/lib/cjs/errors.js +206 -0
  36. package/lib/cjs/index.d.ts +3 -0
  37. package/lib/cjs/index.d.ts.map +1 -0
  38. package/lib/cjs/index.js +15 -0
  39. package/lib/cjs/package.json +1 -0
  40. package/lib/cjs/search.d.ts +50 -0
  41. package/lib/cjs/search.d.ts.map +1 -0
  42. package/lib/cjs/search.js +55 -0
  43. package/lib/cjs/types.d.ts +88 -0
  44. package/lib/cjs/types.d.ts.map +1 -0
  45. package/lib/cjs/types.js +2 -0
  46. package/lib/cjs/utils.d.ts +87 -0
  47. package/lib/cjs/utils.d.ts.map +1 -0
  48. package/lib/cjs/utils.js +282 -0
  49. package/lib/esm/Expression.d.ts +19 -0
  50. package/lib/esm/Expression.d.ts.map +1 -0
  51. package/lib/esm/Expression.js +35 -0
  52. package/lib/esm/Functions.d.ts +242 -0
  53. package/lib/esm/Functions.d.ts.map +1 -0
  54. package/lib/esm/Functions.js +594 -0
  55. package/lib/esm/Lexer.d.ts +19 -0
  56. package/lib/esm/Lexer.d.ts.map +1 -0
  57. package/lib/esm/Lexer.js +268 -0
  58. package/lib/esm/ParsedResult.d.ts +15 -0
  59. package/lib/esm/ParsedResult.d.ts.map +1 -0
  60. package/lib/esm/ParsedResult.js +30 -0
  61. package/lib/esm/Parser.d.ts +35 -0
  62. package/lib/esm/Parser.d.ts.map +1 -0
  63. package/lib/esm/Parser.js +503 -0
  64. package/lib/esm/PowertoolsFunctions.d.ts +36 -0
  65. package/lib/esm/PowertoolsFunctions.d.ts.map +1 -0
  66. package/lib/esm/PowertoolsFunctions.js +66 -0
  67. package/lib/esm/TreeInterpreter.d.ts +28 -0
  68. package/lib/esm/TreeInterpreter.d.ts.map +1 -0
  69. package/lib/esm/TreeInterpreter.js +310 -0
  70. package/lib/esm/ast.d.ts +185 -0
  71. package/lib/esm/ast.d.ts.map +1 -0
  72. package/lib/esm/ast.js +251 -0
  73. package/lib/esm/constants.d.ts +60 -0
  74. package/lib/esm/constants.d.ts.map +1 -0
  75. package/lib/esm/constants.js +88 -0
  76. package/lib/esm/envelopes.d.ts +69 -0
  77. package/lib/esm/envelopes.d.ts.map +1 -0
  78. package/lib/esm/envelopes.js +74 -0
  79. package/lib/esm/errors.d.ts +183 -0
  80. package/lib/esm/errors.d.ts.map +1 -0
  81. package/lib/esm/errors.js +195 -0
  82. package/lib/esm/index.d.ts +3 -0
  83. package/lib/esm/index.d.ts.map +1 -0
  84. package/lib/esm/index.js +2 -0
  85. package/lib/esm/package.json +1 -0
  86. package/lib/esm/search.d.ts +50 -0
  87. package/lib/esm/search.d.ts.map +1 -0
  88. package/lib/esm/search.js +52 -0
  89. package/lib/esm/types.d.ts +88 -0
  90. package/lib/esm/types.d.ts.map +1 -0
  91. package/lib/esm/types.js +1 -0
  92. package/lib/esm/utils.d.ts +87 -0
  93. package/lib/esm/utils.d.ts.map +1 -0
  94. package/lib/esm/utils.js +275 -0
  95. package/package.json +51 -12
  96. package/lib/index.d.ts +0 -0
  97. package/lib/index.js +0 -4
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Check if a value is truthy.
3
+ *
4
+ * In JavaScript, zero is falsy while all other non-zero numbers are truthy.
5
+ * In JMESPath however, zero is truthy as well as all other non-zero numbers. For
6
+ * this reason we wrap the original isTruthy function from the commons package
7
+ * and add a check for numbers.
8
+ *
9
+ * @param value The value to check
10
+ */
11
+ declare const isTruthy: (value: unknown) => boolean;
12
+ /**
13
+ * Given a start, stop, and step value, the sub elements in an array are extracted as follows:
14
+ * * The first element in the extracted array is the index denoted by start.
15
+ * * The last element in the extracted array is the index denoted by end - 1.
16
+ * * The step value determines how many indices to skip after each element is selected from the array. An array of 1 (the default step) will not skip any indices. A step value of 2 will skip every other index while extracting elements from an array. A step value of -1 will extract values in reverse order from the array.
17
+ *
18
+ * Slice expressions adhere to the following rules:
19
+ * * If a negative start position is given, it is calculated as the total length of the array plus the given start position.
20
+ * * If no start position is given, it is assumed to be 0 if the given step is greater than 0 or the end of the array if the given step is less than 0.
21
+ * * If a negative stop position is given, it is calculated as the total length of the array plus the given stop position.
22
+ * * If no stop position is given, it is assumed to be the length of the array if the given step is greater than 0 or 0 if the given step is less than 0.
23
+ * * If the given step is omitted, it it assumed to be 1.
24
+ * * If the given step is 0, an invalid-value error MUST be raised (thrown before calling the function)
25
+ * * If the element being sliced is not an array, the result is null (returned before calling the function)
26
+ * * If the element being sliced is an array and yields no results, the result MUST be an empty array.
27
+ *
28
+ * @param array The array to slice
29
+ * @param start The start index
30
+ * @param end The end index
31
+ * @param step The step value
32
+ */
33
+ declare const sliceArray: <T>({ array, start, end, step, }: {
34
+ array: T[];
35
+ start?: number;
36
+ end?: number;
37
+ step: number;
38
+ }) => T[] | null;
39
+ /**
40
+ * Checks if the number of arguments passed to a function matches the expected arity.
41
+ * If the number of arguments does not match the expected arity, an ArityError is thrown.
42
+ *
43
+ * If the function is variadic, then the number of arguments passed to the function must be
44
+ * greater than or equal to the expected arity. If the number of arguments passed to the function
45
+ * is less than the expected arity, a `VariadicArityError` is thrown.
46
+ *
47
+ * @param args The arguments passed to the function
48
+ * @param argumentsSpecs The expected types for each argument
49
+ * @param decoratedFuncName The name of the function being called
50
+ * @param variadic Whether the function is variadic
51
+ */
52
+ declare const arityCheck: (args: unknown[], argumentsSpecs: Array<Array<string>>, variadic?: boolean) => void;
53
+ /**
54
+ * Type checks the arguments passed to a function against the expected types.
55
+ *
56
+ * Type checking at runtime involves checking the top level type,
57
+ * and in the case of arrays, potentially checking the types of
58
+ * the elements in the array.
59
+ *
60
+ * If the list of types includes 'any', then the type check is a
61
+ * no-op.
62
+ *
63
+ * If the list of types includes more than one type, then the
64
+ * argument is checked against each type in the list. If the
65
+ * argument matches any of the types, then the type check
66
+ * passes. If the argument does not match any of the types, then
67
+ * a JMESPathTypeError is thrown.
68
+ *
69
+ * @param args The arguments passed to the function
70
+ * @param argumentsSpecs The expected types for each argument
71
+ */
72
+ declare const typeCheck: (args: unknown[], argumentsSpecs: Array<Array<string>>) => void;
73
+ /**
74
+ * Type checks an argument against a list of types.
75
+ *
76
+ * If the list of types includes more than one type, then the
77
+ * argument is checked against each type in the list. If the
78
+ * argument matches any of the types, then the type check
79
+ * passes. If the argument does not match any of the types, then
80
+ * a JMESPathTypeError is thrown.
81
+ *
82
+ * @param arg
83
+ * @param argumentSpec
84
+ */
85
+ declare const typeCheckArgument: (arg: unknown, argumentSpec: Array<string>) => void;
86
+ export { isTruthy, arityCheck, sliceArray, typeCheck, typeCheckArgument };
87
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAUA;;;;;;;;;GASG;AACH,QAAA,MAAM,QAAQ,UAAW,OAAO,KAAG,OAMlC,CAAC;AA4BF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,QAAA,MAAM,UAAU,oCAKb;IACD,KAAK,EAAE,CAAC,EAAE,CAAC;IACX,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd,KAAG,CAAC,EAAE,GAAG,IA0BT,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,QAAA,MAAM,UAAU,SACR,OAAO,EAAE,kBACC,MAAM,MAAM,MAAM,CAAC,CAAC,aACzB,OAAO,KACjB,IAcF,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,QAAA,MAAM,SAAS,SACP,OAAO,EAAE,kBACC,MAAM,MAAM,MAAM,CAAC,CAAC,KACnC,IAKF,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,QAAA,MAAM,iBAAiB,QAAS,OAAO,gBAAgB,MAAM,MAAM,CAAC,KAAG,IAMtE,CAAC;AA4IF,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAE,SAAS,EAAE,iBAAiB,EAAE,CAAC"}
@@ -0,0 +1,282 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.typeCheckArgument = exports.typeCheck = exports.sliceArray = exports.arityCheck = exports.isTruthy = void 0;
4
+ const typeutils_1 = require("@aws-lambda-powertools/commons/typeutils");
5
+ const Expression_js_1 = require("./Expression.js");
6
+ const errors_js_1 = require("./errors.js");
7
+ /**
8
+ * Check if a value is truthy.
9
+ *
10
+ * In JavaScript, zero is falsy while all other non-zero numbers are truthy.
11
+ * In JMESPath however, zero is truthy as well as all other non-zero numbers. For
12
+ * this reason we wrap the original isTruthy function from the commons package
13
+ * and add a check for numbers.
14
+ *
15
+ * @param value The value to check
16
+ */
17
+ const isTruthy = (value) => {
18
+ if ((0, typeutils_1.isNumber)(value)) {
19
+ return true;
20
+ }
21
+ else {
22
+ return (0, typeutils_1.isTruthy)(value);
23
+ }
24
+ };
25
+ exports.isTruthy = isTruthy;
26
+ /**
27
+ * @internal
28
+ * Cap a slice range value to the length of an array, taking into account
29
+ * negative values and whether the step is negative.
30
+ *
31
+ * @param arrayLength The length of the array
32
+ * @param value The value to cap
33
+ * @param isStepNegative Whether the step is negative
34
+ */
35
+ const capSliceRange = (arrayLength, value, isStepNegative) => {
36
+ if (value < 0) {
37
+ value += arrayLength;
38
+ if (value < 0) {
39
+ value = isStepNegative ? -1 : 0;
40
+ }
41
+ }
42
+ else if (value >= arrayLength) {
43
+ value = isStepNegative ? arrayLength - 1 : arrayLength;
44
+ }
45
+ return value;
46
+ };
47
+ /**
48
+ * Given a start, stop, and step value, the sub elements in an array are extracted as follows:
49
+ * * The first element in the extracted array is the index denoted by start.
50
+ * * The last element in the extracted array is the index denoted by end - 1.
51
+ * * The step value determines how many indices to skip after each element is selected from the array. An array of 1 (the default step) will not skip any indices. A step value of 2 will skip every other index while extracting elements from an array. A step value of -1 will extract values in reverse order from the array.
52
+ *
53
+ * Slice expressions adhere to the following rules:
54
+ * * If a negative start position is given, it is calculated as the total length of the array plus the given start position.
55
+ * * If no start position is given, it is assumed to be 0 if the given step is greater than 0 or the end of the array if the given step is less than 0.
56
+ * * If a negative stop position is given, it is calculated as the total length of the array plus the given stop position.
57
+ * * If no stop position is given, it is assumed to be the length of the array if the given step is greater than 0 or 0 if the given step is less than 0.
58
+ * * If the given step is omitted, it it assumed to be 1.
59
+ * * If the given step is 0, an invalid-value error MUST be raised (thrown before calling the function)
60
+ * * If the element being sliced is not an array, the result is null (returned before calling the function)
61
+ * * If the element being sliced is an array and yields no results, the result MUST be an empty array.
62
+ *
63
+ * @param array The array to slice
64
+ * @param start The start index
65
+ * @param end The end index
66
+ * @param step The step value
67
+ */
68
+ const sliceArray = ({ array, start, end, step, }) => {
69
+ const isStepNegative = step < 0;
70
+ const length = array.length;
71
+ const defaultStart = isStepNegative ? length - 1 : 0;
72
+ const defaultEnd = isStepNegative ? -1 : length;
73
+ start = (0, typeutils_1.isIntegerNumber)(start)
74
+ ? capSliceRange(length, start, isStepNegative)
75
+ : defaultStart;
76
+ end = (0, typeutils_1.isIntegerNumber)(end)
77
+ ? capSliceRange(length, end, isStepNegative)
78
+ : defaultEnd;
79
+ const result = [];
80
+ if (step > 0) {
81
+ for (let i = start; i < end; i += step) {
82
+ result.push(array[i]);
83
+ }
84
+ }
85
+ else {
86
+ for (let i = start; i > end; i += step) {
87
+ result.push(array[i]);
88
+ }
89
+ }
90
+ return result;
91
+ };
92
+ exports.sliceArray = sliceArray;
93
+ /**
94
+ * Checks if the number of arguments passed to a function matches the expected arity.
95
+ * If the number of arguments does not match the expected arity, an ArityError is thrown.
96
+ *
97
+ * If the function is variadic, then the number of arguments passed to the function must be
98
+ * greater than or equal to the expected arity. If the number of arguments passed to the function
99
+ * is less than the expected arity, a `VariadicArityError` is thrown.
100
+ *
101
+ * @param args The arguments passed to the function
102
+ * @param argumentsSpecs The expected types for each argument
103
+ * @param decoratedFuncName The name of the function being called
104
+ * @param variadic Whether the function is variadic
105
+ */
106
+ const arityCheck = (args, argumentsSpecs, variadic) => {
107
+ if (variadic) {
108
+ if (args.length < argumentsSpecs.length) {
109
+ throw new errors_js_1.VariadicArityError({
110
+ expectedArity: argumentsSpecs.length,
111
+ actualArity: args.length,
112
+ });
113
+ }
114
+ }
115
+ else if (args.length !== argumentsSpecs.length) {
116
+ throw new errors_js_1.ArityError({
117
+ expectedArity: argumentsSpecs.length,
118
+ actualArity: args.length,
119
+ });
120
+ }
121
+ };
122
+ exports.arityCheck = arityCheck;
123
+ /**
124
+ * Type checks the arguments passed to a function against the expected types.
125
+ *
126
+ * Type checking at runtime involves checking the top level type,
127
+ * and in the case of arrays, potentially checking the types of
128
+ * the elements in the array.
129
+ *
130
+ * If the list of types includes 'any', then the type check is a
131
+ * no-op.
132
+ *
133
+ * If the list of types includes more than one type, then the
134
+ * argument is checked against each type in the list. If the
135
+ * argument matches any of the types, then the type check
136
+ * passes. If the argument does not match any of the types, then
137
+ * a JMESPathTypeError is thrown.
138
+ *
139
+ * @param args The arguments passed to the function
140
+ * @param argumentsSpecs The expected types for each argument
141
+ */
142
+ const typeCheck = (args, argumentsSpecs) => {
143
+ for (const [index, argumentSpec] of argumentsSpecs.entries()) {
144
+ if (argumentSpec[0] === 'any')
145
+ continue;
146
+ typeCheckArgument(args[index], argumentSpec);
147
+ }
148
+ };
149
+ exports.typeCheck = typeCheck;
150
+ /**
151
+ * Type checks an argument against a list of types.
152
+ *
153
+ * If the list of types includes more than one type, then the
154
+ * argument is checked against each type in the list. If the
155
+ * argument matches any of the types, then the type check
156
+ * passes. If the argument does not match any of the types, then
157
+ * a JMESPathTypeError is thrown.
158
+ *
159
+ * @param arg
160
+ * @param argumentSpec
161
+ */
162
+ const typeCheckArgument = (arg, argumentSpec) => {
163
+ let valid = false;
164
+ argumentSpec.forEach((type, index) => {
165
+ if (valid)
166
+ return;
167
+ valid = checkIfArgumentTypeIsValid(arg, type, index, argumentSpec);
168
+ });
169
+ };
170
+ exports.typeCheckArgument = typeCheckArgument;
171
+ /**
172
+ * Check if the argument is of the expected type.
173
+ *
174
+ * @param arg The argument to check
175
+ * @param type The expected type
176
+ * @param index The index of the type we are checking
177
+ * @param argumentSpec The list of types to check against
178
+ */
179
+ const checkIfArgumentTypeIsValid = (arg, type, index, argumentSpec) => {
180
+ const hasMoreTypesToCheck = index < argumentSpec.length - 1;
181
+ if (type.startsWith('array')) {
182
+ if (!Array.isArray(arg)) {
183
+ if (hasMoreTypesToCheck) {
184
+ return false;
185
+ }
186
+ throw new errors_js_1.JMESPathTypeError({
187
+ currentValue: arg,
188
+ expectedTypes: argumentSpec,
189
+ actualType: (0, typeutils_1.getType)(arg),
190
+ });
191
+ }
192
+ checkComplexArrayType(arg, type, hasMoreTypesToCheck);
193
+ return true;
194
+ }
195
+ if (type === 'expression') {
196
+ checkExpressionType(arg, argumentSpec, hasMoreTypesToCheck);
197
+ return true;
198
+ }
199
+ else if (['string', 'number', 'boolean'].includes(type)) {
200
+ typeCheckType(arg, type, argumentSpec, hasMoreTypesToCheck);
201
+ if (typeof arg === type)
202
+ return true;
203
+ }
204
+ else if (type === 'object') {
205
+ checkObjectType(arg, argumentSpec, hasMoreTypesToCheck);
206
+ return true;
207
+ }
208
+ return false;
209
+ };
210
+ /**
211
+ * Check if the argument is of the expected type.
212
+ *
213
+ * @param arg The argument to check
214
+ * @param type The type to check against
215
+ * @param argumentSpec The list of types to check against
216
+ * @param hasMoreTypesToCheck Whether there are more types to check
217
+ */
218
+ const typeCheckType = (arg, type, argumentSpec, hasMoreTypesToCheck) => {
219
+ if (typeof arg !== type && !hasMoreTypesToCheck) {
220
+ throw new errors_js_1.JMESPathTypeError({
221
+ currentValue: arg,
222
+ expectedTypes: argumentSpec,
223
+ actualType: (0, typeutils_1.getType)(arg),
224
+ });
225
+ }
226
+ };
227
+ /**
228
+ * Check if the argument is an array of complex types.
229
+ *
230
+ * @param arg The argument to check
231
+ * @param type The type to check against
232
+ * @param hasMoreTypesToCheck Whether there are more types to check
233
+ */
234
+ const checkComplexArrayType = (arg, type, hasMoreTypesToCheck) => {
235
+ if (!type.includes('-'))
236
+ return;
237
+ const arrayItemsType = type.slice(6);
238
+ let actualType;
239
+ for (const element of arg) {
240
+ try {
241
+ typeCheckArgument(element, [arrayItemsType]);
242
+ actualType = arrayItemsType;
243
+ }
244
+ catch (error) {
245
+ if (!hasMoreTypesToCheck || actualType !== undefined) {
246
+ throw error;
247
+ }
248
+ }
249
+ }
250
+ };
251
+ /**
252
+ * Check if the argument is an expression.
253
+ *
254
+ * @param arg The argument to check
255
+ * @param type The type to check against
256
+ * @param hasMoreTypesToCheck Whether there are more types to check
257
+ */
258
+ const checkExpressionType = (arg, type, hasMoreTypesToCheck) => {
259
+ if (!(arg instanceof Expression_js_1.Expression) && !hasMoreTypesToCheck) {
260
+ throw new errors_js_1.JMESPathTypeError({
261
+ currentValue: arg,
262
+ expectedTypes: type,
263
+ actualType: (0, typeutils_1.getType)(arg),
264
+ });
265
+ }
266
+ };
267
+ /**
268
+ * Check if the argument is an object.
269
+ *
270
+ * @param arg The argument to check
271
+ * @param type The type to check against
272
+ * @param hasMoreTypesToCheck Whether there are more types to check
273
+ */
274
+ const checkObjectType = (arg, type, hasMoreTypesToCheck) => {
275
+ if (!(0, typeutils_1.isRecord)(arg) && !hasMoreTypesToCheck) {
276
+ throw new errors_js_1.JMESPathTypeError({
277
+ currentValue: arg,
278
+ expectedTypes: type,
279
+ actualType: (0, typeutils_1.getType)(arg),
280
+ });
281
+ }
282
+ };
@@ -0,0 +1,19 @@
1
+ import type { TreeInterpreter } from './TreeInterpreter.js';
2
+ import type { JSONObject, Node } from './types.js';
3
+ /**
4
+ * Apply a JMESPath expression to a JSON value.
5
+ */
6
+ declare class Expression {
7
+ #private;
8
+ constructor(expression: Node, interpreter: TreeInterpreter);
9
+ /**
10
+ * Evaluate the expression against a JSON value.
11
+ *
12
+ * @param value The JSON value to apply the expression to.
13
+ * @param node The node to visit.
14
+ * @returns The result of applying the expression to the value.
15
+ */
16
+ visit(value: JSONObject, node?: Node): JSONObject;
17
+ }
18
+ export { Expression };
19
+ //# sourceMappingURL=Expression.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Expression.d.ts","sourceRoot":"","sources":["../../src/Expression.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAEnD;;GAEG;AACH,cAAM,UAAU;;gBAIK,UAAU,EAAE,IAAI,EAAE,WAAW,EAAE,eAAe;IAKjE;;;;;;OAMG;IACI,KAAK,CAAC,KAAK,EAAE,UAAU,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG,UAAU;CAGzD;AAED,OAAO,EAAE,UAAU,EAAE,CAAC"}
@@ -0,0 +1,35 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _Expression_expression, _Expression_interpreter;
13
+ /**
14
+ * Apply a JMESPath expression to a JSON value.
15
+ */
16
+ class Expression {
17
+ constructor(expression, interpreter) {
18
+ _Expression_expression.set(this, void 0);
19
+ _Expression_interpreter.set(this, void 0);
20
+ __classPrivateFieldSet(this, _Expression_expression, expression, "f");
21
+ __classPrivateFieldSet(this, _Expression_interpreter, interpreter, "f");
22
+ }
23
+ /**
24
+ * Evaluate the expression against a JSON value.
25
+ *
26
+ * @param value The JSON value to apply the expression to.
27
+ * @param node The node to visit.
28
+ * @returns The result of applying the expression to the value.
29
+ */
30
+ visit(value, node) {
31
+ return __classPrivateFieldGet(this, _Expression_interpreter, "f").visit(node ?? __classPrivateFieldGet(this, _Expression_expression, "f"), value);
32
+ }
33
+ }
34
+ _Expression_expression = new WeakMap(), _Expression_interpreter = new WeakMap();
35
+ export { Expression };
@@ -0,0 +1,242 @@
1
+ import type { JSONArray, JSONObject, JSONValue } from '@aws-lambda-powertools/commons/types';
2
+ import type { Expression } from './Expression.js';
3
+ import type { FunctionSignatureDecorator, FunctionSignatureOptions } from './types.js';
4
+ /**
5
+ * A class that contains the built-in JMESPath functions.
6
+ *
7
+ * The built-in functions are implemented as methods on the Functions class.
8
+ * Each method is decorated with the `@Function.signature()` decorator to enforce the
9
+ * arity and types of the arguments passed to the function at runtime.
10
+ *
11
+ * You can extend the Functions class to add custom functions by creating a new class
12
+ * that extends Functions and adding new methods to it.
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * import { search } from '@aws-lambda-powertools/jmespath';
17
+ * import { Functions } from '@aws-lambda-powertools/jmespath/functions';
18
+ *
19
+ * class MyFunctions extends Functions {
20
+ * ⁣@Functions.signature({
21
+ * argumentsSpecs: [['number'], ['number']],
22
+ * variadic: true,
23
+ * })
24
+ * public funcMyMethod(args: Array<number>): unknown {
25
+ * // ...
26
+ * }
27
+ * }
28
+ *
29
+ * const myFunctions = new MyFunctions();
30
+ *
31
+ * search('myMethod(@)', {}, { customFunctions: new MyFunctions() });
32
+ * ```
33
+ */
34
+ declare class Functions {
35
+ methods: Set<string>;
36
+ /**
37
+ * Get the absolute value of the provided number.
38
+ *
39
+ * @param args The number to get the absolute value of
40
+ */
41
+ funcAbs(args: number): number;
42
+ /**
43
+ * Calculate the average of the numbers in the provided array.
44
+ *
45
+ * @param args The numbers to average
46
+ */
47
+ funcAvg(args: Array<number>): number;
48
+ /**
49
+ * Get the ceiling of the provided number.
50
+ *
51
+ * @param args The number to get the ceiling of
52
+ */
53
+ funcCeil(args: number): number;
54
+ /**
55
+ * Determine if the given value is contained in the provided array or string.
56
+ *
57
+ * @param haystack The array or string to check
58
+ * @param needle The value to check for
59
+ */
60
+ funcContains(haystack: string, needle: string): boolean;
61
+ /**
62
+ * Determines if the provided string ends with the provided suffix.
63
+ *
64
+ * @param str The string to check
65
+ * @param suffix The suffix to check for
66
+ */
67
+ funcEndsWith(str: string, suffix: string): boolean;
68
+ /**
69
+ * Get the floor of the provided number.
70
+ *
71
+ * @param args The number to get the floor of
72
+ */
73
+ funcFloor(args: number): number;
74
+ /**
75
+ * Join the provided array into a single string.
76
+ *
77
+ * @param separator The separator to use
78
+ * @param items The array of itmes to join
79
+ */
80
+ funcJoin(separator: string, items: Array<string>): string;
81
+ /**
82
+ * Get the keys of the provided object.
83
+ *
84
+ * @param arg The object to get the keys of
85
+ */
86
+ funcKeys(arg: JSONObject): string[];
87
+ /**
88
+ * Get the number of items in the provided item.
89
+ *
90
+ * @param arg The array to get the length of
91
+ */
92
+ funcLength(arg: string | Array<unknown> | Record<string, unknown>): number;
93
+ /**
94
+ * Map the provided function over the provided array.
95
+ *
96
+ * @param expression The expression to map over the array
97
+ * @param args The array to map the expression over
98
+ */
99
+ funcMap(expression: Expression, args: JSONArray): JSONArray | Array<unknown>;
100
+ /**
101
+ * Get the maximum value in the provided array.
102
+ *
103
+ * @param arg The array to get the maximum value of
104
+ */
105
+ funcMax(arg: Array<number | string>): number | string | null;
106
+ /**
107
+ * Get the item in the provided array that has the maximum value when the provided expression is evaluated.
108
+ *
109
+ * @param args The array of items to get the maximum value of
110
+ * @param expression The expression to evaluate for each item in the array
111
+ */
112
+ funcMaxBy(args: Array<JSONObject>, expression: Expression): JSONObject | null;
113
+ /**
114
+ * Merge the provided objects into a single object.
115
+ *
116
+ * Note that this is a shallow merge and will not merge nested objects.
117
+ *
118
+ * @param args The objects to merge
119
+ */
120
+ funcMerge(...args: Array<JSONObject>): JSONObject;
121
+ /**
122
+ * Get the minimum value in the provided array.
123
+ *
124
+ * @param arg The array to get the minimum value of
125
+ */
126
+ funcMin(arg: Array<number>): number | string | null;
127
+ /**
128
+ * Get the item in the provided array that has the minimum value when the provided expression is evaluated.
129
+ *
130
+ * @param args The array of items to get the minimum value of
131
+ * @param expression The expression to evaluate for each item in the array
132
+ */
133
+ funcMinBy(args: Array<JSONObject>, expression: Expression): JSONObject | null;
134
+ /**
135
+ * Get the first argument that does not evaluate to null.
136
+ * If all arguments evaluate to null, then null is returned.
137
+ *
138
+ * @param args The keys of the items to check
139
+ */
140
+ funcNotNull(...args: Array<JSONValue>): JSONValue | null;
141
+ /**
142
+ * Reverses the provided string or array.
143
+ *
144
+ * @param arg The string or array to reverse
145
+ */
146
+ funcReverse(arg: string | Array<unknown>): string | Array<unknown>;
147
+ /**
148
+ * Sort the provided array.
149
+ *
150
+ * @param arg The array to sort
151
+ */
152
+ funcSort(arg: Array<string> | Array<number>): Array<unknown>;
153
+ /**
154
+ * Sort the provided array by the provided expression.
155
+ *
156
+ * @param args The array to sort
157
+ * @param expression The expression to sort by
158
+ */
159
+ funcSortBy(args: Array<JSONValue>, expression: Expression): Array<unknown>;
160
+ /**
161
+ * Determines if the provided string starts with the provided prefix.
162
+ *
163
+ * @param str The string to check
164
+ * @param prefix The prefix to check for
165
+ */
166
+ funcStartsWith(str: string, prefix: string): boolean;
167
+ /**
168
+ * Sum the provided numbers.
169
+ *
170
+ * @param args The numbers to sum
171
+ */
172
+ funcSum(args: Array<number>): number;
173
+ /**
174
+ * Convert the provided value to an array.
175
+ *
176
+ * If the provided value is an array, then it is returned.
177
+ * Otherwise, the value is wrapped in an array and returned.
178
+ *
179
+ * @param arg The items to convert to an array
180
+ */
181
+ funcToArray(arg: JSONArray | Array<JSONValue>): Array<JSONValue> | JSONArray;
182
+ /**
183
+ * Convert the provided value to a number.
184
+ *
185
+ * If the provided value is a number, then it is returned.
186
+ * Otherwise, the value is converted to a number and returned.
187
+ *
188
+ * If the value cannot be converted to a number, then null is returned.
189
+ *
190
+ * @param arg The value to convert to a number
191
+ */
192
+ funcToNumber(arg: JSONValue): number | null;
193
+ /**
194
+ * Convert the provided value to a string.
195
+ *
196
+ * If the provided value is a string, then it is returned.
197
+ * Otherwise, the value is converted to a string and returned.
198
+ *
199
+ * @param arg The value to convert to a string
200
+ */
201
+ funcToString(arg: JSONValue): string;
202
+ /**
203
+ * Get the type of the provided value.
204
+ *
205
+ * @param arg The value to check the type of
206
+ */
207
+ funcType(arg: JSONValue): string;
208
+ /**
209
+ * Get the values of the provided object.
210
+ *
211
+ * @param arg The object to get the values of
212
+ */
213
+ funcValues(arg: JSONObject): JSONValue[];
214
+ introspectMethods(scope?: Functions): Set<string>;
215
+ /**
216
+ * Decorator to enforce the signature of a function at runtime.
217
+ *
218
+ * The signature decorator enforces the arity and types of the arguments
219
+ * passed to a function at runtime. If the arguments do not match the
220
+ * expected arity or types errors are thrown.
221
+ *
222
+ * @example
223
+ * ```typescript
224
+ * import { PowertoolsFunctions } from '@aws-lambda-powertools/jmespath/functions';
225
+ *
226
+ * class MyFunctions extends Functions {
227
+ * ⁣@Functions.signature({
228
+ * argumentsSpecs: [['number'], ['number']],
229
+ * variadic: true,
230
+ * })
231
+ * public funcMyMethod(args: Array<number>): unknown {
232
+ * // ...
233
+ * }
234
+ * }
235
+ * ```
236
+ *
237
+ * @param options The options for the signature decorator
238
+ */
239
+ static signature(options: FunctionSignatureOptions): FunctionSignatureDecorator;
240
+ }
241
+ export { Functions };
242
+ //# sourceMappingURL=Functions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Functions.d.ts","sourceRoot":"","sources":["../../src/Functions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EACT,UAAU,EACV,SAAS,EACV,MAAM,sCAAsC,CAAC;AAM9C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAElD,OAAO,KAAK,EACV,0BAA0B,EA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