UNPKG

json-path

Version:

JSON-Path utility (XPath for JSON) for nodejs and modern browsers.

1 lines 10.8 kB
!function(e){"use strict";var r,n,t=[];if(typeof global==="object"&&global){r=global;n=global.JsonPointer}else if(typeof window!=="undefined"){r=window;n=window.JsonPointer}else{r={}}if(n){t.push(function(){if(r.JsonPointer===f){r.JsonPointer=n;n=e}})}function i(e){if(typeof e!=="string"){throw new TypeError("Invalid type: JSON Pointers are represented as strings.")}if(e.length===0){return[]}if(e[0]!=="/"){throw new ReferenceError("Invalid JSON Pointer syntax. Non-empty pointer must begin with a solidus `/`.")}var r=e.substring(1).split("/"),n=-1,t=r.length;while(++n<t){r[n]=r[n].replace("~1","/").replace("~0","~")}return r}function o(e){if(e&&!Array.isArray(e)){throw new TypeError("Invalid type: path must be an array of segments.")}if(e.length===0){return""}var r=[],n=-1,t=e.length;while(++n<t){r.push(e[n].replace("~","~0").replace("/","~1"))}return"/".concat(r.join("/"))}function s(e){if(typeof e!=="string"){throw new TypeError("Invalid type: JSON Pointers are represented as strings.")}if(e.length===0||e[0]!=="#"){throw new ReferenceError("Invalid JSON Pointer syntax; URI fragment idetifiers must begin with a hash.")}if(e.length===1){return[]}if(e[1]!=="/"){throw new ReferenceError("Invalid JSON Pointer syntax.")}var r=e.substring(2).split("/"),n=-1,t=r.length;while(++n<t){r[n]=decodeURIComponent(r[n]).replace("~1","/").replace("~0","~")}return r}function a(e){if(e&&!Array.isArray(e)){throw new TypeError("Invalid type: path must be an array of segments.")}if(e.length===0){return"#"}var r=[],n=-1,t=e.length;while(++n<t){r.push(encodeURIComponent(e[n].replace("~","~0").replace("/","~1")))}return"#/".concat(r.join("/"))}function u(e,r){var n=r.length,t=0;if(n===0||n>1&&r[0]==="0"){return-1}if(n===1&&r[0]==="-"){return e.length}while(++t<n){if(r[t]<"0"||r[t]>"9"){return-1}}return parseInt(r,10)}function c(e,r){if(typeof e!=="undefined"){var n=e,t=r.length,i=-1,o,s;if(t){while(++i<t&&n){o=r[i];if(Array.isArray(n)){if(isNaN(o)){return}s=u(n,o);if(n.length>s){n=n[s]}else{return}}else{n=n[o]}}return n}else{return e}}}function l(r,n,t,i){if(t.length===0){throw new Error("Cannot set the root object; assign it directly.")}if(typeof r!=="undefined"){var o=r,s=t.length,a=t.length-1,c=-1,l,f,h;if(s){while(++c<s){l=t[c];if(Array.isArray(o)){f=u(o,l);if(o.length>f){if(c===a){h=o[f];o[f]=n;return h}o=o[f]}else if(o.length===f){o.push(n);return e}else{throw new ReferenceError("Not found: ".concat(i(t.slice(0,c+1),true),"."))}}else{if(c===a){h=o[l];o[l]=n;return h}o=o[l];if(typeof o==="undefined"){throw new ReferenceError("Not found: ".concat(i(t.slice(0,c+1),true),"."))}}}if(c===s){return o}}else{return o}}}function f(e){this.encode=e.length>0&&e[0]==="#"?a:o;if(Array.isArray(e)){this.path=e}else{var r=e.length>0&&e[0]==="#"?s:i;this.path=r(e)}}Object.defineProperty(f.prototype,"pointer",{enumerable:true,get:function(){return o(this.path)}});Object.defineProperty(f.prototype,"uriFragmentIdentifier",{enumerable:true,get:function(){return a(this.path)}});f.prototype.get=function(e){return c(e,this.path)};f.prototype.set=function(e,r){return l(e,r,this.path,this.encode)};f.prototype.toString=function(){return this.pointer};f.create=function(e){return new f(e)};f.get=function(e,r){var n=r.length>0&&r[0]==="#"?s:i;return c(e,n(r))};f.set=function(e,r,n){var t=r.length>0&&r[0]==="#"?a:o;var u=r.length>0&&r[0]==="#"?s:i;return l(e,n,u(r),t)};f.decodePointer=i;f.encodePointer=o;f.decodeUriFragmentIdentifier=s;f.encodeUriFragmentIdentifier=a;f.noConflict=function(){if(t){t.forEach(function(e){e()});t=null}return f};if(typeof module!=="undefined"&&module&&typeof exports==="object"&&exports&&module.exports===exports){module.exports=f}else{r.JsonPointer=f}}();!function(e){"use strict";var r,n,t=[];if(typeof global==="object"&&global){r=global;n=global.JsonPath}else if(typeof window!=="undefined"){r=window;n=window.JsonPath}else{r={}}if(n){t.push(function(){if(r.JsonPath===P){r.JsonPath=n;n=null}})}if(e){t.push(function(r){if(r){e=r}})}else if(!e){if(typeof r.JsonPointer!=="undefined"){e=r.JsonPointer;t.push(function(r){if(r){e=r}})}else if(typeof require==="function"){e=require("json-ptr")}else{throw new Error("Missing JsonPointer (https://github.com/flitbit/json-ptr).")}}function i(e,r){e=Array.isArray(e)?e:[e];var n,t;for(n=0;n<e.length;n++){var i=e[n];t=typeof i==="function"?i():i;if(!t){r=r||"Failed contract requirement:".concat(i);throw new Error(typeof r==="function"?r():r)}}}function o(e,r,n){n=Array.isArray(n)?n:[n];var t=r,i,o=e.length,s=n.length;while(++t<o){i=-1;while(++i<s){if(e[t]===n[i]){return t}}}return-1}function s(e,r,n,t){var i=r-1,o=-1,s=t.length;if(n-r<s){throw new Error("Expected `".concat(t,"` beginning at character ",r,"."))}while(++i<n&&++o<s){if(e[i]!==t[o]){throw new Error("Unexpected character at position ".concat(i," expected `",t,"` beginning at position ",r,"."))}}}function a(e,r,n){var t=e[r],i=r,o=e.length,s=[];s.push(r);while(++i<o){if(e[i]===t){s.push(r)}else if(e[i]===n){s.pop();if(s.length===0){break}}}if(s.length){throw new Error("Expected `".concat(e[0],"` to have a matching `",n,"`."))}return i}function u(r,n){var t=n.cursor,i=n.result,s=r.length,a=o(r,t,["]","["]);if(a<t){a=s}var u=e.create(r.substring(t,a));i.push(function(e,r){r=r||[];var n=u.get(e);if(typeof n!=="undefined"){r.push(n)}return r});n.cursor=a-1}function c(e,r,n){s(e,r.cursor,e.length,n)}function l(e,r,n){var t=-1,i=Array.isArray(e)?e:[e],o=r.length,s,a,u;while(++t<o&&i.length){a=-1;u=i.length;s=[];while(++a<u){s=r[t](i[a],s,n)}i=s}return i}function f(e,r,n,t){n=n||[];var i=-1,o,s,a;if(typeof e==="object"&&e!==null){a=l(e,r,t);if(a.length){n=n.concat(a)}if(!Array.isArray(e)){o=Object.keys(e);s=o.length;while(++i<s){n=f(e[o[i]],r,n,t)}}}return n}function h(e,r){var n=r.result,t=[];r.result=t;n.push(function(e,r,n){return f(e,t,r,n)});m(e,r);r.result=n}function p(e,r){r=r||[];if(typeof e==="object"&&e!==null){if(Array.isArray(e)){r=r.concat(e)}else{var n=-1,t=Object.keys(e),i=t.length;while(++n<i){r.push(e[t[n]])}}}return r}function d(r,n,t){var o=-1,s=r.length,a,u={},c,l,f=[];while(++o<s){a=r[o];i([false],"Expressions are not implemented in this version.");switch(a){case"#":{c=r.indexOf(" ",o);if(c<o){c=s}l=r.substring(o,c);if(!u[l]){u[l]=e.create(l)}f.push({kind:"v",ref:u[l]});o=c;break}}}}function g(e,r){var n=e[r.cursor]==="!";if(n){r.cursor++}c(e,r,"{");var t=a(e,r.cursor,"}"),i=e.substring(r.cursor+1,t);r.result.push(d(i,n,r.offset));r.cursor=t}function w(e,r){var n=r.cursor,t=r.result,i=e.length,o=e.indexOf("]",n);if(o<n){o=i}var s=e.substring(n,o);r.result.push(function(e,r,n){var t;if(e){if(s.length===0&&typeof n==="function"){t=n}else if(typeof n==="object"&&n){if(!n[s]&&n.RESOLVER){t=n.RESOLVER(s)}else{t=n[s]}}if(!t){throw new Error("Missing user-supplied function: `".concat(s.length?s:"@","`."))}return t(e,r,n)}return r});r.cursor=o-1}function b(e){var r={result:[],stack:[],cursor:-1,offset:0};m(e,r);return r.result}function y(r,n){var t=n.cursor,i=r.indexOf(")",t);s(r,t,i,"take(");t+=5;var o=r.slice(t,i).split(","),a=-1,u=o.length,c;while(++a<u){c=o[a].split("=");if(c.length===1){c=e.create(c[0]);c={name:c.path[c.path.length-1],ptr:c}}else if(c.length===2){c={name:c[0],ptr:e.create(c[1])}}else{throw new Error("Invalid `take` expression")}t+=o[a].length;o[a]=c}n.result.push(function(e,r){r=r||[];var n={},t=-1,i=o.length;while(++t<i){n[o[t].name]=o[t].ptr.get(e)}r.push(n);return r});n.cursor=i}function v(e,r,n){var t=r;while(e[t]>="0"&&e[t]<="9"){t=t+1}if(t===r){throw new Error("Expected an integer at position ".concat(t,"."))}return t-r}function x(e,r,n,t,i){var o=1,a;s(e,r,n,t);r+=t.length-1;if(e[r+1]==="("){r+=2;a=v(e,r,n);o=parseInt(e.substring(r,r+a),10);r+=a;s(e,r,n,")");++r}i.push({kind:t[0],index:o});return r}function k(e,r){var n=r.cursor-1,t=e.length,i=e.indexOf("]",r.cursor),o=null,a=null,u=false,c=[];if(i<n){i=t}while(++n<i){switch(e[n]){case" ":if(a!==null){c.push({kind:"i",index:parseInt(e.substring(a,n),10)});a=null;u=true}break;case",":{if(a!==null){c.push({kind:"i",index:parseInt(e.substring(a,n),10)});a=null}if(u){u=false}}break;case".":{s(e,n,i,"..");if(a!==null){c.push({kind:"s",index:parseInt(e.substring(a,n),10)});a=null}n++;if(u){u=false}}break;case"0":case"1":case"2":case"3":case"4":case"5":case"6":case"7":case"8":case"9":{if(u){throw new Error("Unexpected numeral at position ".concat(n," expected punctuation."))}if(a===null){a=n}}break;case"l":{if(u){throw new Error("Unexpected numeral at position ".concat(n," expected punctuation."))}n=x(e,n,i,"last",c)}break;case"f":{if(u){throw new Error("Unexpected numeral at position ".concat(n," expected punctuation."))}n=x(e,n,i,"first",c)}break;case"c":{if(u){throw new Error("Unexpected numeral at position ".concat(n," expected punctuation."))}n=x(e,n,i,"count",c);break}}}if(a!==null){c.push({kind:"i",index:parseInt(e.substring(a,n),10)})}r.result.push(function(e,r){r=r||[];if(Array.isArray(e)){var n=-1,t=c.length,i=e.length,o,s;while(++n<t){var a=c[n];switch(a.kind){case"c":r.push(i);break;case"f":{o=-1;while(++o<a.index&&o<i){r.push(e[o])}}break;case"l":{o=i;s=i-a.index;while(--o>=s&&o>0){r.push(e[o])}}break;case"i":case"s":{if(a.index<i){r.push(e[a.index]);if(a.kind==="s"){o=a.index;s=++n<t?c[n].index:i-1;while(++o<=s){r.push(e[o])}}}}}}}return r});r.cursor=i-1}function m(e,r){i([typeof e==="string"],"Selector must be a string.");if(e.length===0){return[]}var n=e.length,t;while(++r.cursor<n){t=e[r.cursor];switch(t){case"/":case"#":{u(e,r)}break;case"[":{r.stack.push(r.cursor)}break;case"]":{if(r.stack.length){r.stack.pop()}else{throw new Error("Unexpected `]` at cursor position ".concat(r.cursor,"."))}}break;case".":{c(e,r,"..");++r.cursor;h(e,r)}break;case"*":{c(e,r,"*]");r.result.push(p)}break;case"{":case"!":{g(e,r)}break;case"0":case"1":case"2":case"3":case"4":case"5":case"6":case"7":case"8":case"9":{k(e,r)}break;case"l":{k(e,r)}break;case"f":{k(e,r)}break;case"c":{k(e,r)}break;case"t":{y(e,r)}break;case"@":{r.cursor+=1;w(e,r)}break;default:{throw new Error("Unexpected character at position ".concat(r.cursor,": ",t,"."))}}}i([!r.stack.length],function(){return"Unexpected end; unclosed scope beginning at cursor position ".concat(r.stack.pop(),".")})}function E(e,r,n){return l(e,r,n)}function P(e){Object.defineProperties(this,{selectors:{value:b(e),enumerable:true},resolve:{value:function(e,r){return l(e,this.selectors,r)},enumerable:true}})}P.parseSelector=b;P.executeSelectors=E;P.create=function(e){return new P(e)};P.resolve=function(e,r,n){var t=b(r);return l(e,t,n)};P.noConflict=function(e){if(t){t.forEach(function(r){r(e)});t=null}return P};if(typeof module!=="undefined"&&module&&typeof exports==="object"&&exports&&module.exports===exports){module.exports=P}else{r.JsonPath=P}}(typeof JsonPointer!=="undefined"?JsonPointer:null);