Skip to content

Commit 8782ba3

Browse files
committed
allow async functions
1 parent b8cc4ba commit 8782ba3

File tree

5 files changed

+4030
-1636
lines changed

5 files changed

+4030
-1636
lines changed

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ you use, `jmespath.search`:
1313

1414
```
1515
> var jmespath = require('jmespath');
16-
> jmespath.search({foo: {bar: {baz: [0, 1, 2, 3, 4]}}}, "foo.bar.baz[2]")
16+
> await jmespath.search({foo: {bar: {baz: [0, 1, 2, 3, 4]}}}, "foo.bar.baz[2]")
1717
2
1818
```
1919

@@ -26,15 +26,15 @@ The JMESPath language can do a lot more than select an element
2626
from a list. Here are a few more examples:
2727

2828
```
29-
> jmespath.search({foo: {bar: {baz: [0, 1, 2, 3, 4]}}}, "foo.bar")
29+
> await jmespath.search({foo: {bar: {baz: [0, 1, 2, 3, 4]}}}, "foo.bar")
3030
{ baz: [ 0, 1, 2, 3, 4 ] }
3131
32-
> jmespath.search({"foo": [{"first": "a", "last": "b"},
32+
> await jmespath.search({"foo": [{"first": "a", "last": "b"},
3333
{"first": "c", "last": "d"}]},
3434
"foo[*].first")
3535
[ 'a', 'c' ]
3636
37-
> jmespath.search({"foo": [{"age": 20}, {"age": 25},
37+
> await jmespath.search({"foo": [{"age": 20}, {"age": 25},
3838
{"age": 30}, {"age": 35},
3939
{"age": 40}]},
4040
"foo[?age > `30`]")
@@ -61,11 +61,9 @@ on the [JMESPath site](http://jmespath.org/specification.html).
6161
As an extension to common JMESPath API and available in jmespath.js only,
6262
custom filter functions can be specified through the ``functionTable``
6363
property of the optional third argument of the ``search`` function.
64-
The custom functions can even call third-party
65-
libraries via closure. The following example shows how a custom
66-
filter function `contains_ci` is implemented with
67-
[`lodash`](https://lodash.com/) library
68-
to provide case insensitive string matching
64+
A custom function can even call third-party
65+
libraries via closure. Custom functions can by asynchronous. The following example shows how a custom async filter function `contains_ci` is implemented with
66+
[`lodash`](https://lodash.com/) library to provide case insensitive string matching
6967

7068
```
7169
const jmespath = require('jmespath')
@@ -75,13 +73,15 @@ let res = jmespath.search([{ a: 'foo' }], "[?contains_ci(a, 'FOO')]", {
7573
functionTable: {
7674
/*jshint camelcase: false */
7775
contains_ci: {
78-
_func: function(resolvedArgs) {
76+
_func: async function(resolvedArgs) {
7977
if (!resolvedArgs[0] || !resolvedArgs[1]) {
8078
return false
8179
}
82-
return (
83-
_.toLower(resolvedArgs[0]).indexOf(_.toLower(resolvedArgs[1])) >= 0
84-
)
80+
return new Promise(resolve => {
81+
setTimeout(() => {
82+
resolve(_.toLower(resolvedArgs[0]).indexOf(_.toLower(resolvedArgs[1])) >= 0)
83+
}, 1000)
84+
})
8585
},
8686
_signature: [
8787
{
@@ -94,7 +94,7 @@ let res = jmespath.search([{ a: 'foo' }], "[?contains_ci(a, 'FOO')]", {
9494
}
9595
}
9696
})
97-
assert.deepStrictEqual(res, [{ a: 'foo' }])
97+
res.then(val => assert.deepStrictEqual(val, [{ a: 'foo' }]))
9898
```
9999

100100
See [type constants](https://github.com/jmespath/jmespath.js/blob/master/jmespath.js#L132) for type mapping used by the example.

0 commit comments

Comments
 (0)