jsonpath-plus
Version:
A JS implementation of JSONPath with some additional operators
3 lines (2 loc) • 11.1 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.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 c=eval,u=!("undefined"==typeof module||!module.exports||"undefined"!=typeof navigator&&"ReactNative"===navigator.product),l=["value","path","pointer","parent","parentProperty","all"],s=Object.prototype.hasOwnProperty,p=u?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 c(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}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={});var i=(t=t||{}).hasOwnProperty("json")&&t.hasOwnProperty("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=!t.hasOwnProperty("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 c=this.evaluate({path:i?t.path:r,json:i?t.json:n});if(!c||"object"!==e(c))throw new y(c);return c}}v.prototype.evaluate=function(t,r,n,a){var o=this,i=this.parent,c=this.parentProperty,u=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=t.hasOwnProperty("json")?t.json:r,u=t.hasOwnProperty("flatten")?t.flatten:u,this.currResultType=t.hasOwnProperty("resultType")?t.resultType:this.currResultType,this.currSandbox=t.hasOwnProperty("sandbox")?t.sandbox:this.currSandbox,s=t.hasOwnProperty("wrap")?t.wrap:s,this.currPreventEval=t.hasOwnProperty("preventEval")?t.preventEval:this.currPreventEval,n=t.hasOwnProperty("callback")?t.callback:n,this.currOtherTypeCallback=t.hasOwnProperty("otherTypeCallback")?t.otherTypeCallback:this.currOtherTypeCallback,i=t.hasOwnProperty("parent")?t.parent:i,c=t.hasOwnProperty("parentProperty")?t.parentProperty:c,t=t.path}if(i=i||null,c=c||null,Array.isArray(t)&&(t=v.toPathString(t)),t&&r&&l.includes(this.currResultType)){this._obj=r;var p=v.toPathArray(t);"$"===p[0]&&p.length>1&&p.shift(),this._hasParentSelector=null;var h=this._trace(p,r,["$"],i,c,n).filter(function(t){return t&&!t.isParentSelector});return h.length?1!==h.length||s||Array.isArray(h[0].value)?h.reduce(function(t,e){var r=o._getPreferredOutput(e);return u&&Array.isArray(r)?t=t.concat(r):t.push(r),t},[]):this._getPreferredOutput(h[0]):s?[]:void 0}},v.prototype._getPreferredOutput=function(t){var e=this.currResultType;switch(e){case"all":return 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,c){var u,l=this;if(!t.length)return u={path:n,value:r,parent:a,parentProperty:o},this._handleCallback(u,i,"value"),u;var p=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 p||c)&&r&&s.call(r,p))b(this._trace(y,r[p],h(n,p),r,p,i));else if("*"===p)this._walk(p,y,r,n,a,o,i,function(t,e,r,n,a,o,i,c){b(l._trace(f(t,r),n,a,o,i,c,!0))});else if(".."===p)b(this._trace(y,r,n,a,o,i)),this._walk(p,y,r,n,a,o,i,function(t,r,n,a,o,i,c,u){"object"===e(a[t])&&b(l._trace(f(r,n),a[t],h(o,t),a,t,u))});else{if("^"===p)return this._hasParentSelector=!0,n.length?{path:n.slice(0,-1),expr:y,isParentSelector:!0}:[];if("~"===p)return u={path:h(n,p),value:o,parent:a,parentProperty:null},this._handleCallback(u,i,"property"),u;if("$"===p)b(this._trace(y,r,n,null,null,i));else if(/^(-?[0-9]*):(-?[0-9]*):?([0-9]*)$/.test(p))b(this._slice(p,y,r,n,a,o,i));else if(0===p.indexOf("?(")){if(this.currPreventEval)throw new Error("Eval [?(expr)] prevented in JSONPath expression.");this._walk(p,y,r,n,a,o,i,function(t,e,r,n,a,o,i,c){l._eval(e.replace(/^\?\((.*?)\)$/,"$1"),n[t],t,a,o,i)&&b(l._trace(f(t,r),n,a,o,i,c))})}else if("("===p[0]){if(this.currPreventEval)throw new Error("Eval [(expr)] prevented in JSONPath expression.");b(this._trace(f(this._eval(p,r,n[n.length-1],n.slice(0,-1),a,o),y),r,n,a,o,i))}else if("@"===p[0]){var d=!1,g=p.slice(1,-2);switch(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!==+r||!isFinite(r)||r%1||(d=!0);break;case"null":null===r&&(d=!0)}if(d)return u={path:n,value:r,parent:a,parentProperty:o},this._handleCallback(u,i,"value"),u}else if("`"===p[0]&&r&&s.call(r,p.slice(1))){var P=p.slice(1);b(this._trace(y,r[P],h(n,P),r,P,i,!0))}else if(p.includes(",")){var _=p.split(","),w=!0,O=!1,S=void 0;try{for(var x,k=_[Symbol.iterator]();!(w=(x=k.next()).done);w=!0){var j=x.value;b(this._trace(f(j,y),r,n,a,o,i))}}catch(t){O=!0,S=t}finally{try{w||null==k.return||k.return()}finally{if(O)throw S}}}else!c&&r&&s.call(r,p)&&b(this._trace(y,r[p],h(n,p),r,p,i,!0))}if(this._hasParentSelector)for(var m=0;m<v.length;m++){var E=v[m];if(E.isParentSelector){var T=l._trace(E.expr,r,E.path,a,o,i);if(Array.isArray(T)){v[m]=T[0];for(var $=T.length,A=1;A<$;A++)m++,v.splice(m,0,T[A])}else v[m]=T}}return v},v.prototype._walk=function(t,r,n,a,o,i,c,u){if(Array.isArray(n))for(var l=n.length,p=0;p<l;p++)u(p,t,r,n,a,o,i,c);else if("object"===e(n))for(var h in n)s.call(n,h)&&u(h,t,r,n,a,o,i,c)},v.prototype._slice=function(t,e,r,n,a,o,i){if(Array.isArray(r)){var c=r.length,u=t.split(":"),l=u[2]&&parseInt(u[2],10)||1,s=u[0]&&parseInt(u[0],10)||0,p=u[1]&&parseInt(u[1],10)||c;s=s<0?Math.max(0,s+c):Math.min(c,s),p=p<0?Math.max(0,p+c):Math.min(c,p);for(var h=[],y=s;y<p;y+=l){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(//g,"_$_parentProperty")),t.includes("@parent")&&(this.currSandbox._$_parent=a,t=t.replace(//g,"_$_parent")),t.includes("@property")&&(this.currSandbox._$_property=r,t=t.replace(//g,"_$_property")),t.includes("@path")&&(this.currSandbox._$_path=v.toPathString(n.concat([r])),t=t.replace(//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)}},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(/#([0-9]+)/);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