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.5 KB
/
Copy pathindex-es.min.js
File metadata and controls
2 lines (2 loc) · 12.5 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 p(t,r){return(t=t.slice()).push(r),t}function s(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,p=this.flatten,s=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,p=c.call(r,"flatten")?r.flatten:p,this.currResultType=c.call(r,"resultType")?r.resultType:this.currResultType,this.currSandbox=c.call(r,"sandbox")?r.sandbox:this.currSandbox,s=c.call(r,"wrap")?r.wrap:s,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?s||1!==F.length||F[0].hasArrExpr?F.reduce((function(t,r){var e=u._getPreferredOutput(r);return p&&Array.isArray(e)?t=t.concat(e):t.push(e),t}),[]):this._getPreferredOutput(F[0]):s?[]: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._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,l){var h,f=this;if(!r.length)return h={path:n,value:e,parent:a,parentProperty:u,hasArrExpr:i},this._handleCallback(h,o,"value"),h;var F=r[0],y=r.slice(1),v=[];function b(t){Array.isArray(t)?t.forEach((function(t){v.push(t)})):v.push(t)}if(("string"!=typeof F||l)&&e&&c.call(e,F))b(this._trace(y,e[F],p(n,F),e,F,o,i));else if("*"===F)this._walk(F,y,e,n,a,u,o,(function(t,r,e,n,a,u,o,i){b(f._trace(s(t,e),n,a,u,o,i,!0,!0))}));else if(".."===F)b(this._trace(y,e,n,a,u,o,i)),this._walk(F,y,e,n,a,u,o,(function(r,e,n,a,u,o,i,c){"object"===t(a[r])&&b(f._trace(s(e,n),a[r],p(u,r),a,r,c,!0))}));else{if("^"===F)return this._hasParentSelector=!0,n.length?{path:n.slice(0,-1),expr:y,isParentSelector:!0}:[];if("~"===F)return h={path:p(n,F),value:u,parent:a,parentProperty:null},this._handleCallback(h,o,"property"),h;if("$"===F)b(this._trace(y,e,n,null,null,o,i));else if(/^(\x2D?[0-9]*):(\x2D?[0-9]*):?([0-9]*)$/.test(F))b(this._slice(F,y,e,n,a,u,o));else if(0===F.indexOf("?(")){if(this.currPreventEval)throw new Error("Eval [?(expr)] prevented in JSONPath expression.");this._walk(F,y,e,n,a,u,o,(function(t,r,e,n,a,u,o,i){f._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)&&b(f._trace(s(t,e),n,a,u,o,i,!0))}))}else if("("===F[0]){if(this.currPreventEval)throw new Error("Eval [(expr)] prevented in JSONPath expression.");b(this._trace(s(this._eval(F,e,n[n.length-1],n.slice(0,-1),a,u),y),e,n,a,u,o,i))}else if("@"===F[0]){var D=!1,g=F.slice(1,-2);switch(g){default:throw new TypeError("Unknown value type "+g);case"scalar":e&&["object","function"].includes(t(e))||(D=!0);break;case"boolean":case"string":case"undefined":case"function":t(e)===g&&(D=!0);break;case"number":t(e)===g&&isFinite(e)&&(D=!0);break;case"nonFinite":"number"!=typeof e||isFinite(e)||(D=!0);break;case"object":e&&t(e)===g&&(D=!0);break;case"array":Array.isArray(e)&&(D=!0);break;case"other":D=this.currOtherTypeCallback(e,n,a,u);break;case"integer":e!==Number(e)||!isFinite(e)||e%1||(D=!0);break;case"null":null===e&&(D=!0)}if(D)return h={path:n,value:e,parent:a,parentProperty:u},this._handleCallback(h,o,"value"),h}else if("`"===F[0]&&e&&c.call(e,F.slice(1))){var d=F.slice(1);b(this._trace(y,e[d],p(n,d),e,d,o,i,!0))}else if(F.includes(",")){var _=F.split(","),w=!0,P=!1,x=void 0;try{for(var E,S=_[Symbol.iterator]();!(w=(E=S.next()).done);w=!0){var j=E.value;b(this._trace(s(j,y),e,n,a,u,o,!0))}}catch(t){P=!0,x=t}finally{try{w||null==S.return||S.return()}finally{if(P)throw x}}}else!l&&e&&c.call(e,F)&&b(this._trace(y,e[F],p(n,F),e,F,o,i,!0))}if(this._hasParentSelector)for(var m=0;m<v.length;m++){var C=v[m];if(C.isParentSelector){var k=f._trace(C.expr,e,C.path,a,u,o,i);if(Array.isArray(k)){v[m]=k[0];for(var A=k.length,O=1;O<A;O++)m++,v.splice(m,0,k[O])}else v[m]=k}}return v},f.prototype._walk=function(r,e,n,a,u,o,i,l){if(Array.isArray(n))for(var p=n.length,s=0;s<p;s++)l(s,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,p=c[0]&&parseInt(c[0])||0,h=c[1]&&parseInt(c[1])||i;p=p<0?Math.max(0,p+i):Math.min(i,p),h=h<0?Math.max(0,h+i):Math.min(i,h);for(var f=[],F=p;F<h;F+=l){var y=this._trace(s(F,r),e,n,a,u,o,!0);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