Skip to content

Commit

Permalink
Export TreeInterpreter in order to search compiled expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Glen Van Ginkel committed Jun 1, 2020
1 parent e0db9c5 commit b11b2f5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ You can precompile all your expressions ready for use later on. the `compile`
function takes a JMESPath expression and returns an abstract syntax tree that
can be used by the TreeInterpreter function

```javascript
import { compile, TreeInterpreter } from '@metrichor/jmespath';

const ast = compile('foo.bar');

TreeInterpreter.search(ast, {foo: {bar: 'BAZ'}})
// RETURNS: "BAZ"


```


## More Resources

Expand Down
13 changes: 8 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Parser from './Parser';
import Lexer from './Lexer';
import TreeInterpreter from './TreeInterpreter';
import TreeInterpreterInst from './TreeInterpreter';
import {
ExpressionNodeTree,
LexerToken,
Expand All @@ -21,9 +21,9 @@ export const TYPE_NUMBER = InputArgument.TYPE_NUMBER;
export const TYPE_OBJECT = InputArgument.TYPE_OBJECT;
export const TYPE_STRING = InputArgument.TYPE_STRING;

export function compile(expression: string): (data: JSONValue) => JSONValue {
export function compile(expression: string): ExpressionNodeTree {
const nodeTree = Parser.parse(expression);
return (data: JSONValue) => TreeInterpreter.search(nodeTree, data);
return nodeTree;
}

export function tokenize(expression: string): LexerToken[] {
Expand All @@ -35,19 +35,22 @@ export const registerFunction = (
customFunction: RuntimeFunction<any, any>,
signature: InputSignature[],
): void => {
TreeInterpreter.runtime.registerFunction(functionName, customFunction, signature);
TreeInterpreterInst.runtime.registerFunction(functionName, customFunction, signature);
};

export function search(data: JSONValue, expression: string): JSONValue {
const nodeTree = Parser.parse(expression);
return TreeInterpreter.search(nodeTree, data);
return TreeInterpreterInst.search(nodeTree, data);
}

export const TreeInterpreter = TreeInterpreterInst;

export const jmespath = {
compile,
registerFunction,
search,
tokenize,
TreeInterpreter,
TYPE_ANY,
TYPE_ARRAY_NUMBER,
TYPE_ARRAY_STRING,
Expand Down
9 changes: 8 additions & 1 deletion test/jmespath.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import jmespath, { search, tokenize, compile, registerFunction } from '../src';
import jmespath, { search, tokenize, compile, registerFunction, TreeInterpreter } from '../src';
import { strictDeepEqual } from '../src/utils';

describe('tokenize', () => {
Expand Down Expand Up @@ -106,6 +106,13 @@ describe('parsing', () => {
});
});

describe('Searches compiled ast', () => {
it('search a compiled expression', () => {
const ast = compile('foo.bar');
expect(TreeInterpreter.search(ast, {foo: {bar: 'BAZ'}})).toEqual('BAZ');
});
});

describe('strictDeepEqual', () => {
it('should compare scalars', () => {
expect(strictDeepEqual('a', 'a')).toStrictEqual(true);
Expand Down

0 comments on commit b11b2f5

Please sign in to comment.