Add generic scope support for expressions.
Hey thanks for the awesome library. I'm sending a PR adding functionality where variables and functions can now be injected into the expression scope like so:
jsonpath.scope({foo:'bar',parseInt:parseInt})
This is so the client can inject custom variables and functions to be used when evaluating expressions like so:
$..[?(parseInt(@.Amount) > 0)]
I'd say jsonpath should focus on query, safely and as fast as possible. Check out 'json-tots' on npm for transformations capabilities. Disclaimer: This is my own view, I'm not speaking for the maintainer(s) of this package
@sdawood appreciate the comments. This patch wouldn't preclude fast / safe querying, nor does it serve as a transformation mechanism, rather it allows the developer to use custom helper / utility methods during the query process.
The primary use case we had when developing this patch was to query JSON transactions as returned from rippled (https://github.com/ripple/rippled) which stores all floating point values as strings (so as to avoid platform-specific discrepancies with floating point representation). This patch would allow the developer to query this json for float values which match specified numerical conditions. Otherwise the dev would need to run a first pass using jsonpath, then do a second round of querying using some other custom logic.
See, our discussion and comparing problem scopes, one-query -> jsonpath language is specified. custom systematic transformation -> is where I started experimenting with json-tots, and focus on trasducer-pipelines for values cleanup and casting. And I use jsonpath as my assembly language to access json shaped memory faster and without cognitive overhead of custom assertions.
@movitto, Great problem statement and enough intuition to start a good conversation amongst the json consumer community 😎
Thanks for this PR. With it, I was able to make partial matches easily:
jp.scope({ normalizeCompare: (a,b) => {
return a.toLowerCase().includes(b.toLowerCase());
}});
jp.nodes(books, `$..sheets[?(normalizeCompare(@.title, '${query}'))]`)
https://github.com/infojunkie/sheetdex