From 7a5b0aef257bf77046a65915bf8e5d3804885ed4 Mon Sep 17 00:00:00 2001 From: Piotr Mionskowski Date: Tue, 19 Apr 2016 11:42:35 +0200 Subject: [PATCH] Allow reusing parse results by creating searcher --- jmespath.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/jmespath.js b/jmespath.js index f59e8e4..94d852f 100644 --- a/jmespath.js +++ b/jmespath.js @@ -1649,19 +1649,26 @@ } function search(data, expression) { - var parser = new Parser(); + return searcher(expression)(data); + } + + function searcher(expression){ // This needs to be improved. Both the interpreter and runtime depend on // each other. The runtime needs the interpreter to support exprefs. // There's likely a clean way to avoid the cyclic dependency. var runtime = new Runtime(); var interpreter = new TreeInterpreter(runtime); runtime._interpreter = interpreter; + var parser = new Parser(); var node = parser.parse(expression); - return interpreter.search(node, data); + return function(data){ + return interpreter.search(node, data); + } } exports.tokenize = tokenize; exports.compile = compile; exports.search = search; + exports.searcher = searcher; exports.strictDeepEqual = strictDeepEqual; })(typeof exports === "undefined" ? this.jmespath = {} : exports);