3 lines
11 KiB
JavaScript
3 lines
11 KiB
JavaScript
function t(e){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})(e)}function e(t){return(e=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function r(t,e){return(r=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function n(t,e,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,e,n){var a=[null];a.push.apply(a,e);var o=new(Function.bind.apply(t,a));return n&&r(o,n.prototype),o}).apply(null,arguments)}function a(t){var o="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!==o){if(o.has(t))return o.get(t);o.set(t,i)}function i(){return n(t,arguments,e(this).constructor)}return i.prototype=Object.create(t.prototype,{constructor:{value:i,enumerable:!1,writable:!0,configurable:!0}}),r(i,t)})(t)}function o(t,e){return!e||"object"!=typeof e&&"function"!=typeof e?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):e}var i=eval,l="undefined"!=typeof module&&Boolean(module.exports)&&!("undefined"!=typeof navigator&&"ReactNative"===navigator.product),c=["value","path","pointer","parent","parentProperty","all"],u=Object.prototype.hasOwnProperty,p=l?require("vm"):{runInNewContext:function(t,e){var r=Object.keys(e),n=[];!function(t,e,r){for(var n=t.length,a=0;a<n;a++)r(t[a])&&e.push(t.splice(a--,1)[0])}(r,n,function(t){return"function"==typeof e[t]});var a=n.reduce(function(t,r){var n=e[r].toString();return/function/.exec(n)||(n="function "+n),"var "+r+"="+n+";"+t},"")+r.reduce(function(t,r){return"var "+r+"="+JSON.stringify(e[r]).replace(/\u2028|\u2029/g,function(t){return"\\u202"+("\u2028"===t?"8":"9")})+";"+t},t);return i(a)}};function s(t,e){return(t=t.slice()).push(e),t}function h(t,e){return(e=e.slice()).unshift(t),e}var f=function(t){function n(t){var r;return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,n),(r=o(this,e(n).call(this,'JSONPath should not be called with "new" (it prevents return of (unwrapped) scalar values)'))).avoidNew=!0,r.value=t,r.name="NewError",r}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&r(t,e)}(n,a(Error)),n}();function y(e,r,n,a,o){if(!(this instanceof y))try{return new y(e,r,n,a,o)}catch(t){if(!t.avoidNew)throw t;return t.value}"string"==typeof e&&(o=a,a=n,n=r,r=e,e={}),e=e||{};var i=u.call(e,"json")&&u.call(e,"path");if(this.json=e.json||n,this.path=e.path||r,this.resultType=e.resultType&&e.resultType.toLowerCase()||"value",this.flatten=e.flatten||!1,this.wrap=!u.call(e,"wrap")||e.wrap,this.sandbox=e.sandbox||{},this.preventEval=e.preventEval||!1,this.parent=e.parent||null,this.parentProperty=e.parentProperty||null,this.callback=e.callback||a||null,this.otherTypeCallback=e.otherTypeCallback||o||function(){throw new Error("You must supply an otherTypeCallback callback option with the @other() operator.")},!1!==e.autostart){var l=this.evaluate({path:i?e.path:r,json:i?e.json:n});if(!l||"object"!==t(l))throw new f(l);return l}}y.prototype.evaluate=function(e,r,n,a){var o=this,i=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,r=r||this.json,(e=e||this.path)&&"object"===t(e)){if(!e.path)throw new Error('You must supply a "path" property when providing an object argument to JSONPath.evaluate().');r=u.call(e,"json")?e.json:r,p=u.call(e,"flatten")?e.flatten:p,this.currResultType=u.call(e,"resultType")?e.resultType:this.currResultType,this.currSandbox=u.call(e,"sandbox")?e.sandbox:this.currSandbox,s=u.call(e,"wrap")?e.wrap:s,this.currPreventEval=u.call(e,"preventEval")?e.preventEval:this.currPreventEval,n=u.call(e,"callback")?e.callback:n,this.currOtherTypeCallback=u.call(e,"otherTypeCallback")?e.otherTypeCallback:this.currOtherTypeCallback,i=u.call(e,"parent")?e.parent:i,l=u.call(e,"parentProperty")?e.parentProperty:l,e=e.path}if(i=i||null,l=l||null,Array.isArray(e)&&(e=y.toPathString(e)),e&&r&&c.includes(this.currResultType)){this._obj=r;var h=y.toPathArray(e);"$"===h[0]&&h.length>1&&h.shift(),this._hasParentSelector=null;var f=this._trace(h,r,["$"],i,l,n).filter(function(t){return t&&!t.isParentSelector});return f.length?1!==f.length||s||Array.isArray(f[0].value)?f.reduce(function(t,e){var r=o._getPreferredOutput(e);return p&&Array.isArray(r)?t=t.concat(r):t.push(r),t},[]):this._getPreferredOutput(f[0]):s?[]:void 0}},y.prototype._getPreferredOutput=function(t){var e=this.currResultType;switch(e){default:throw new TypeError("Unknown result type");case"all":return t.pointer=y.toPointer(t.path),t.path="string"==typeof t.path?t.path:y.toPathString(t.path),t;case"value":case"parent":case"parentProperty":return t[e];case"path":return y.toPathString(t[e]);case"pointer":return y.toPointer(t.path)}},y.prototype._handleCallback=function(t,e,r){if(e){var n=this._getPreferredOutput(t);t.path="string"==typeof t.path?t.path:y.toPathString(t.path),e(n,r,t)}},y.prototype._trace=function(e,r,n,a,o,i,l){var c,p=this;if(!e.length)return c={path:n,value:r,parent:a,parentProperty:o},this._handleCallback(c,i,"value"),c;var f=e[0],y=e.slice(1),v=[];function b(t){Array.isArray(t)?t.forEach(function(t){v.push(t)}):v.push(t)}if(("string"!=typeof f||l)&&r&&u.call(r,f))b(this._trace(y,r[f],s(n,f),r,f,i));else if("*"===f)this._walk(f,y,r,n,a,o,i,function(t,e,r,n,a,o,i,l){b(p._trace(h(t,r),n,a,o,i,l,!0))});else if(".."===f)b(this._trace(y,r,n,a,o,i)),this._walk(f,y,r,n,a,o,i,function(e,r,n,a,o,i,l,c){"object"===t(a[e])&&b(p._trace(h(r,n),a[e],s(o,e),a,e,c))});else{if("^"===f)return this._hasParentSelector=!0,n.length?{path:n.slice(0,-1),expr:y,isParentSelector:!0}:[];if("~"===f)return c={path:s(n,f),value:o,parent:a,parentProperty:null},this._handleCallback(c,i,"property"),c;if("$"===f)b(this._trace(y,r,n,null,null,i));else if(/^(-?\d*):(-?\d*):?(\d*)$/.test(f))b(this._slice(f,y,r,n,a,o,i));else if(0===f.indexOf("?(")){if(this.currPreventEval)throw new Error("Eval [?(expr)] prevented in JSONPath expression.");this._walk(f,y,r,n,a,o,i,function(t,e,r,n,a,o,i,l){p._eval(e.replace(/^\?\((.*?)\)$/,"$1"),n[t],t,a,o,i)&&b(p._trace(h(t,r),n,a,o,i,l))})}else if("("===f[0]){if(this.currPreventEval)throw new Error("Eval [(expr)] prevented in JSONPath expression.");b(this._trace(h(this._eval(f,r,n[n.length-1],n.slice(0,-1),a,o),y),r,n,a,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":r&&["object","function"].includes(t(r))||(d=!0);break;case"boolean":case"string":case"undefined":case"function":t(r)===g&&(d=!0);break;case"number":t(r)===g&&isFinite(r)&&(d=!0);break;case"nonFinite":"number"!=typeof r||isFinite(r)||(d=!0);break;case"object":r&&t(r)===g&&(d=!0);break;case"array":Array.isArray(r)&&(d=!0);break;case"other":d=this.currOtherTypeCallback(r,n,a,o);break;case"integer":r!==Number(r)||!isFinite(r)||r%1||(d=!0);break;case"null":null===r&&(d=!0)}if(d)return c={path:n,value:r,parent:a,parentProperty:o},this._handleCallback(c,i,"value"),c}else if("`"===f[0]&&r&&u.call(r,f.slice(1))){var _=f.slice(1);b(this._trace(y,r[_],s(n,_),r,_,i,!0))}else if(f.includes(",")){var w=f.split(","),P=!0,S=!1,k=void 0;try{for(var x,m=w[Symbol.iterator]();!(P=(x=m.next()).done);P=!0){var j=x.value;b(this._trace(h(j,y),r,n,a,o,i))}}catch(t){S=!0,k=t}finally{try{P||null==m.return||m.return()}finally{if(S)throw k}}}else!l&&r&&u.call(r,f)&&b(this._trace(y,r[f],s(n,f),r,f,i,!0))}if(this._hasParentSelector)for(var O=0;O<v.length;O++){var E=v[O];if(E.isParentSelector){var T=p._trace(E.expr,r,E.path,a,o,i);if(Array.isArray(T)){v[O]=T[0];for(var $=T.length,A=1;A<$;A++)O++,v.splice(O,0,T[A])}else v[O]=T}}return v},y.prototype._walk=function(e,r,n,a,o,i,l,c){if(Array.isArray(n))for(var p=n.length,s=0;s<p;s++)c(s,e,r,n,a,o,i,l);else if("object"===t(n))for(var h in n)u.call(n,h)&&c(h,e,r,n,a,o,i,l)},y.prototype._slice=function(t,e,r,n,a,o,i){if(Array.isArray(r)){var l=r.length,c=t.split(":"),u=c[2]&&parseInt(c[2])||1,p=c[0]&&parseInt(c[0])||0,s=c[1]&&parseInt(c[1])||l;p=p<0?Math.max(0,p+l):Math.min(l,p),s=s<0?Math.max(0,s+l):Math.min(l,s);for(var f=[],y=p;y<s;y+=u){var v=this._trace(h(y,e),r,n,a,o,i);Array.isArray(v)?v.forEach(function(t){f.push(t)}):f.push(v)}return f}},y.prototype._eval=function(t,e,r,n,a,o){if(!this._obj||!e)return!1;t.includes("@parentProperty")&&(this.currSandbox._$_parentProperty=o,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=r,t=t.replace(/@property/g,"_$_property")),t.includes("@path")&&(this.currSandbox._$_path=y.toPathString(n.concat([r])),t=t.replace(/@path/g,"_$_path")),t.match(/@([.\s)[])/)&&(this.currSandbox._$_v=e,t=t.replace(/@([.\s)[])/g,"_$_v$1"));try{return p.runInNewContext(t,this.currSandbox)}catch(e){throw console.log(e),new Error("jsonPath: "+e.message+": "+t)}},y.cache={},y.toPathString=function(t){for(var e=t,r=e.length,n="$",a=1;a<r;a++)/^(~|\^|@.*?\(\))$/.test(e[a])||(n+=/^[0-9*]+$/.test(e[a])?"["+e[a]+"]":"['"+e[a]+"']");return n},y.toPointer=function(t){for(var e=t,r=e.length,n="",a=1;a<r;a++)/^(~|\^|@.*?\(\))$/.test(e[a])||(n+="/"+e[a].toString().replace(/~/g,"~0").replace(/\//g,"~1"));return n},y.toPathArray=function(t){var e=y.cache;if(e[t])return e[t].concat();var r=[],n=t.replace(/@(?:null|boolean|number|string|integer|undefined|nonFinite|scalar|array|object|function|other)\(\)/g,";$&;").replace(/[['](\??\(.*?\))[\]']/g,function(t,e){return"[#"+(r.push(e)-1)+"]"}).replace(/\['([^'\]]*)'\]/g,function(t,e){return"['"+e.replace(/\./g,"%@%").replace(/~/g,"%%@@%%")+"']"}).replace(/~/g,";~;").replace(/'?\.'?(?![^[]*\])|\['?/g,";").replace(/%@%/g,".").replace(/%%@@%%/g,"~").replace(/(?:;)?(\^+)(?:;)?/g,function(t,e){return";"+e.split("").join(";")+";"}).replace(/;;;|;;/g,";..;").replace(/;$|'?\]|'$/g,"").split(";").map(function(t){var e=t.match(/#(\d+)/);return e&&e[1]?r[e[1]]:t});return e[t]=n,e[t]};export{y as JSONPath};
|
|
//# sourceMappingURL=index-es.min.js.map
|