1- ![ Build] ( https://github.com/jmespath-community/typescript- jmespath/actions/workflows/nodejs.yml/badge.svg?branch=main )
1+ ![ Build] ( https://github.com/letsflow/ jmespath/actions/workflows/nodejs.yml/badge.svg?branch=main )
22
33# @letsflow/jmespath
44
5- @letsflow/jmespath is a ** TypeScript** implementation of the [ JMESPath] ( https://jmespath.site / ) spec.
5+ @letsflow/jmespath is a ** TypeScript** implementation of the [ JMESPath] ( https://jmespath.org / ) spec.
66
77JMESPath is a query language for JSON. It will take a JSON document
88as input and transform it into another JSON document
99given a JMESPath expression.
1010
1111This fork extends the original specs, adding the following functionality;
12+
1213* [ Custom functions] ( #custom-functions )
1314* [ Root value access] ( #root-value-access )
1415* [ Number literals] ( #number-literals )
@@ -27,9 +28,9 @@ npm install @letsflow/jmespath
2728import { search } from ' @letsflow/jmespath' ;
2829
2930search (
30- {foo: {bar: {baz: [0 , 1 , 2 , 3 , 4 ]}} },
31+ { foo: { bar: { baz: [0 , 1 , 2 , 3 , 4 ] } } },
3132 " foo.bar.baz[2]"
32- );
33+ );
3334
3435// OUTPUTS: 2
3536```
@@ -45,46 +46,46 @@ from a list. Here are a few more examples:
4546``` javascript
4647import { search } from ' @letsflow/jmespath' ;
4748
48- /* --- EXAMPLE 1 --- */
49-
50- let JSON_DOCUMENT = {
49+ const document = {
5150 foo: {
5251 bar: {
5352 baz: [0 , 1 , 2 , 3 , 4 ]
5453 }
5554 }
5655};
5756
58- search (JSON_DOCUMENT , " foo.bar" );
57+ search (document , " foo.bar" );
5958// OUTPUTS: { baz: [ 0, 1, 2, 3, 4 ] }
59+ ```
6060
61+ ``` javascript
62+ import { search } from ' @letsflow/jmespath' ;
6163
62- /* --- EXAMPLE 2 --- */
63-
64- JSON_DOCUMENT = {
64+ const document = {
6565 " foo" : [
66- {" first" : " a" , " last" : " b" },
67- {" first" : " c" , " last" : " d" }
66+ { " first" : " a" , " last" : " b" },
67+ { " first" : " c" , " last" : " d" }
6868 ]
6969};
7070
71- search (JSON_DOCUMENT , " foo[*].first" )
71+ search (document , " foo[*].first" )
7272// OUTPUTS: [ 'a', 'c' ]
73+ ```
7374
75+ ``` javascript
76+ import { search } from ' @letsflow/jmespath' ;
7477
75- /* --- EXAMPLE 3 --- */
76-
77- JSON_DOCUMENT = {
78+ const document = {
7879 " foo" : [
79- {" age" : 20 },
80- {" age" : 25 },
81- {" age" : 30 },
82- {" age" : 35 },
83- {" age" : 40 }
80+ { " age" : 20 },
81+ { " age" : 25 },
82+ { " age" : 30 },
83+ { " age" : 35 },
84+ { " age" : 40 }
8485 ]
8586}
8687
87- search (JSON_DOCUMENT , " foo[?age > `30`]" );
88+ search (document , " foo[?age > `30`]" );
8889// OUTPUTS: [ { age: 35 }, { age: 40 } ]
8990```
9091
@@ -99,66 +100,59 @@ import { compile, TreeInterpreter } from '@jmespath-community/jmespath';
99100
100101const ast = compile (' foo.bar' );
101102
102- TreeInterpreter .search (ast, {foo: {bar: ' BAZ' } })
103+ TreeInterpreter .search (ast, { foo: { bar: ' BAZ' } })
103104// RETURNS: "BAZ"
104-
105105```
106106
107107## EXTENSIONS TO ORIGINAL SPEC
108108
109109### Custom functions
110110
111- #### `registerFunction(functionName: string, customFunction: RuntimeFunction, signature: InputSignature[]): void`
112-
113- Extend the list of built-in JMESpath expressions with your own functions.
114-
115- ```javascript
116- import {search, registerFunction, TYPE_NUMBER} from '@letsflow/jmespath'
111+ #### ` registerFunction(functionName: string, customFunction: RuntimeFunction, signature: InputSignature[]): void `
117112
113+ Extend the list of built-in JMESpath expressions with your own functions.
118114
119- search({ foo: 60, bar: 10 }, 'divide(foo, bar)')
120- // THROWS ERROR: Error: Unknown function: divide()
121-
122- registerFunction(
123- 'divide', // FUNCTION NAME
124- (resolvedArgs) => { // CUSTOM FUNCTION
125- const [dividend, divisor] = resolvedArgs;
126- return dividend / divisor;
127- },
128- [{ types: [TYPE_NUMBER] }, { types: [TYPE_NUMBER] }] //SIGNATURE
129- );
130-
131- search({ foo: 60,bar: 10 }, 'divide(foo, bar)');
132- // OUTPUTS: 6
133-
134- ```
135-
136- Optional arguments are supported by setting `{..., optional: true}` in argument signatures
137-
115+ ``` javascript
116+ import {search , registerFunction , TYPE_NUMBER } from ' @letsflow/jmespath'
117+
118+ search ({ foo: 60 , bar: 10 }, ' divide(foo, bar)' )
119+ // THROWS ERROR: Error: Unknown function: divide()
120+
121+ registerFunction (
122+ ' divide' , // FUNCTION NAME
123+ (resolvedArgs ) => { // CUSTOM FUNCTION
124+ const [dividend , divisor ] = resolvedArgs;
125+ return dividend / divisor;
126+ },
127+ [{ types: [TYPE_NUMBER ] }, { types: [TYPE_NUMBER ] }] // SIGNATURE
128+ );
138129
139- ```javascript
130+ search ({ foo: 60 , bar: 10 }, ' divide(foo, bar)' );
131+ // OUTPUTS: 6
132+ ```
140133
141- registerFunction(
142- 'divide',
143- (resolvedArgs) => {
144- const [dividend, divisor] = resolvedArgs;
145- return dividend / divisor ?? 1; //OPTIONAL DIVISOR THAT DEFAULTS TO 1
146- },
147- [{ types: [TYPE_NUMBER] }, { types: [TYPE_NUMBER], optional: true }] //SIGNATURE
148- );
134+ Optional arguments are supported by setting ` {..., optional: true} ` in argument signatures
149135
150- search({ foo: 60, bar: 10 }, 'divide(foo)');
151- // OUTPUTS: 60
136+ ``` javascript
137+ registerFunction (
138+ ' divide' ,
139+ (resolvedArgs ) => {
140+ const [dividend , divisor ] = resolvedArgs;
141+ return dividend / divisor ?? 1 ; // OPTIONAL DIVISOR THAT DEFAULTS TO 1
142+ },
143+ [{ types: [TYPE_NUMBER ] }, { types: [TYPE_NUMBER ], optional: true }] // SIGNATURE
144+ );
152145
153- ```
146+ search ({ foo: 60 , bar: 10 }, ' divide(foo)' );
147+ // OUTPUTS: 60
148+ ` ` `
154149
155150### Root value access
156151
157152Use ` $` to access the document root.
158153
159154` ` ` javascript
160-
161- search ({foo: {bar: 999 }, baz: [1 , 2 , 3 ]}, ' $.baz[*].[@, $.foo.bar]' )
155+ search ({foo: { bar: 999 }, baz: [1 , 2 , 3 ]}, ' $.baz[*].[@, $.foo.bar]' )
162156
163157// OUTPUTS:
164158// [ [ 1, 999 ], [ 2, 999 ], [ 3, 999 ] ]
@@ -170,7 +164,6 @@ Numbers in the root scope are treated as number literals. This means that you do
170164need to quote numbers with backticks.
171165
172166` ` ` javascript
173-
174167search ([{" bar" : 1 }, {" bar" : 10 }]}, ' [?bar==10]' )
175168
176169// OUTPUTS;
0 commit comments