UNPKG

jsonuri

Version:

Use URI path to get or set data

2 lines (1 loc) 4.86 kB
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):(r=r||self,e(r.jsonuri={}))}(this,function(r){"use strict";var e=function(){},n=Array.isArray,t=function(r){return"string"==typeof r},i=function(r){return Number.isInteger(r)},o=function(r){return i(r)&&r>=0},u=/\//,a=function(r){return u.test(r)},f=function(r){var e=typeof r;return null!=r&&("object"===e||"function"===e)},l=function(r){return r+""},p=function(r){console.error(r)},s=function(r,e,t){if(!n(r))return void(r[e]=t);if("length"===e){if(!o(t))throw new Error("value: "+t+" is not a natural number");return t>r.length&&(r.length=t),void r.splice(t)}var i=+e;if(!o(i))return void p("key: "+e+" is not a natural number");r.length=Math.max(r.length,i),r.splice(i,1,t)},c=function(r,e){if(n(r)){var t=+e;if(!o(t))return;r.splice(t,1)}else delete r[e]},v=function(r,e,n,t){if(void 0===t&&(t="after"),e<0||e>r.length)throw new Error("the Index Out of Bounds");switch(t){case"before":e-=1;break;case"append":p("TODO")}r.splice(e,0,n)},h=function(r){var e,n,t=null!==(e=r.path)&&void 0!==e?e:"";for(null==r.keys?n=r.path.split("/"):t||(n=r.keys),n=n.filter(Boolean);~n.indexOf("");){i=n.indexOf("");n.splice(i,1)}for(;~n.indexOf(".");){var i=n.indexOf(".");n.splice(i,1)}for(;~n.indexOf("..");)n[i=n.indexOf("..")]=n[i-1]="",delete n[i],delete n[i-1],n.splice(i,1),n.splice(i-1,1);return{keys:n,path:n.join("/")}},d=function(r,e){if(null==r)return r;if(""===(e=l(e)))return r;if(!a(e))return r[e];var n,t=h({path:e}).keys;if(0===t.length)return r;for(var i=t.length,o=0;o<i&&null!=(n=(o?n:r)[t[o]]);++o);return n},y=function(r){return["__proto__","prototype","constructor"].includes(r)},g=function(r,e,n){if(e=l(e),!r||!e)return p("the parameter is illegal");if(!a(e))return s(r,e,n);for(var t=h({path:e}).keys,i=0,o=t.length;i<o;i++){var u=t[i];y(u)||(null==r[u]&&(r[u]={}),i===o-1?s(r,u,n):r=r[u])}return r},b=function(r,e){var n;if(e=l(e),r&&e){if(!a(e))return void c(r,e);var t=d(r,e+"/..");if(t){var i=null!==(n=h({path:e}).keys.pop())&&void 0!==n?n:"";c(t,i)}}},k=function(r,e,n){if(e=l(e),n=l(n),!(r&&e&&n&&t(e)&&t(n)))return void p("the parameter is illegal");var i=d(r,e),o=d(r,n);g(r,n,i),g(r,e,o)},m=function(r,e,t,i){if(e=l(e),!r)return void p("the parameter is illegal");if(!i)throw new Error("direction must be 'before' | 'after' | 'append'");var o=d(r,e+"/..");if(!n(o))throw new Error("insert node must be a Array");var u=+h({path:e}).keys.pop(),a=u;"after"===i?a=u+1:"before"===i&&(a=u),v(o,a,t)},w=Array.prototype,x=function(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];var n=w.concat.apply(w,r).join("/").split("/");return h({keys:n}).path},O=function(r,e){var n=e.split("/"),t=r.split("/"),i=n.slice(0,n.length-1),o=t.slice(0,n.length-1);return!(n.length>t.length)&&i.join("/")===o.join("/")},_=function(r,e,i,o){var u,a,s;if(e=l(e),i=l(i),!(r&&e&&i&&t(e)&&t(i)))return void p("the parameter is illegal");if(e!==i){var v=d(r,i),y=d(r,e),k=d(r,i+"/.."),w=+(null!==(u=h({path:e}).keys.pop())&&void 0!==u?u:""),_=+(null!==(a=h({path:i}).keys.pop())&&void 0!==a?a:"");if(n(k)){if(!o)throw new Error("direction must be 'before' | 'after' | 'append'");return x()===x()?(m(r,i,y,o),void c(k,w+(_>w?0:1))):O(e,i)&&_<+(null!==(s=e.split("/").slice(0,i.split("/").length).pop())&&void 0!==s?s:"")?(b(r,e),void m(r,i,y,o)):(m(r,i,y,o),void b(r,e))}if(!f(v))throw new Error("'"+i+"': "+v+" is primitive values");g(r,i+"/"+e,y),b(r,e)}},j=function(r,e,t,i){if(void 0===i&&(i=1),e=l(e),!(o(i)&&i>0))return void p("the parameter is illegal");if(!r)return void p("the parameter is illegal");var u=d(r,e+"/..");if(!n(u))return void p("must be a Array");var a=u.length,f=+h({path:e}).keys.pop();if(o(f)&&!(f>a-1)){var s=f+t*i;s<=0&&(s=0),s>a-1&&(s=a-1);var y=u[f];c(u,f),v(u,s,y)}},E=function(r,e,n){j(r,e,-1,n)},A=function(r,e,n){j(r,e,1,n)},P=function(r,e){var n=+h({path:r}).keys.pop();return i(n)?"prev"===e?x():"next"===e?x():null:null},B=function(r,e){if(void 0===e&&(e=[]),!f(r))return!1;e.push(r);for(var n in r)if(r.hasOwnProperty(n)){var t=r[n];if(f(t)&&(~e.indexOf(t)||B(t,e.slice())))return!0}return!1},I=function(r,e){for(var n=!1,t=function(){n=!0},i=0,o=Object.keys(r);i<o.length;i++){var u=o[i];if(n)break;e(r[u],u,r,{_break:t})}},M=function(r,t,i){if(void 0===r&&(r={}),void 0===t&&(t=e),void 0===i&&(i=e),B(r))throw new Error("obj is a circular structure");var o=[],u=function(r){return I(r,function(r,e,a,l){var p=l._break,s=!1,c=function(){p(),s=!0,n(a)&&o.pop()};if(o.push(e),t(r,e,a,{path:x(),_break:c}),o.pop(),f(r)){if(o.push(e),s)return;u(r),o.pop(),i(r,e,a,{path:x(),_break:c})}}),r};return u(r)};r._computePath=P,r.down=A,r.get=d,r.insert=m,r.isCircular=B,r.mv=_,r.normalizePath=x,r.rm=b,r.set=g,r.swap=k,r.up=E,r.walk=M,Object.defineProperty(r,"__esModule",{value:!0})});