Skip to content

Commit d340d50

Browse files
committed
feat(jmespath): add parser component
1 parent ba4561d commit d340d50

File tree

2 files changed

+878
-0
lines changed

2 files changed

+878
-0
lines changed

Diff for: packages/jmespath/src/ParsedResult.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { TreeInterpreter } from './TreeInterpreter.js';
2+
import {
3+
ArityError,
4+
JMESPathTypeError,
5+
UnknownFunctionError,
6+
VariadicArityError,
7+
} from './errors.js';
8+
import type { Node, ParsingOptions, JSONObject } from './types.js';
9+
10+
class ParsedResult {
11+
public expression: string;
12+
public parsed: Node;
13+
14+
public constructor(expression: string, parsed: Node) {
15+
this.expression = expression;
16+
this.parsed = parsed;
17+
}
18+
19+
/**
20+
* Perform a JMESPath search on a JSON value.
21+
*
22+
* @param value The JSON value to search
23+
* @param options The parsing options to use
24+
*/
25+
public search(value: JSONObject, options?: ParsingOptions): unknown {
26+
const interpreter = new TreeInterpreter(options);
27+
28+
try {
29+
return interpreter.visit(this.parsed, value);
30+
} catch (error) {
31+
if (
32+
error instanceof JMESPathTypeError ||
33+
error instanceof UnknownFunctionError ||
34+
error instanceof ArityError ||
35+
error instanceof VariadicArityError
36+
) {
37+
error.setExpression(this.expression);
38+
}
39+
throw error;
40+
}
41+
}
42+
}
43+
44+
export { ParsedResult };

0 commit comments

Comments
 (0)