3 lines
11 KiB
JavaScript
3 lines
11 KiB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self).JSONPath={})}(this,function(t){"use strict";function e(t){return(e="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})(t)}function r(t){return(r=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function n(t,e){return(n=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function a(t,e,r){return(a=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,r){var a=[null];a.push.apply(a,e);var o=new(Function.bind.apply(t,a));return r&&n(o,r.prototype),o}).apply(null,arguments)}function o(t){var e="function"==typeof Map?new Map:void 0;return(o=function(t){if(null===t||(o=t,-1===Function.toString.call(o).indexOf("[native code]")))return t;var o;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,i)}function i(){return a(t,arguments,r(this).constructor)}return i.prototype=Object.create(t.prototype,{constructor:{value:i,enumerable:!1,writable:!0,configurable:!0}}),n(i,t)})(t)}function i(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 l=eval,c="undefined"!=typeof module&&Boolean(module.exports)&&!("undefined"!=typeof navigator&&"ReactNative"===navigator.product),u=["value","path","pointer","parent","parentProperty","all"],p=Object.prototype.hasOwnProperty,s=c?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 l(a)}};function h(t,e){return(t=t.slice()).push(e),t}function f(t,e){return(e=e.slice()).unshift(t),e}var y=function(t){function e(t){var n;return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e),(n=i(this,r(e).call(this,'JSONPath should not be called with "new" (it prevents return of (unwrapped) scalar values)'))).avoidNew=!0,n.value=t,n.name="NewError",n}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&&n(t,e)}(e,o(Error)),e}();function v(t,r,n,a,o){if(!(this instanceof v))try{return new v(t,r,n,a,o)}catch(t){if(!t.avoidNew)throw t;return t.value}"string"==typeof t&&(o=a,a=n,n=r,r=t,t={}),t=t||{};var i=p.call(t,"json")&&p.call(t,"path");if(this.json=t.json||n,this.path=t.path||r,this.resultType=t.resultType&&t.resultType.toLowerCase()||"value",this.flatten=t.flatten||!1,this.wrap=!p.call(t,"wrap")||t.wrap,this.sandbox=t.sandbox||{},this.preventEval=t.preventEval||!1,this.parent=t.parent||null,this.parentProperty=t.parentProperty||null,this.callback=t.callback||a||null,this.otherTypeCallback=t.otherTypeCallback||o||function(){throw new Error("You must supply an otherTypeCallback callback option with the @other() operator.")},!1!==t.autostart){var l=this.evaluate({path:i?t.path:r,json:i?t.json:n});if(!l||"object"!==e(l))throw new y(l);return l}}v.prototype.evaluate=function(t,r,n,a){var o=this,i=this.parent,l=this.parentProperty,c=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,(t=t||this.path)&&"object"===e(t)){if(!t.path)throw new Error('You must supply a "path" property when providing an object argument to JSONPath.evaluate().');r=p.call(t,"json")?t.json:r,c=p.call(t,"flatten")?t.flatten:c,this.currResultType=p.call(t,"resultType")?t.resultType:this.currResultType,this.currSandbox=p.call(t,"sandbox")?t.sandbox:this.currSandbox,s=p.call(t,"wrap")?t.wrap:s,this.currPreventEval=p.call(t,"preventEval")?t.preventEval:this.currPreventEval,n=p.call(t,"callback")?t.callback:n,this.currOtherTypeCallback=p.call(t,"otherTypeCallback")?t.otherTypeCallback:this.currOtherTypeCallback,i=p.call(t,"parent")?t.parent:i,l=p.call(t,"parentProperty")?t.parentProperty:l,t=t.path}if(i=i||null,l=l||null,Array.isArray(t)&&(t=v.toPathString(t)),t&&r&&u.includes(this.currResultType)){this._obj=r;var h=v.toPathArray(t);"$"===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 c&&Array.isArray(r)?t=t.concat(r):t.push(r),t},[]):this._getPreferredOutput(f[0]):s?[]:void 0}},v.prototype._getPreferredOutput=function(t){var e=this.currResultType;switch(e){default:throw new TypeError("Unknown result type");case"all":return t.pointer=v.toPointer(t.path),t.path="string"==typeof t.path?t.path:v.toPathString(t.path),t;case"value":case"parent":case"parentProperty":return t[e];case"path":return v.toPathString(t[e]);case"pointer":return v.toPointer(t.path)}},v.prototype._handleCallback=function(t,e,r){if(e){var n=this._getPreferredOutput(t);t.path="string"==typeof t.path?t.path:v.toPathString(t.path),e(n,r,t)}},v.prototype._trace=function(t,r,n,a,o,i,l){var c,u=this;if(!t.length)return c={path:n,value:r,parent:a,parentProperty:o},this._handleCallback(c,i,"value"),c;var s=t[0],y=t.slice(1),v=[];function b(t){Array.isArray(t)?t.forEach(function(t){v.push(t)}):v.push(t)}if(("string"!=typeof s||l)&&r&&p.call(r,s))b(this._trace(y,r[s],h(n,s),r,s,i));else if("*"===s)this._walk(s,y,r,n,a,o,i,function(t,e,r,n,a,o,i,l){b(u._trace(f(t,r),n,a,o,i,l,!0))});else if(".."===s)b(this._trace(y,r,n,a,o,i)),this._walk(s,y,r,n,a,o,i,function(t,r,n,a,o,i,l,c){"object"===e(a[t])&&b(u._trace(f(r,n),a[t],h(o,t),a,t,c))});else{if("^"===s)return this._hasParentSelector=!0,n.length?{path:n.slice(0,-1),expr:y,isParentSelector:!0}:[];if("~"===s)return c={path:h(n,s),value:o,parent:a,parentProperty:null},this._handleCallback(c,i,"property"),c;if("$"===s)b(this._trace(y,r,n,null,null,i));else if(/^(-?\d*):(-?\d*):?(\d*)$/.test(s))b(this._slice(s,y,r,n,a,o,i));else if(0===s.indexOf("?(")){if(this.currPreventEval)throw new Error("Eval [?(expr)] prevented in JSONPath expression.");this._walk(s,y,r,n,a,o,i,function(t,e,r,n,a,o,i,l){u._eval(e.replace(/^\?\((.*?)\)$/,"$1"),n[t],t,a,o,i)&&b(u._trace(f(t,r),n,a,o,i,l))})}else if("("===s[0]){if(this.currPreventEval)throw new Error("Eval [(expr)] prevented in JSONPath expression.");b(this._trace(f(this._eval(s,r,n[n.length-1],n.slice(0,-1),a,o),y),r,n,a,o,i))}else if("@"===s[0]){var d=!1,g=s.slice(1,-2);switch(g){default:throw new TypeError("Unknown value type "+g);case"scalar":r&&["object","function"].includes(e(r))||(d=!0);break;case"boolean":case"string":case"undefined":case"function":e(r)===g&&(d=!0);break;case"number":e(r)===g&&isFinite(r)&&(d=!0);break;case"nonFinite":"number"!=typeof r||isFinite(r)||(d=!0);break;case"object":r&&e(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("`"===s[0]&&r&&p.call(r,s.slice(1))){var _=s.slice(1);b(this._trace(y,r[_],h(n,_),r,_,i,!0))}else if(s.includes(",")){var w=s.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(f(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&&p.call(r,s)&&b(this._trace(y,r[s],h(n,s),r,s,i,!0))}if(this._hasParentSelector)for(var O=0;O<v.length;O++){var E=v[O];if(E.isParentSelector){var T=u._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},v.prototype._walk=function(t,r,n,a,o,i,l,c){if(Array.isArray(n))for(var u=n.length,s=0;s<u;s++)c(s,t,r,n,a,o,i,l);else if("object"===e(n))for(var h in n)p.call(n,h)&&c(h,t,r,n,a,o,i,l)},v.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 h=[],y=p;y<s;y+=u){var v=this._trace(f(y,e),r,n,a,o,i);Array.isArray(v)?v.forEach(function(t){h.push(t)}):h.push(v)}return h}},v.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=v.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 s.runInNewContext(t,this.currSandbox)}catch(e){throw console.log(e),new Error("jsonPath: "+e.message+": "+t)}},v.cache={},v.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},v.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},v.toPathArray=function(t){var e=v.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]},t.JSONPath=v,Object.defineProperty(t,"__esModule",{value:!0})});
|
|
//# sourceMappingURL=index-umd.min.js.map
|