@@ -2,53 +2,48 @@ import { query, type JsonValue } from 'jsonpath-rfc9535';
22
33export function evaluateJSONPathCondition ( condition : string , context : JsonValue ) {
44 try {
5- const jsonpathExpressions = parseJSONPathExpressions ( condition ) ;
6- let replacedCondition = condition ;
5+ const jsonpathExpressions = parseJSONPathExpressions ( condition , context ) ;
6+ const evaluateFn = new Function ( `return ${ jsonpathExpressions . join ( '' ) } ;` ) ;
77
8- for ( let j = jsonpathExpressions . length - 1 ; j >= 0 ; j -- ) {
9- const { expression, start, end } = jsonpathExpressions [ j ] ;
10- const replacement = evaluateExpression ( expression , context ) ;
11-
12- replacedCondition =
13- replacedCondition . slice ( 0 , start ) + replacement + replacedCondition . slice ( end ) ;
14- }
15-
16- const evaluateFn = new Function ( `return ${ replacedCondition } ;` ) ;
178 return ! ! evaluateFn ( ) ;
189 } catch ( error ) {
1910 return false ;
2011 }
2112}
2213
23- function parseJSONPathExpressions ( condition : string ) {
24- const jsonpathExpressions : Array < { expression : string ; start : number ; end : number } > = [ ] ;
14+ function parseJSONPathExpressions ( condition : string , context : JsonValue ) {
15+ const jsonpathExpressions : Array < string > = [ ] ;
2516 let i = 0 ;
17+ let logicalExpressionOrValues = '' ;
2618
2719 while ( i < condition . length ) {
2820 if ( condition [ i ] === '$' ) {
21+ if ( logicalExpressionOrValues . length > 0 ) {
22+ jsonpathExpressions . push ( logicalExpressionOrValues ) ;
23+ logicalExpressionOrValues = '' ;
24+ }
2925 const start = i ;
30- const jsonpath = parseSingleJSONPath ( condition , i ) ;
31-
32- if ( jsonpath . expression . length > 1 ) {
33- jsonpathExpressions . push ( {
34- expression : jsonpath . expression ,
35- start,
36- end : jsonpath . end ,
37- } ) ;
26+ const expression = parseSingleJSONPath ( condition , i ) ;
27+
28+ if ( expression . length > 1 ) {
29+ const evaluatedExpression = evaluateExpression ( expression , context ) ;
30+ jsonpathExpressions . push ( evaluatedExpression ) ;
3831 }
39- i = jsonpath . end ;
32+ i = start + expression . length ;
4033 } else {
34+ logicalExpressionOrValues += condition [ i ] ;
4135 i ++ ;
36+
37+ if ( i >= condition . length && logicalExpressionOrValues . length > 0 ) {
38+ jsonpathExpressions . push ( logicalExpressionOrValues ) ;
39+ }
4240 }
4341 }
4442
4543 return jsonpathExpressions ;
4644}
4745
48- function parseSingleJSONPath (
49- condition : string ,
50- startIndex : number
51- ) : { expression : string ; end : number } {
46+ function parseSingleJSONPath ( condition : string , startIndex : number ) : string {
5247 let jsonpath = '$' ;
5348 let bracketDepth = 0 ;
5449 let inQuotes = false ;
@@ -83,7 +78,7 @@ function parseSingleJSONPath(
8378
8479 if ( char === '[' ) {
8580 bracketDepth ++ ;
86- if ( condition [ i + 1 ] === '?' ) {
81+ if ( i + 1 < condition . length && condition [ i + 1 ] === '?' ) {
8782 inFilter = true ;
8883 filterDepth = bracketDepth ;
8984 }
@@ -111,7 +106,7 @@ function parseSingleJSONPath(
111106 i ++ ;
112107 }
113108
114- return { expression : jsonpath , end : i } ;
109+ return jsonpath ;
115110}
116111
117112function evaluateExpression ( expression : string , context : JsonValue ) : string {
0 commit comments