Permalink
Browse files

Cache expressions

  • Loading branch information...
1 parent eabe328 commit 1040c1c2c68d9c47ef2ea8ddb30154f0e4f76f72 @s3u committed Aug 27, 2011
Showing with 8 additions and 2 deletions.
  1. +8 −2 lib/jsonpath.js
View
@@ -5,18 +5,24 @@
*/
exports.eval = jsonPath;
-
+var cache = {};
function jsonPath(obj, expr, arg) {
var P = {
resultType: arg && arg.resultType || "VALUE",
result: [],
normalize: function(expr) {
+ if(cache[expr]) {
+ return cache[expr];
+ }
+
var subx = [];
- return expr.replace(/[\['](\??\(.*?\))[\]']/g, function($0,$1){return "[#"+(subx.push($1)-1)+"]";})
+ ret = expr.replace(/[\['](\??\(.*?\))[\]']/g, function($0,$1){return "[#"+(subx.push($1)-1)+"]";})
.replace(/'?\.'?|\['?/g, ";")
.replace(/;;;|;;/g, ";..;")
.replace(/;$|'?\]|'$/g, "")
.replace(/#([0-9]+)/g, function($0,$1){return subx[$1];});
+ cache[expr] = ret;
+ return ret;
},
asPath: function(path) {
var x = path.split(";"), p = "$";

0 comments on commit 1040c1c

Please sign in to comment.