Mentions légales du service

Skip to content
Snippets Groups Projects
Commit ef808d34 authored by Brett Zamir's avatar Brett Zamir
Browse files

Return null or undefined if supplied; deal with nullish values by requiring "new" by avoided

parent 1639b925
No related branches found
No related tags found
No related merge requests found
...@@ -34,8 +34,16 @@ function push (arr, elem) {arr = arr.slice(); arr.push(elem); return arr;} ...@@ -34,8 +34,16 @@ function push (arr, elem) {arr = arr.slice(); arr.push(elem); return arr;}
function unshift (elem, arr) {arr = arr.slice(); arr.unshift(elem); return arr;} function unshift (elem, arr) {arr = arr.slice(); arr.unshift(elem); return arr;}
function JSONPath (opts, obj, expr) { function JSONPath (opts, obj, expr) {
if (!(this instanceof JSONPath)) { // Make "new" optional if (!(this instanceof JSONPath)) {
return new JSONPath(opts, obj, expr); try {
return new JSONPath(opts, obj, expr);
}
catch (e) {
if (!e.avoidNew) {
throw e;
}
return e.value;
}
} }
opts = opts || {}; opts = opts || {};
...@@ -46,7 +54,10 @@ function JSONPath (opts, obj, expr) { ...@@ -46,7 +54,10 @@ function JSONPath (opts, obj, expr) {
this.sandbox = opts.sandbox || {}; this.sandbox = opts.sandbox || {};
if (opts.autostart !== false) { if (opts.autostart !== false) {
return this.evaluate((objArgs ? opts.json : obj), (objArgs ? opts.path : expr)); var ret = this.evaluate((objArgs ? opts.json : obj), (objArgs ? opts.path : expr));
if (!ret) {
throw {avoidNew: true, value: ret, message: "JSONPath should not be called with 'new'"};
}
} }
} }
...@@ -213,7 +224,7 @@ JSONPath.prototype._eval = function (code, _v, _vname, path) { ...@@ -213,7 +224,7 @@ JSONPath.prototype._eval = function (code, _v, _vname, path) {
// For backward compatibility (deprecated) // For backward compatibility (deprecated)
JSONPath.eval = function (obj, expr, opts) { JSONPath.eval = function (obj, expr, opts) {
return new JSONPath(opts, obj, expr); return JSONPath(opts, obj, expr);
}; };
if (typeof module === 'undefined') { if (typeof module === 'undefined') {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment