UNPKG

transfigure-json

Version:

Transfigure JSON. Provides JSON objects a fluent, Array-like interface for data transformations.

3 lines (2 loc) 3.84 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).Transfigurator=e()}(this,(function(){"use strict";function t(t,e){let o=t,n=0;for(;void 0!==e[n]&&(o=o[e[n]],void 0!==o);)n++;return o}function e(t,e,o){let n=t,i=0;for(;void 0!==e[i+1];){const t=e[i];n=void 0===n[t]?n[t]=isNaN(e[i+1])?{}:[]:n[t],i++}return n[e[i]]=o,n}function o(t){return Array.isArray(t)}function n(t){return o(t)||function(t){return"object"==typeof t&&!o(t)&&null!==t}(t)}function i(t,e="."){return t.split(e)}function r(t,e){this.delimiter=e,o(t)?this.path=t:this.path=t.split(this.delimiter)}function s(t,e){this.doc=t,this.delim=e,this.q=[],this.setQueue(new r([],this.delim),Object.keys(this.doc))}function c(t,e={}){if(!n(t))throw new Error("Instantiating TransfigureJSON requires an Object or an Array.");if(!(this instanceof c))return new c(t,e);this.options={delimeter:e.delimeter||".",useConstructor:e.useConstructor||!1},this.doc=t}return r.prototype.toString=function(){return this.path.join(this.delimiter)},r.prototype.toArray=function(){return this.path},r.prototype.join=function(t){return void 0!==t?this.path.join(t):this.toString()},r.prototype.clone=function(){return new r([...this.path],this.delimiter)},r.prototype.slice=function(t,e){return new r(this.path.slice(t,e),this.delimiter)},r.prototype.append=function(t){return this.path.push(t),this},s.prototype.setQueue=function(t,e){return e.forEach((e=>{const o=t.clone().append(e);this.q.push(o)})),this},s.prototype.next=function(){const e=this.q.shift();if(!e)return null;const o=t(this.doc,e.toArray());return n(o)?(this.setQueue(e,Object.keys(o)),this.next()):{path:e,value:o,key:e.slice(-1).toString()}},s.prototype.empty=function(){return 0===this.q.length},c.prototype.get=function(e,o={}){if(!e)return this.doc;const r=o.useConstructor||this.options.useConstructor,s=i(e,this.options.delimeter),u=t(this.doc,s);return r&&n(u)?new c(u,this.options):u},c.prototype.set=function(t,o){const n=i(t,this.options.delimeter);return e(this.doc,n,o),this},c.prototype.fold=function(t,e){const o=new s(this.doc,this.options.delimeter);let n=e;for(;!o.empty();)n=t.call(this,n,o.next());return n},c.prototype.each=function(t){const e=new s(this.doc,this.options.delimeter);for(;!e.empty();)t.call(this,e.next());return this},c.prototype.transform=function(t){const e=new s(this.doc,this.options.delimeter),o=new c(c.getBase(this.doc),this.options);for(;!e.empty();){const n=e.next();o.set(n.path.toString(),t.call(this,n))}return o},c.prototype.prune=function(t){const e=new s(this.doc,this.options.delimeter),o=new c(c.getBase(this.doc),this.options);for(;!e.empty();){const n=e.next();t.call(this,n)&&o.set(n.path.toString(),n.value)}return o},c.prototype.select=function(t){const e=new s(this.doc,this.options.delimeter);for(;!e.empty();){const o=e.next();if(t(o))return o}},c.prototype.smoosh=function(){const t=new s(this.doc,this.options.delimeter),e={};for(;!t.empty();){const{path:o,value:n}=t.next();e[o.toString()]=n}return new c(e,this.options)},c.prototype.toggle=function(){let t=this.doc;return t=o(this.doc)?Object.keys(this.doc).reduce(((t,e)=>(t[e]=this.doc[e],t)),{}):Object.keys(this.doc).map((t=>[t,this.doc[t]]),{}),new c(t,this.options)},c.prototype.toStream=function(){return new s(this.doc,this.options.delimeter)},c.getBase=function(t){return o(t)?[]:{}},c.clone=function(t,o){const n=new s(t,o&&o.delimeter||"."),i=c.getBase(t);for(;!n.empty();){const t=n.next();e(i,t.path.toArray(),t.value)}return i},c.schema=function(t,o={}){const n=new s(t,o.delimeter||".");let i=c.getBase(t);for(;!n.empty();){const{path:t,value:o}=n.next();e(i,t.toArray(),null===(r=o)?"null":typeof r)}var r;return i},c})); //# sourceMappingURL=index.js.map