Skip to content

Commit 879c68b

Browse files
committed
chore: changes after review
1 parent 587d1bd commit 879c68b

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

packages/respect-core/src/modules/flow-runner/success-criteria/evaluate-jsonpath-condition.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,48 @@ import { query, type JsonValue } from 'jsonpath-rfc9535';
22

33
export function evaluateJSONPathCondition(condition: string, context: JsonValue) {
44
try {
5-
const jsonpathExpressions = parseJSONPathExpressions(condition, context);
6-
const evaluateFn = new Function(`return ${jsonpathExpressions.join('')};`);
5+
const resolvedCondition = parseExpressions(condition, context);
6+
const evaluateFn = new Function(`return ${resolvedCondition};`);
77

88
return !!evaluateFn();
99
} catch (error) {
1010
return false;
1111
}
1212
}
1313

14-
function parseJSONPathExpressions(condition: string, context: JsonValue) {
15-
const jsonpathExpressions: Array<string> = [];
14+
function parseExpressions(condition: string, context: JsonValue): string {
15+
const expressionsParts: Array<string> = [];
16+
1617
let i = 0;
17-
let logicalExpressionOrValues = '';
18+
let expressionElements = '';
1819

1920
while (i < condition.length) {
2021
if (condition[i] === '$') {
21-
if (logicalExpressionOrValues.length > 0) {
22-
jsonpathExpressions.push(logicalExpressionOrValues);
23-
logicalExpressionOrValues = '';
22+
if (expressionElements.length > 0) {
23+
expressionsParts.push(expressionElements);
24+
expressionElements = '';
2425
}
2526
const start = i;
2627
const expression = parseSingleJSONPath(condition, i);
2728

2829
if (expression.length > 1) {
29-
const evaluatedExpression = evaluateExpression(expression, context);
30-
jsonpathExpressions.push(evaluatedExpression);
30+
const evaluatedExpression = evaluateJSONPathExpression(expression, context);
31+
32+
expressionsParts.push(evaluatedExpression);
3133
}
3234
i = start + expression.length;
3335
} else {
34-
logicalExpressionOrValues += condition[i];
36+
expressionElements += condition[i];
3537
i++;
36-
37-
if (i >= condition.length && logicalExpressionOrValues.length > 0) {
38-
jsonpathExpressions.push(logicalExpressionOrValues);
39-
}
4038
}
4139
}
4240

43-
return jsonpathExpressions;
41+
// Push any remaining content after the while loop
42+
if (expressionElements.length > 0) {
43+
expressionsParts.push(expressionElements);
44+
}
45+
46+
return expressionsParts.join('');
4447
}
4548

4649
function parseSingleJSONPath(condition: string, startIndex: number): string {
@@ -109,7 +112,7 @@ function parseSingleJSONPath(condition: string, startIndex: number): string {
109112
return jsonpath;
110113
}
111114

112-
function evaluateExpression(expression: string, context: JsonValue): string {
115+
function evaluateJSONPathExpression(expression: string, context: JsonValue): string {
113116
// Handle legacy .length suffix for backward compatibility that is not a valid RFC 9535 expression
114117
if (expression.endsWith('.length')) {
115118
const basePath = expression.slice(0, -'.length'.length);
@@ -125,8 +128,7 @@ function evaluateExpression(expression: string, context: JsonValue): string {
125128

126129
const normalizedPath = transformHyphensToUnderscores(expression);
127130
const result = query(context, normalizedPath);
128-
const value = result[0] ?? null;
129-
return JSON.stringify(value);
131+
return JSON.stringify(result[0]);
130132
}
131133

132134
function handleFilterExpression(expression: string, context: JsonValue): string {

0 commit comments

Comments
 (0)