Skip to content

Commit

Permalink
feat(jmespath): add parser component
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamorosi committed Mar 21, 2024
1 parent ba4561d commit d340d50
Show file tree
Hide file tree
Showing 2 changed files with 878 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/jmespath/src/ParsedResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { TreeInterpreter } from './TreeInterpreter.js';
import {
ArityError,
JMESPathTypeError,
UnknownFunctionError,
VariadicArityError,
} from './errors.js';
import type { Node, ParsingOptions, JSONObject } from './types.js';

class ParsedResult {
public expression: string;
public parsed: Node;

public constructor(expression: string, parsed: Node) {
this.expression = expression;
this.parsed = parsed;
}

/**
* Perform a JMESPath search on a JSON value.
*
* @param value The JSON value to search
* @param options The parsing options to use
*/
public search(value: JSONObject, options?: ParsingOptions): unknown {
const interpreter = new TreeInterpreter(options);

try {
return interpreter.visit(this.parsed, value);
} catch (error) {
if (
error instanceof JMESPathTypeError ||
error instanceof UnknownFunctionError ||
error instanceof ArityError ||
error instanceof VariadicArityError
) {
error.setExpression(this.expression);
}
throw error;
}
}
}

export { ParsedResult };
Loading

0 comments on commit d340d50

Please sign in to comment.