UNPKG

object-traversal

Version:

Flexible and performant utility for traversing javascript objects

3 lines (2 loc) 2.38 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t={traversalType:"depth-first",maxNodes:Number.POSITIVE_INFINITY,cycleHandling:!0,maxDepth:Number.POSITIVE_INFINITY,haltOnTruthy:!1,pathSeparator:"."},e=function(){function t(){this.head=void 0,this.tail=void 0}var e=t.prototype;return e.enqueue=function(t){this.tail?this.tail=this.tail.next={value:t}:this.head=this.tail={value:t}},e.dequeue=function(){var t=this.head.value;return this.head=this.head.next,this.head||(this.tail=this.head),t},e.isEmpty=function(){return!this.head},e.reset=function(){this.head=this.tail=void 0},t}(),i=function(){function t(t){this.queue=void 0,this.queue=t}var e=t.prototype;return e.push=function(t){this.queue.enqueue(t)},e.pop=function(){return this.queue.dequeue()},e.isEmpty=function(){return this.queue.isEmpty()},e.reset=function(){return this.queue.reset()},t}(),a=function(){function t(){this.tail=void 0}var e=t.prototype;return e.push=function(t){this.tail={value:t,prev:this.tail}},e.pop=function(){var t=this.tail;return this.tail=this.tail.prev,t.value},e.isEmpty=function(){return!this.tail},e.reset=function(){this.tail=void 0},t}();exports._Queue=e,exports._QueueToStackAdapter=i,exports._Stack=a,exports.getNodeByPath=function(t,e,i){void 0===i&&(i=".");for(var a=t,r=e.split(i),n=0,s=r[n];a&&s;)a=a[s],s=r[++n];return a},exports.traverse=function(r,n,s){if(!(r instanceof Object))throw new Error("First argument must be an object");var u,o=Object.assign({},t,s);o.disablePathTracking="string"!=typeof o.pathSeparator,u="depth-first"===o.traversalType?new a:new i(new e);var h={visitedNodes:new WeakSet,depth:0};o.disablePathTracking||(h.nodePath=null),u.push({parent:null,key:null,value:r,meta:h}),function(t,r,n){var s;s="depth-first"===n.traversalType?new a:new i(new e);for(var u=n.maxNodes,o=n.cycleHandling,h=n.maxDepth,p=n.haltOnTruthy,d=n.pathSeparator,l=0;!r.isEmpty()&&u>l;){var c=r.pop(),v=c.value,f=c.meta,y=f.visitedNodes,m=v instanceof Object;if(!(o&&m&&y.has(v))){if(t(c)&&p)break;if(l++,m){y.add(v);var b=f.nodePath,T=f.depth+1;if(T>h)continue;s.reset();for(var x=Object.keys(v),N=0;N<x.length;N++){var g=x[N],k={visitedNodes:y,depth:T};n.disablePathTracking||(k.nodePath=b?""+b+d+g:g),s.push({value:v[g],meta:k,key:g,parent:v})}for(;!s.isEmpty();)r.push(s.pop())}}}}(n,u,o)}; //# sourceMappingURL=object-traversal.cjs.production.min.js.map