forked from JSONPath-Plus/JSONPath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex-es.min.js
More file actions
2 lines (2 loc) · 12.8 KB
/
index-es.min.js
File metadata and controls
2 lines (2 loc) · 12.8 KB
1
2
function t(r){return(t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(r)}function r(t){return(r=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function e(t,r){return(e=Object.setPrototypeOf||function(t,r){return t.__proto__=r,t})(t,r)}function n(t,r,a){return(n=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}()?Reflect.construct:function(t,r,n){var a=[null];a.push.apply(a,r);var u=new(Function.bind.apply(t,a));return n&&e(u,n.prototype),u}).apply(null,arguments)}function a(t){var u="function"==typeof Map?new Map:void 0;return(a=function(t){if(null===t||(a=t,-1===Function.toString.call(a).indexOf("[native code]")))return t;var a;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==u){if(u.has(t))return u.get(t);u.set(t,o)}function o(){return n(t,arguments,r(this).constructor)}return o.prototype=Object.create(t.prototype,{constructor:{value:o,enumerable:!1,writable:!0,configurable:!0}}),e(o,t)})(t)}function u(t,r){return!r||"object"!=typeof r&&"function"!=typeof r?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):r}function o(t){return function(t){if(Array.isArray(t)){for(var r=0,e=new Array(t.length);r<t.length;r++)e[r]=t[r];return e}}(t)||function(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}var i=["value","path","pointer","parent","parentProperty","all"],c=Object.prototype.hasOwnProperty,l=function(){try{return"[object process]"===Object.prototype.toString.call(global.process)}catch(t){return!1}}()?require("vm"):{runInNewContext:function(t,r){var e=Object.keys(r),a=[];!function(t,r,e){for(var n=t.length,a=0;a<n;a++){e(t[a])&&r.push(t.splice(a--,1)[0])}}(e,a,(function(t){return"function"==typeof r[t]}));var u=e.map((function(t,e){return r[t]}));(t=a.reduce((function(t,e){var n=r[e].toString();return/function/.exec(n)||(n="function "+n),"var "+e+"="+n+";"+t}),"")+t).match(/(["'])use strict\1/)||e.includes("arguments")||(t="var arguments = undefined;"+t);var i=(t=t.replace(/;[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*$/,"")).lastIndexOf(";"),c=i>-1?t.slice(0,i+1)+" return "+t.slice(i+1):" return "+t;return n(Function,o(e).concat([c])).apply(void 0,o(u))}};function s(t,r){return(t=t.slice()).push(r),t}function p(t,r){return(r=r.slice()).unshift(t),r}var h=function(t){function n(t){var e;return function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,n),(e=u(this,r(n).call(this,'JSONPath should not be called with "new" (it prevents return of (unwrapped) scalar values)'))).avoidNew=!0,e.value=t,e.name="NewError",e}return function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(r&&r.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),r&&e(t,r)}(n,a(Error)),n}();function f(r,e,n,a,u){if(!(this instanceof f))try{return new f(r,e,n,a,u)}catch(t){if(!t.avoidNew)throw t;return t.value}"string"==typeof r&&(u=a,a=n,n=e,e=r,r=null);var o=r&&"object"===t(r);if(r=r||{},this.json=r.json||n,this.path=r.path||e,this.resultType=r.resultType&&r.resultType.toLowerCase()||"value",this.flatten=r.flatten||!1,this.wrap=!c.call(r,"wrap")||r.wrap,this.sandbox=r.sandbox||{},this.preventEval=r.preventEval||!1,this.parent=r.parent||null,this.parentProperty=r.parentProperty||null,this.callback=r.callback||a||null,this.otherTypeCallback=r.otherTypeCallback||u||function(){throw new TypeError("You must supply an otherTypeCallback callback option with the @other() operator.")},!1!==r.autostart){var i={path:o?r.path:e};o?"json"in r&&(i.json=r.json):i.json=n;var l=this.evaluate(i);if(!l||"object"!==t(l))throw new h(l);return l}}f.prototype.evaluate=function(r,e,n,a){var u=this,o=this.parent,l=this.parentProperty,s=this.flatten,p=this.wrap;if(this.currResultType=this.resultType,this.currPreventEval=this.preventEval,this.currSandbox=this.sandbox,n=n||this.callback,this.currOtherTypeCallback=a||this.otherTypeCallback,e=e||this.json,(r=r||this.path)&&"object"===t(r)){if(!r.path)throw new TypeError('You must supply a "path" property when providing an object argument to JSONPath.evaluate().');if(!("json"in r))throw new TypeError('You must supply a "json" property when providing an object argument to JSONPath.evaluate().');e=c.call(r,"json")?r.json:e,s=c.call(r,"flatten")?r.flatten:s,this.currResultType=c.call(r,"resultType")?r.resultType:this.currResultType,this.currSandbox=c.call(r,"sandbox")?r.sandbox:this.currSandbox,p=c.call(r,"wrap")?r.wrap:p,this.currPreventEval=c.call(r,"preventEval")?r.preventEval:this.currPreventEval,n=c.call(r,"callback")?r.callback:n,this.currOtherTypeCallback=c.call(r,"otherTypeCallback")?r.otherTypeCallback:this.currOtherTypeCallback,o=c.call(r,"parent")?r.parent:o,l=c.call(r,"parentProperty")?r.parentProperty:l,r=r.path}if(o=o||null,l=l||null,Array.isArray(r)&&(r=f.toPathString(r)),r&&e&&i.includes(this.currResultType)){this._obj=e;var h=f.toPathArray(r);"$"===h[0]&&h.length>1&&h.shift(),this._hasParentSelector=null;var F=this._trace(h,e,["$"],o,l,n).filter((function(t){return t&&!t.isParentSelector}));return F.length?!p&&this.isSingularResult(F,h)?this._getPreferredOutput(F[0]):F.reduce((function(t,r){var e=u._getPreferredOutput(r);return s&&Array.isArray(e)?t=t.concat(e):t.push(e),t}),[]):p?[]:void 0}},f.prototype._getPreferredOutput=function(t){var r=this.currResultType;switch(r){default:throw new TypeError("Unknown result type");case"all":return t.pointer=f.toPointer(t.path),t.path="string"==typeof t.path?t.path:f.toPathString(t.path),t;case"value":case"parent":case"parentProperty":return t[r];case"path":return f.toPathString(t[r]);case"pointer":return f.toPointer(t.path)}},f.prototype.isFilterExpr=function(t){return 0===t.indexOf("?(")},f.prototype.isSingularResult=function(t,r){var e=this;return 1===t.length&&!r.includes("*")&&!r.includes("..")&&r.every((function(t){return!e.isFilterExpr(t)}))&&r.every((function(t){return!t.includes(",")}))&&r.every((function(t){return!t.includes(":")}))},f.prototype._handleCallback=function(t,r,e){if(r){var n=this._getPreferredOutput(t);t.path="string"==typeof t.path?t.path:f.toPathString(t.path),r(n,e,t)}},f.prototype._trace=function(r,e,n,a,u,o,i){var l,h=this;if(!r.length)return l={path:n,value:e,parent:a,parentProperty:u},this._handleCallback(l,o,"value"),l;var f=r[0],F=r.slice(1),y=[];function v(t){Array.isArray(t)?t.forEach((function(t){y.push(t)})):y.push(t)}if(("string"!=typeof f||i)&&e&&c.call(e,f))v(this._trace(F,e[f],s(n,f),e,f,o));else if("*"===f)this._walk(f,F,e,n,a,u,o,(function(t,r,e,n,a,u,o,i){v(h._trace(p(t,e),n,a,u,o,i,!0))}));else if(".."===f)v(this._trace(F,e,n,a,u,o)),this._walk(f,F,e,n,a,u,o,(function(r,e,n,a,u,o,i,c){"object"===t(a[r])&&v(h._trace(p(e,n),a[r],s(u,r),a,r,c))}));else{if("^"===f)return this._hasParentSelector=!0,n.length?{path:n.slice(0,-1),expr:F,isParentSelector:!0}:[];if("~"===f)return l={path:s(n,f),value:u,parent:a,parentProperty:null},this._handleCallback(l,o,"property"),l;if("$"===f)v(this._trace(F,e,n,null,null,o));else if(/^(\x2D?[0-9]*):(\x2D?[0-9]*):?([0-9]*)$/.test(f))v(this._slice(f,F,e,n,a,u,o));else if(this.isFilterExpr(f)){if(this.currPreventEval)throw new Error("Eval [?(expr)] prevented in JSONPath expression.");this._walk(f,F,e,n,a,u,o,(function(t,r,e,n,a,u,o,i){h._eval(r.replace(/^\?\(((?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])*?)\)$/,"$1"),n[t],t,a,u,o)&&v(h._trace(p(t,e),n,a,u,o,i))}))}else if("("===f[0]){if(this.currPreventEval)throw new Error("Eval [(expr)] prevented in JSONPath expression.");v(this._trace(p(this._eval(f,e,n[n.length-1],n.slice(0,-1),a,u),F),e,n,a,u,o))}else if("@"===f[0]){var b=!1,D=f.slice(1,-2);switch(D){default:throw new TypeError("Unknown value type "+D);case"scalar":e&&["object","function"].includes(t(e))||(b=!0);break;case"boolean":case"string":case"undefined":case"function":t(e)===D&&(b=!0);break;case"number":t(e)===D&&isFinite(e)&&(b=!0);break;case"nonFinite":"number"!=typeof e||isFinite(e)||(b=!0);break;case"object":e&&t(e)===D&&(b=!0);break;case"array":Array.isArray(e)&&(b=!0);break;case"other":b=this.currOtherTypeCallback(e,n,a,u);break;case"integer":e!==Number(e)||!isFinite(e)||e%1||(b=!0);break;case"null":null===e&&(b=!0)}if(b)return l={path:n,value:e,parent:a,parentProperty:u},this._handleCallback(l,o,"value"),l}else if("`"===f[0]&&e&&c.call(e,f.slice(1))){var g=f.slice(1);v(this._trace(F,e[g],s(n,g),e,g,o,!0))}else if(f.includes(",")){var d=f.split(","),_=!0,w=!1,P=void 0;try{for(var x,E=d[Symbol.iterator]();!(_=(x=E.next()).done);_=!0){var S=x.value;v(this._trace(p(S,F),e,n,a,u,o))}}catch(t){w=!0,P=t}finally{try{_||null==E.return||E.return()}finally{if(w)throw P}}}else!i&&e&&c.call(e,f)&&v(this._trace(F,e[f],s(n,f),e,f,o,!0))}if(this._hasParentSelector)for(var j=0;j<y.length;j++){var m=y[j];if(m.isParentSelector){var C=h._trace(m.expr,e,m.path,a,u,o);if(Array.isArray(C)){y[j]=C[0];for(var k=C.length,A=1;A<k;A++)j++,y.splice(j,0,C[A])}else y[j]=C}}return y},f.prototype._walk=function(r,e,n,a,u,o,i,l){if(Array.isArray(n))for(var s=n.length,p=0;p<s;p++)l(p,r,e,n,a,u,o,i);else if("object"===t(n))for(var h in n)c.call(n,h)&&l(h,r,e,n,a,u,o,i)},f.prototype._slice=function(t,r,e,n,a,u,o){if(Array.isArray(e)){var i=e.length,c=t.split(":"),l=c[2]&&parseInt(c[2])||1,s=c[0]&&parseInt(c[0])||0,h=c[1]&&parseInt(c[1])||i;s=s<0?Math.max(0,s+i):Math.min(i,s),h=h<0?Math.max(0,h+i):Math.min(i,h);for(var f=[],F=s;F<h;F+=l){var y=this._trace(p(F,r),e,n,a,u,o);Array.isArray(y)?y.forEach((function(t){f.push(t)})):f.push(y)}return f}},f.prototype._eval=function(t,r,e,n,a,u){if(!this._obj||!r)return!1;t.includes("@parentProperty")&&(this.currSandbox._$_parentProperty=u,t=t.replace(/@parentProperty/g,"_$_parentProperty")),t.includes("@parent")&&(this.currSandbox._$_parent=a,t=t.replace(/@parent/g,"_$_parent")),t.includes("@property")&&(this.currSandbox._$_property=e,t=t.replace(/@property/g,"_$_property")),t.includes("@path")&&(this.currSandbox._$_path=f.toPathString(n.concat([e])),t=t.replace(/@path/g,"_$_path")),t.includes("@root")&&(this.currSandbox._$_root=this.json,t=t.replace(/@root/g,"_$_root")),t.match(/@([\t-\r \)\.\[\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF])/)&&(this.currSandbox._$_v=r,t=t.replace(/@([\t-\r \)\.\[\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF])/g,"_$_v$1"));try{return l.runInNewContext(t,this.currSandbox)}catch(r){throw console.log(r),new Error("jsonPath: "+r.message+": "+t)}},f.cache={},f.toPathString=function(t){for(var r=t,e=r.length,n="$",a=1;a<e;a++)/^(~|\^|@(?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])*?\(\))$/.test(r[a])||(n+=/^[\*0-9]+$/.test(r[a])?"["+r[a]+"]":"['"+r[a]+"']");return n},f.toPointer=function(t){for(var r=t,e=r.length,n="",a=1;a<e;a++)/^(~|\^|@(?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])*?\(\))$/.test(r[a])||(n+="/"+r[a].toString().replace(/~/g,"~0").replace(/\//g,"~1"));return n},f.toPathArray=function(t){var r=f.cache;if(r[t])return r[t].concat();var e=[],n=t.replace(/@(?:null|boolean|number|string|integer|undefined|nonFinite|scalar|array|object|function|other)\(\)/g,";$&;").replace(/['\[](\??\((?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])*?\))['\]]/g,(function(t,r){return"[#"+(e.push(r)-1)+"]"})).replace(/\['((?:[\0-&\(-\\\^-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])*)'\]/g,(function(t,r){return"['"+r.replace(/\./g,"%@%").replace(/~/g,"%%@@%%")+"']"})).replace(/~/g,";~;").replace(/'?\.'?(?!(?:[\0-Z\\-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])*\])|\['?/g,";").replace(/%@%/g,".").replace(/%%@@%%/g,"~").replace(/(?:;)?(\^+)(?:;)?/g,(function(t,r){return";"+r.split("").join(";")+";"})).replace(/;;;|;;/g,";..;").replace(/;$|'?\]|'$/g,"").split(";").map((function(t){var r=t.match(/#([0-9]+)/);return r&&r[1]?e[r[1]]:t}));return r[t]=n,r[t]};export{f as JSONPath};
//# sourceMappingURL=index-es.min.js.map