UNPKG

@sagold/json-pointer

Version:

json pointer - failsafe data retrieval from js and json objects

1 lines 2.42 kB
var jsonPointer=(function(e){Object.defineProperty(e,`__esModule`,{value:!0});function t(e){return e===`#`||e===``||Array.isArray(e)&&e.length===0||!1}let n=/~1/g,r=/~0/g,i=/(^#?\/?)/g;function a(e){return e.replace(n,`/`).replace(r,`~`)}function o(e){return a(decodeURIComponent(e))}function s(e){if(e==null||typeof e!=`string`||t(e))return Array.isArray(e)?e.slice():[];let n=e.indexOf(`#`)>=0?o:a;e=e.replace(i,``);let r=e.split(`/`);for(let e=0,t=r.length;e<t;e+=1)r[e]=n(r[e]);return r}function c(e,n,r=void 0){if(n==null||e==null)return r;if(t(n))return e;let i=l(e,s(n));return i===void 0?r:i}function l(e,t){let n=t.shift();if(e!==void 0)return n===void 0?e:e==null?void 0:l(e[n],t)}let u=/^\[.*\]$/,d=/^[[{](.+)[\]}]$/;function f(e,t){return e===`__proto__`||e==`constructor`&&t.length>0&&t[0]==`prototype`}function p(e,t,n){if(t==null)return e;let r=s(t);if(r.length===0)return e;e??=u.test(r[0])?[]:{};let i,a,o=e;for(;r.length>1;)i=r.shift(),a=u.test(r[0])||`${parseInt(r[0])}`===r[0],!f(i,r)&&(o=h(o,i,a));return i=r.pop(),m(o,i,n),e}function m(e,t,n){let r=t.match(d)?.pop()??t;t===`[]`&&Array.isArray(e)?e.push(n):f(r,e)||(e[r]=n)}function h(e,t,n){let r=t.match(d)?.pop()??t;if(e[r]!=null)return e[r];let i=n?[]:{};return m(e,t,i),i}function g(e){let t=0,n=0;for(;t+n<e.length;)e[t+n]===void 0&&(n+=1),e[t]=e[t+n],t+=1;return e.length-=n,e}function _(e,t,n){let r=s(t),i=r.pop(),a=c(e,r);return a&&delete a[i],Array.isArray(a)&&n!==!0&&g(a),e}let v=/~/g,y=/\//g;function b(e,t){if(e.length===0)return t?`#`:``;for(let n=0,r=e.length;n<r;n+=1)e[n]=e[n].replace(v,`~0`).replace(y,`~1`),t&&(e[n]=encodeURIComponent(e[n]));return(t?`#/`:`/`)+e.join(`/`)}function x(e,...t){let n=[];if(Array.isArray(e))return b(e,arguments[1]===!0);let r=arguments[arguments.length-1],i=typeof r==`boolean`?r:e&&e[0]===`#`;for(let e=0,t=arguments.length;e<t;e+=1)n.push.apply(n,s(arguments[e]));let a=[];for(let e=0,t=n.length;e<t;e+=1)if(n[e]===`..`){if(a.length===0)return i?`#`:``;a.pop()}else a.push(n[e]);return b(a,i)}function S(e){let t=s(e);if(t.length===0)return typeof e==`string`&&e[0]===`#`?[`#`,t[0]]:[``,void 0];if(t.length===1)return e[0]===`#`?[`#`,t[0]]:[``,t[0]];let n=t.pop();return[x(t,e[0]===`#`),n]}return e.default={get:c,set:p,remove:_,join:x,split:s,splitLast:S,isRoot:t,removeUndefinedItems:g},e.get=c,e.isRoot=t,e.join=x,e.remove=_,e.removeUndefinedItems=g,e.set=p,e.split=s,e.splitLast=S,e})({});