Skip to content

Commit 8d685b5

Browse files
committed
chore: update build files
1 parent 258832a commit 8d685b5

8 files changed

+174
-156
lines changed

dist/index-browser-esm.js

Lines changed: 68 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -889,38 +889,31 @@ JSONPath.prototype._slice = function (loc, expr, val, path, parent, parentPropNa
889889
};
890890

891891
JSONPath.prototype._eval = function (code, _v, _vname, path, parent, parentPropName) {
892-
if (code.includes('@parentProperty')) {
893-
this.currSandbox._$_parentProperty = parentPropName;
894-
code = code.replace(/@parentProperty/g, '_$_parentProperty');
895-
}
896-
897-
if (code.includes('@parent')) {
898-
this.currSandbox._$_parent = parent;
899-
code = code.replace(/@parent/g, '_$_parent');
892+
this.currSandbox._$_parentProperty = parentPropName;
893+
this.currSandbox._$_parent = parent;
894+
this.currSandbox._$_property = _vname;
895+
this.currSandbox._$_root = this.json;
896+
this.currSandbox._$_v = _v;
897+
var containsPath = code.includes('@path');
898+
899+
if (containsPath) {
900+
this.currSandbox._$_path = JSONPath.toPathString(path.concat([_vname]));
900901
}
901902

902-
if (code.includes('@property')) {
903-
this.currSandbox._$_property = _vname;
904-
code = code.replace(/@property/g, '_$_property');
905-
}
903+
var scriptCacheKey = 'script:' + code;
906904

907-
if (code.includes('@path')) {
908-
this.currSandbox._$_path = JSONPath.toPathString(path.concat([_vname]));
909-
code = code.replace(/@path/g, '_$_path');
910-
}
905+
if (!JSONPath.cache[scriptCacheKey]) {
906+
var script = code.replace(/@parentProperty/g, '_$_parentProperty').replace(/@parent/g, '_$_parent').replace(/@property/g, '_$_property').replace(/@root/g, '_$_root').replace(/@([\t-\r \)\.\[\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF])/g, '_$_v$1');
911907

912-
if (code.includes('@root')) {
913-
this.currSandbox._$_root = this.json;
914-
code = code.replace(/@root/g, '_$_root');
915-
}
908+
if (containsPath) {
909+
script = script.replace(/@path/g, '_$_path');
910+
}
916911

917-
if (/@([\t-\r \)\.\[\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF])/.test(code)) {
918-
this.currSandbox._$_v = _v;
919-
code = code.replace(/@([\t-\r \)\.\[\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF])/g, '_$_v$1');
912+
JSONPath.cache[scriptCacheKey] = new this.vm.Script(script);
920913
}
921914

922915
try {
923-
return this.vm.runInNewContext(code, this.currSandbox);
916+
return JSONPath.cache[scriptCacheKey].runInNewContext(this.currSandbox);
924917
} catch (e) {
925918
throw new Error('jsonPath: ' + e.message + ': ' + code);
926919
}
@@ -1040,48 +1033,71 @@ var moveToAnotherArray = function moveToAnotherArray(source, target, conditionCb
10401033
}
10411034
}
10421035
};
1036+
/**
1037+
* In-browser replacement for NodeJS' VM.Script.
1038+
*/
10431039

1044-
JSONPath.prototype.vm = {
1040+
1041+
var Script = /*#__PURE__*/function () {
10451042
/**
10461043
* @param {string} expr Expression to evaluate
1044+
*/
1045+
function Script(expr) {
1046+
_classCallCheck(this, Script);
1047+
1048+
this.code = expr;
1049+
}
1050+
/**
10471051
* @param {PlainObject} context Object whose items will be added
10481052
* to evaluation
10491053
* @returns {EvaluatedResult} Result of evaluated code
10501054
*/
1051-
runInNewContext: function runInNewContext(expr, context) {
1052-
var keys = Object.keys(context);
1053-
var funcs = [];
1054-
moveToAnotherArray(keys, funcs, function (key) {
1055-
return typeof context[key] === 'function';
1056-
});
1057-
var values = keys.map(function (vr, i) {
1058-
return context[vr];
1059-
});
1060-
var funcString = funcs.reduce(function (s, func) {
1061-
var fString = context[func].toString();
10621055

1063-
if (!/function/.test(fString)) {
1064-
fString = 'function ' + fString;
1065-
}
10661056

1067-
return 'var ' + func + '=' + fString + ';' + s;
1068-
}, '');
1069-
expr = funcString + expr; // Mitigate http://perfectionkills.com/global-eval-what-are-the-options/#new_function
1057+
_createClass(Script, [{
1058+
key: "runInNewContext",
1059+
value: function runInNewContext(context) {
1060+
var expr = this.code;
1061+
var keys = Object.keys(context);
1062+
var funcs = [];
1063+
moveToAnotherArray(keys, funcs, function (key) {
1064+
return typeof context[key] === 'function';
1065+
});
1066+
var values = keys.map(function (vr, i) {
1067+
return context[vr];
1068+
});
1069+
var funcString = funcs.reduce(function (s, func) {
1070+
var fString = context[func].toString();
1071+
1072+
if (!/function/.test(fString)) {
1073+
fString = 'function ' + fString;
1074+
}
10701075

1071-
if (!/(["'])use strict\1/.test(expr) && !keys.includes('arguments')) {
1072-
expr = 'var arguments = undefined;' + expr;
1073-
} // Remove last semi so `return` will be inserted before
1074-
// the previous one instead, allowing for the return
1075-
// of a bare ending expression
1076+
return 'var ' + func + '=' + fString + ';' + s;
1077+
}, '');
1078+
expr = funcString + expr; // Mitigate http://perfectionkills.com/global-eval-what-are-the-options/#new_function
10761079

1080+
if (!/(["'])use strict\1/.test(expr) && !keys.includes('arguments')) {
1081+
expr = 'var arguments = undefined;' + expr;
1082+
} // Remove last semi so `return` will be inserted before
1083+
// the previous one instead, allowing for the return
1084+
// of a bare ending expression
10771085

1078-
expr = expr.replace(/;[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*$/, ''); // Insert `return`
10791086

1080-
var lastStatementEnd = expr.lastIndexOf(';');
1081-
var code = lastStatementEnd > -1 ? expr.slice(0, lastStatementEnd + 1) + ' return ' + expr.slice(lastStatementEnd + 1) : ' return ' + expr; // eslint-disable-next-line no-new-func
1087+
expr = expr.replace(/;[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*$/, ''); // Insert `return`
10821088

1083-
return _construct(Function, _toConsumableArray(keys).concat([code])).apply(void 0, _toConsumableArray(values));
1084-
}
1089+
var lastStatementEnd = expr.lastIndexOf(';');
1090+
var code = lastStatementEnd > -1 ? expr.slice(0, lastStatementEnd + 1) + ' return ' + expr.slice(lastStatementEnd + 1) : ' return ' + expr; // eslint-disable-next-line no-new-func
1091+
1092+
return _construct(Function, _toConsumableArray(keys).concat([code])).apply(void 0, _toConsumableArray(values));
1093+
}
1094+
}]);
1095+
1096+
return Script;
1097+
}();
1098+
1099+
JSONPath.prototype.vm = {
1100+
Script: Script
10851101
};
10861102

10871103
export { JSONPath };

dist/index-browser-esm.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index-browser-esm.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index-browser-umd.cjs

Lines changed: 68 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -895,38 +895,31 @@
895895
};
896896

897897
JSONPath.prototype._eval = function (code, _v, _vname, path, parent, parentPropName) {
898-
if (code.includes('@parentProperty')) {
899-
this.currSandbox._$_parentProperty = parentPropName;
900-
code = code.replace(/@parentProperty/g, '_$_parentProperty');
901-
}
902-
903-
if (code.includes('@parent')) {
904-
this.currSandbox._$_parent = parent;
905-
code = code.replace(/@parent/g, '_$_parent');
898+
this.currSandbox._$_parentProperty = parentPropName;
899+
this.currSandbox._$_parent = parent;
900+
this.currSandbox._$_property = _vname;
901+
this.currSandbox._$_root = this.json;
902+
this.currSandbox._$_v = _v;
903+
var containsPath = code.includes('@path');
904+
905+
if (containsPath) {
906+
this.currSandbox._$_path = JSONPath.toPathString(path.concat([_vname]));
906907
}
907908

908-
if (code.includes('@property')) {
909-
this.currSandbox._$_property = _vname;
910-
code = code.replace(/@property/g, '_$_property');
911-
}
909+
var scriptCacheKey = 'script:' + code;
912910

913-
if (code.includes('@path')) {
914-
this.currSandbox._$_path = JSONPath.toPathString(path.concat([_vname]));
915-
code = code.replace(/@path/g, '_$_path');
916-
}
911+
if (!JSONPath.cache[scriptCacheKey]) {
912+
var script = code.replace(/@parentProperty/g, '_$_parentProperty').replace(/@parent/g, '_$_parent').replace(/@property/g, '_$_property').replace(/@root/g, '_$_root').replace(/@([\t-\r \)\.\[\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF])/g, '_$_v$1');
917913

918-
if (code.includes('@root')) {
919-
this.currSandbox._$_root = this.json;
920-
code = code.replace(/@root/g, '_$_root');
921-
}
914+
if (containsPath) {
915+
script = script.replace(/@path/g, '_$_path');
916+
}
922917

923-
if (/@([\t-\r \)\.\[\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF])/.test(code)) {
924-
this.currSandbox._$_v = _v;
925-
code = code.replace(/@([\t-\r \)\.\[\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF])/g, '_$_v$1');
918+
JSONPath.cache[scriptCacheKey] = new this.vm.Script(script);
926919
}
927920

928921
try {
929-
return this.vm.runInNewContext(code, this.currSandbox);
922+
return JSONPath.cache[scriptCacheKey].runInNewContext(this.currSandbox);
930923
} catch (e) {
931924
throw new Error('jsonPath: ' + e.message + ': ' + code);
932925
}
@@ -1046,48 +1039,71 @@
10461039
}
10471040
}
10481041
};
1042+
/**
1043+
* In-browser replacement for NodeJS' VM.Script.
1044+
*/
10491045

1050-
JSONPath.prototype.vm = {
1046+
1047+
var Script = /*#__PURE__*/function () {
10511048
/**
10521049
* @param {string} expr Expression to evaluate
1050+
*/
1051+
function Script(expr) {
1052+
_classCallCheck(this, Script);
1053+
1054+
this.code = expr;
1055+
}
1056+
/**
10531057
* @param {PlainObject} context Object whose items will be added
10541058
* to evaluation
10551059
* @returns {EvaluatedResult} Result of evaluated code
10561060
*/
1057-
runInNewContext: function runInNewContext(expr, context) {
1058-
var keys = Object.keys(context);
1059-
var funcs = [];
1060-
moveToAnotherArray(keys, funcs, function (key) {
1061-
return typeof context[key] === 'function';
1062-
});
1063-
var values = keys.map(function (vr, i) {
1064-
return context[vr];
1065-
});
1066-
var funcString = funcs.reduce(function (s, func) {
1067-
var fString = context[func].toString();
10681061

1069-
if (!/function/.test(fString)) {
1070-
fString = 'function ' + fString;
1071-
}
10721062

1073-
return 'var ' + func + '=' + fString + ';' + s;
1074-
}, '');
1075-
expr = funcString + expr; // Mitigate http://perfectionkills.com/global-eval-what-are-the-options/#new_function
1063+
_createClass(Script, [{
1064+
key: "runInNewContext",
1065+
value: function runInNewContext(context) {
1066+
var expr = this.code;
1067+
var keys = Object.keys(context);
1068+
var funcs = [];
1069+
moveToAnotherArray(keys, funcs, function (key) {
1070+
return typeof context[key] === 'function';
1071+
});
1072+
var values = keys.map(function (vr, i) {
1073+
return context[vr];
1074+
});
1075+
var funcString = funcs.reduce(function (s, func) {
1076+
var fString = context[func].toString();
1077+
1078+
if (!/function/.test(fString)) {
1079+
fString = 'function ' + fString;
1080+
}
10761081

1077-
if (!/(["'])use strict\1/.test(expr) && !keys.includes('arguments')) {
1078-
expr = 'var arguments = undefined;' + expr;
1079-
} // Remove last semi so `return` will be inserted before
1080-
// the previous one instead, allowing for the return
1081-
// of a bare ending expression
1082+
return 'var ' + func + '=' + fString + ';' + s;
1083+
}, '');
1084+
expr = funcString + expr; // Mitigate http://perfectionkills.com/global-eval-what-are-the-options/#new_function
10821085

1086+
if (!/(["'])use strict\1/.test(expr) && !keys.includes('arguments')) {
1087+
expr = 'var arguments = undefined;' + expr;
1088+
} // Remove last semi so `return` will be inserted before
1089+
// the previous one instead, allowing for the return
1090+
// of a bare ending expression
10831091

1084-
expr = expr.replace(/;[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*$/, ''); // Insert `return`
10851092

1086-
var lastStatementEnd = expr.lastIndexOf(';');
1087-
var code = lastStatementEnd > -1 ? expr.slice(0, lastStatementEnd + 1) + ' return ' + expr.slice(lastStatementEnd + 1) : ' return ' + expr; // eslint-disable-next-line no-new-func
1093+
expr = expr.replace(/;[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*$/, ''); // Insert `return`
10881094

1089-
return _construct(Function, _toConsumableArray(keys).concat([code])).apply(void 0, _toConsumableArray(values));
1090-
}
1095+
var lastStatementEnd = expr.lastIndexOf(';');
1096+
var code = lastStatementEnd > -1 ? expr.slice(0, lastStatementEnd + 1) + ' return ' + expr.slice(lastStatementEnd + 1) : ' return ' + expr; // eslint-disable-next-line no-new-func
1097+
1098+
return _construct(Function, _toConsumableArray(keys).concat([code])).apply(void 0, _toConsumableArray(values));
1099+
}
1100+
}]);
1101+
1102+
return Script;
1103+
}();
1104+
1105+
JSONPath.prototype.vm = {
1106+
Script: Script
10911107
};
10921108

10931109
exports.JSONPath = JSONPath;

0 commit comments

Comments
 (0)