@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,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"}
|
package/lib/cjs/utils.js
ADDED
|
@@ -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,262 @@
|
|
|
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
|
+
/**
|
|
36
|
+
* A set of all the custom functions available in this and all child classes.
|
|
37
|
+
*/
|
|
38
|
+
methods: Set<string>;
|
|
39
|
+
/**
|
|
40
|
+
* Get the absolute value of the provided number.
|
|
41
|
+
*
|
|
42
|
+
* @param args The number to get the absolute value of
|
|
43
|
+
*/
|
|
44
|
+
funcAbs(args: number): number;
|
|
45
|
+
/**
|
|
46
|
+
* Calculate the average of the numbers in the provided array.
|
|
47
|
+
*
|
|
48
|
+
* @param args The numbers to average
|
|
49
|
+
*/
|
|
50
|
+
funcAvg(args: Array<number>): number;
|
|
51
|
+
/**
|
|
52
|
+
* Get the ceiling of the provided number.
|
|
53
|
+
*
|
|
54
|
+
* @param args The number to get the ceiling of
|
|
55
|
+
*/
|
|
56
|
+
funcCeil(args: number): number;
|
|
57
|
+
/**
|
|
58
|
+
* Determine if the given value is contained in the provided array or string.
|
|
59
|
+
*
|
|
60
|
+
* @param haystack The array or string to check
|
|
61
|
+
* @param needle The value to check for
|
|
62
|
+
*/
|
|
63
|
+
funcContains(haystack: string, needle: string): boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Determines if the provided string ends with the provided suffix.
|
|
66
|
+
*
|
|
67
|
+
* @param str The string to check
|
|
68
|
+
* @param suffix The suffix to check for
|
|
69
|
+
*/
|
|
70
|
+
funcEndsWith(str: string, suffix: string): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Get the floor of the provided number.
|
|
73
|
+
*
|
|
74
|
+
* @param args The number to get the floor of
|
|
75
|
+
*/
|
|
76
|
+
funcFloor(args: number): number;
|
|
77
|
+
/**
|
|
78
|
+
* Join the provided array into a single string.
|
|
79
|
+
*
|
|
80
|
+
* @param separator The separator to use
|
|
81
|
+
* @param items The array of itmes to join
|
|
82
|
+
*/
|
|
83
|
+
funcJoin(separator: string, items: Array<string>): string;
|
|
84
|
+
/**
|
|
85
|
+
* Get the keys of the provided object.
|
|
86
|
+
*
|
|
87
|
+
* @param arg The object to get the keys of
|
|
88
|
+
*/
|
|
89
|
+
funcKeys(arg: JSONObject): string[];
|
|
90
|
+
/**
|
|
91
|
+
* Get the number of items in the provided item.
|
|
92
|
+
*
|
|
93
|
+
* @param arg The array to get the length of
|
|
94
|
+
*/
|
|
95
|
+
funcLength(arg: string | Array<unknown> | Record<string, unknown>): number;
|
|
96
|
+
/**
|
|
97
|
+
* Map the provided function over the provided array.
|
|
98
|
+
*
|
|
99
|
+
* @param expression The expression to map over the array
|
|
100
|
+
* @param args The array to map the expression over
|
|
101
|
+
*/
|
|
102
|
+
funcMap(expression: Expression, args: JSONArray): JSONArray | Array<unknown>;
|
|
103
|
+
/**
|
|
104
|
+
* Get the maximum value in the provided array.
|
|
105
|
+
*
|
|
106
|
+
* @param arg The array to get the maximum value of
|
|
107
|
+
*/
|
|
108
|
+
funcMax(arg: Array<number | string>): number | string | null;
|
|
109
|
+
/**
|
|
110
|
+
* Get the item in the provided array that has the maximum value when the provided expression is evaluated.
|
|
111
|
+
*
|
|
112
|
+
* @param args The array of items to get the maximum value of
|
|
113
|
+
* @param expression The expression to evaluate for each item in the array
|
|
114
|
+
*/
|
|
115
|
+
funcMaxBy(args: Array<JSONObject>, expression: Expression): JSONObject | null;
|
|
116
|
+
/**
|
|
117
|
+
* Merge the provided objects into a single object.
|
|
118
|
+
*
|
|
119
|
+
* Note that this is a shallow merge and will not merge nested objects.
|
|
120
|
+
*
|
|
121
|
+
* @param args The objects to merge
|
|
122
|
+
*/
|
|
123
|
+
funcMerge(...args: Array<JSONObject>): JSONObject;
|
|
124
|
+
/**
|
|
125
|
+
* Get the minimum value in the provided array.
|
|
126
|
+
*
|
|
127
|
+
* @param arg The array to get the minimum value of
|
|
128
|
+
*/
|
|
129
|
+
funcMin(arg: Array<number>): number | string | null;
|
|
130
|
+
/**
|
|
131
|
+
* Get the item in the provided array that has the minimum value when the provided expression is evaluated.
|
|
132
|
+
*
|
|
133
|
+
* @param args The array of items to get the minimum value of
|
|
134
|
+
* @param expression The expression to evaluate for each item in the array
|
|
135
|
+
*/
|
|
136
|
+
funcMinBy(args: Array<JSONObject>, expression: Expression): JSONObject | null;
|
|
137
|
+
/**
|
|
138
|
+
* Get the first argument that does not evaluate to null.
|
|
139
|
+
* If all arguments evaluate to null, then null is returned.
|
|
140
|
+
*
|
|
141
|
+
* @param args The keys of the items to check
|
|
142
|
+
*/
|
|
143
|
+
funcNotNull(...args: Array<JSONValue>): JSONValue | null;
|
|
144
|
+
/**
|
|
145
|
+
* Reverses the provided string or array.
|
|
146
|
+
*
|
|
147
|
+
* @param arg The string or array to reverse
|
|
148
|
+
*/
|
|
149
|
+
funcReverse(arg: string | Array<unknown>): string | Array<unknown>;
|
|
150
|
+
/**
|
|
151
|
+
* Sort the provided array.
|
|
152
|
+
*
|
|
153
|
+
* @param arg The array to sort
|
|
154
|
+
*/
|
|
155
|
+
funcSort(arg: Array<string> | Array<number>): Array<unknown>;
|
|
156
|
+
/**
|
|
157
|
+
* Sort the provided array by the provided expression.
|
|
158
|
+
*
|
|
159
|
+
* @param args The array to sort
|
|
160
|
+
* @param expression The expression to sort by
|
|
161
|
+
*/
|
|
162
|
+
funcSortBy(args: Array<JSONValue>, expression: Expression): Array<unknown>;
|
|
163
|
+
/**
|
|
164
|
+
* Determines if the provided string starts with the provided prefix.
|
|
165
|
+
*
|
|
166
|
+
* @param str The string to check
|
|
167
|
+
* @param prefix The prefix to check for
|
|
168
|
+
*/
|
|
169
|
+
funcStartsWith(str: string, prefix: string): boolean;
|
|
170
|
+
/**
|
|
171
|
+
* Sum the provided numbers.
|
|
172
|
+
*
|
|
173
|
+
* @param args The numbers to sum
|
|
174
|
+
*/
|
|
175
|
+
funcSum(args: Array<number>): number;
|
|
176
|
+
/**
|
|
177
|
+
* Convert the provided value to an array.
|
|
178
|
+
*
|
|
179
|
+
* If the provided value is an array, then it is returned.
|
|
180
|
+
* Otherwise, the value is wrapped in an array and returned.
|
|
181
|
+
*
|
|
182
|
+
* @param arg The items to convert to an array
|
|
183
|
+
*/
|
|
184
|
+
funcToArray(arg: JSONArray | Array<JSONValue>): Array<JSONValue> | JSONArray;
|
|
185
|
+
/**
|
|
186
|
+
* Convert the provided value to a number.
|
|
187
|
+
*
|
|
188
|
+
* If the provided value is a number, then it is returned.
|
|
189
|
+
* Otherwise, the value is converted to a number and returned.
|
|
190
|
+
*
|
|
191
|
+
* If the value cannot be converted to a number, then null is returned.
|
|
192
|
+
*
|
|
193
|
+
* @param arg The value to convert to a number
|
|
194
|
+
*/
|
|
195
|
+
funcToNumber(arg: JSONValue): number | null;
|
|
196
|
+
/**
|
|
197
|
+
* Convert the provided value to a string.
|
|
198
|
+
*
|
|
199
|
+
* If the provided value is a string, then it is returned.
|
|
200
|
+
* Otherwise, the value is converted to a string and returned.
|
|
201
|
+
*
|
|
202
|
+
* @param arg The value to convert to a string
|
|
203
|
+
*/
|
|
204
|
+
funcToString(arg: JSONValue): string;
|
|
205
|
+
/**
|
|
206
|
+
* Get the type of the provided value.
|
|
207
|
+
*
|
|
208
|
+
* @param arg The value to check the type of
|
|
209
|
+
*/
|
|
210
|
+
funcType(arg: JSONValue): string;
|
|
211
|
+
/**
|
|
212
|
+
* Get the values of the provided object.
|
|
213
|
+
*
|
|
214
|
+
* @param arg The object to get the values of
|
|
215
|
+
*/
|
|
216
|
+
funcValues(arg: JSONObject): JSONValue[];
|
|
217
|
+
/**
|
|
218
|
+
* Lazily introspects the methods of the class instance and all child classes
|
|
219
|
+
* to get the names of the methods that correspond to JMESPath functions.
|
|
220
|
+
*
|
|
221
|
+
* This method is used to get the names of the custom functions that are available
|
|
222
|
+
* in the class instance and all child classes. The names of the functions are used
|
|
223
|
+
* to create the custom function map that is passed to the JMESPath search function.
|
|
224
|
+
*
|
|
225
|
+
* The method traverses the inheritance chain going from the leaf class to the root class
|
|
226
|
+
* and stops when it reaches the `Functions` class, which is the root class.
|
|
227
|
+
*
|
|
228
|
+
* In doing so, it collects the names of the methods that start with `func` and adds them
|
|
229
|
+
* to the `methods` set. Finally, when the recursion collects back to the current instance,
|
|
230
|
+
* it adds the collected methods to the `this.methods` set so that they can be accessed later.
|
|
231
|
+
*
|
|
232
|
+
* @param scope The scope of the class instance to introspect
|
|
233
|
+
*/
|
|
234
|
+
introspectMethods(scope?: Functions): Set<string>;
|
|
235
|
+
/**
|
|
236
|
+
* Decorator to enforce the signature of a function at runtime.
|
|
237
|
+
*
|
|
238
|
+
* The signature decorator enforces the arity and types of the arguments
|
|
239
|
+
* passed to a function at runtime. If the arguments do not match the
|
|
240
|
+
* expected arity or types errors are thrown.
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```typescript
|
|
244
|
+
* import { PowertoolsFunctions } from '@aws-lambda-powertools/jmespath/functions';
|
|
245
|
+
*
|
|
246
|
+
* class MyFunctions extends Functions {
|
|
247
|
+
* @Functions.signature({
|
|
248
|
+
* argumentsSpecs: [['number'], ['number']],
|
|
249
|
+
* variadic: true,
|
|
250
|
+
* })
|
|
251
|
+
* public funcMyMethod(args: Array<number>): unknown {
|
|
252
|
+
* // ...
|
|
253
|
+
* }
|
|
254
|
+
* }
|
|
255
|
+
* ```
|
|
256
|
+
*
|
|
257
|
+
* @param options The options for the signature decorator
|
|
258
|
+
*/
|
|
259
|
+
static signature(options: FunctionSignatureOptions): FunctionSignatureDecorator;
|
|
260
|
+
}
|
|
261
|
+
export { Functions };
|
|
262
|
+
//# sourceMappingURL=Functions.d.ts.map
|