-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathsearch.ts
60 lines (57 loc) · 1.48 KB
/
search.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Parser } from './Parser.js';
import type { JMESPathParsingOptions, JSONObject } from './types.js';
const parser = new Parser();
/**
* Search for data in a JSON object using a JMESPath expression.
*
* @example
* ```typescript
* import { search } from '@aws-lambda-powertools/jmespath';
*
* const data = {
* foo: {
* bar: {
* baz: 1
* }
* }
* };
*
* const result = search('foo.bar.baz', data);
* console.log(result); // 1
* ```
*
* By default the search function will use all the built-in functions
* present in the [JMESPath specification](https://jmespath.org/specification.html).
*
* Powertools for AWS Lambda provides some additional functions that can be used
* by passing them in the `customFunctions` option.
*
* @example
* ```typescript
* import { search } from '@aws-lambda-powertools/jmespath';
* import { PowertoolsFunctions } from '@aws-lambda-powertools/jmespath/functions';
*
* const data = {
* body: "{\"foo\": \"bar\"}"
* };
*
* const result = search(
* 'powertools_json(body)',
* data,
* { customFunctions: new PowertoolsFunctions() }
* );
* console.log(result); // { foo: 'bar' }
* ```
*
* @param expression The JMESPath expression to use
* @param data The JSON object to search
* @param options The parsing options to use
*/
const search = (
expression: string,
data: JSONObject,
options?: JMESPathParsingOptions
): unknown => {
return parser.parse(expression).search(data, options);
};
export { search };