Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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