Skip to content

Commit fbb96f8

Browse files
authored
0.8.5+ with =~ style regex in script filters
jpaquit adds a clever replace() fucntion to match `=~` in something like `@name =~ /key.*/`, allowing for Ruby style regex syntax that fits neatly with the existing `===`, `==!`, `==`, `=!` operators , see: https://github.com/jpaquit/jsonpath/tree/0.8.5-%2B-regexp
1 parent 195b319 commit fbb96f8

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

jsonpath.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
/* JSONPath 0.8.5 - XPath for JSON
1+
/* JSONPath 0.8.5+ - XPath for JSON
22
*
33
* Copyright (c) 2007 Stefan Goessner (goessner.net)
4+
* Copyright (c) 2020 jpaquit
45
* Licensed under the MIT (MIT-LICENSE.txt) licence.
56
*
6-
* Proposal of Chris Zyp goes into version 0.9.x
7-
* Issue 7 resolved
7+
* https://github.com/jpaquit/jsonpath/tree/0.8.5-%2B-regexp
8+
* 2020/01/09 - Add a patch to enable conditional regex as in jayway jsonpath ("=~")
9+
*
810
*/
911
function jsonPath(obj, expr, arg) {
1012
var P = {
@@ -44,7 +46,7 @@ function jsonPath(obj, expr, arg) {
4446
P.trace(P.eval(loc, val, path.substr(path.lastIndexOf(";")+1))+";"+x, val, path);
4547
else if (/^\?\(.*?\)$/.test(loc)) /* [?(expr)] */
4648
P.walk(loc, x, val, path, function(m,l,x,v,p) { if (P.eval(l.replace(/^\?\((.*?)\)$/,"$1"), v instanceof Array ? v[m] : v, m)) P.trace(m+";"+x,v,p); }); /* issue 5 resolved */
47-
else if (/^(-?[0-9]*):(-?[0-9]*):?([0-9]*)$/.test(loc)) / [start:end:step] /* python slice syntax */
49+
else if (/^(-?[0-9]*):(-?[0-9]*):?([0-9]*)$/.test(loc)) /* [start:end:step] python slice syntax */
4850
P.slice(loc, x, val, path);
4951
else if (/,/.test(loc)) { /* [name1,name2,...] */
5052
for (var s=loc.split(/'?,'?/),i=0,n=s.length; i<n; i++)
@@ -77,8 +79,16 @@ function jsonPath(obj, expr, arg) {
7779
}
7880
},
7981
eval: function(x, _v, _vname) {
80-
try { return $ && _v && eval(x.replace(/(^|[^\\])@/g, "$1_v").replace(/\\@/g, "@")); } /* issue 7 : resolved */
81-
catch(e) { throw new SyntaxError("jsonPath: " + e.message + ": " + x.replace(/(^|[^\\])@/g, "$1_v").replace(/\\@/g, "@")); } /* issue 7 : resolved */
82+
try { return $ && _v && eval(x.replace(/(^|[^\\])@/g, "$1_v")
83+
.replace(/\\@/g, "@") /* issue 7 : resolved .. */
84+
.replace(/(_v(?:(?!(\|\||&&)).)*)=~((?:(?!\)* *(\|\||&&)).)*)/g, function(match, p1, p2, p3, offset, currentString) { return match ? p3.trim()+'.test('+p1.trim()+')' : match}) /* 2020/01/09 - manage regexp syntax "=~" */
85+
);
86+
}
87+
catch(e) { throw new SyntaxError("jsonPath: " + e.message + ": " + x.replace(/(^|[^\\])@/g, "$1_v")
88+
.replace(/\\@/g, "@") /* issue 7 : resolved .. */
89+
.replace(/(_v(?:(?!(\|\||&&)).)*)=~((?:(?!\)* *(\|\||&&)).)*)/g, function(match, p1, p2, p3, offset, currentString) { return match ? p3.trim()+'.test('+p1.trim()+')' : match}) /* 2020/01/09 - manage regexp syntax "=~" */
90+
);
91+
}
8292
}
8393
};
8494

@@ -87,4 +97,4 @@ function jsonPath(obj, expr, arg) {
8797
P.trace(P.normalize(expr).replace(/^\$;?/,""), obj, "$"); /* issue 6 resolved */
8898
return P.result.length ? P.result : false;
8999
}
90-
}
100+
}

0 commit comments

Comments
 (0)