UNPKG

@lukeaus/plain-tree

Version:

A plain tree with a bunch of tree tools

3 lines (2 loc) 6.8 kB
var t=function(t){return t&&"data"in t?t.data:t},r=function(r){return r.map(t)},e=function(t){return Boolean(t&&t.children&&t.children.length)},n=function(){return function(t){for(var r="";r.length<36;)r+=Math.random().toString(36).substring(2,15);return r.slice(0,36)}()},i=function(t){return Array.isArray(t)&&t.length?t[0]:null},o=function(t,r){var e=r.disallowedKeys;return void 0===e&&(e=[]),Object.keys(t).filter(function(t){return!e.includes(t)}).reduce(function(r,e){return r[e]=t[e],r},{})},h=function(t){var r=t.parent,e={data:t.data,children:t.children,id:t.id,parentId:null};return r&&(e.parentId=r.id),e.children=t.children.map(function(t){return h(t)}),e},a=function(t){var r;if(null===t)return[1];var n=[1],i=[t],o=[];do{for(;i.length;){var h=i.pop();e(h)&&o.push.apply(o,h.children)}o.length&&(n[n.length]=o.length),o=(r=[i,o])[0],i=r[1]}while(i.length);return n},u=function(t,r){var n;void 0===r&&(r=null);var i=[t],o=[],h=[[r(t)]];do{for(;i.length;){var a=i.pop();e(a)&&o.push.apply(o,a.children)}o.length&&(h[h.length]=r?o.map(function(t){return r(t)}):o),o=(n=[i,o])[0],i=n[1]}while(i.length);return h},s=function(t){void 0===t&&(t=null),this.root=t,this.root=t};s.prototype._traverse=function(t,r,n){void 0===r&&(r={});for(var i,o=r.some,h=r.every,a=r.returnBoolean,u=r.returnArray,s=[this.root],l=[],p=!1;s.length;){var d=s.shift();if(e(d)&&s[n].apply(s,d.children),o||h){var f=t(d);if(f&&u&&l.push(d),h&&!f||o&&f){p=!0,i=f;break}}else t(d)}if(h){if(a)return!p;if(u)return l}else if(o){if(a)return Boolean(i);if(u)return l}},s.prototype._traverseBreathFirst=function(t,r){return this._traverse(t,r,"push")},s.prototype._traverseDepthFirst=function(t,r){return this._traverse(t,r,"unshift")},s.prototype.traverseBreathFirst=function(t){this._traverseBreathFirst(t)},s.prototype.traverseDepthFirst=function(t){this._traverseDepthFirst(t)},s.prototype.someBreathFirst=function(t){return Boolean(this._traverseBreathFirst(t,{some:!0,returnBoolean:!0}))},s.prototype.someDepthFirst=function(t){return Boolean(this._traverseDepthFirst(t,{some:!0,returnBoolean:!0}))},s.prototype.everyBreathFirst=function(t){return Boolean(this._traverseDepthFirst(t,{every:!0,returnBoolean:!0}))},s.prototype.everyDepthFirst=function(t){return Boolean(this._traverseDepthFirst(t,{every:!0,returnBoolean:!0}))},s.prototype.findOneBreathFirst=function(t){var r=this._traverseBreathFirst(t,{some:!0,returnArray:!0});return i(r)},s.prototype.findOneDepthFirst=function(t){var r=this._traverseDepthFirst(t,{some:!0,returnArray:!0});return i(r)},s.prototype.findAllBreathFirst=function(t){var r=this._traverseBreathFirst(t,{every:!0,returnArray:!0});return Array.isArray(r)?r:[]},s.prototype.findAllDepthFirst=function(t){var r=this._traverseDepthFirst(t,{every:!0,returnArray:!0});return Array.isArray(r)?r:[]},s.prototype.flatMap=function(t){void 0===t&&(t=null);var r=[];return this._traverseBreathFirst(function(e){t&&r.push(t(e))||r.push(e)}),r},s.prototype.flattenData=function(){return this.flatMap(t)},s.prototype.flattenByHeight=function(t){return void 0===t&&(t=null),u(this.root,t)},s.prototype.flattenDataByHeight=function(){return this.flattenByHeight(t)},s.prototype.widthsByHeight=function(){return a(this.root)},s.prototype.nodesAtHeight=function(t){var r,n=this.root?[1]:[],i=[this.root];if(n.length===t)return i;var o=[];do{for(;i.length;){var h=i.pop();e(h)&&o.push.apply(o,h.children)}if(n.length===t)return o;o.length&&(n[n.length]=o.length),o=(r=[i,o])[0],i=r[1]}while(i.length);return[]},s.prototype.countNodes=function(){return this.widthsByHeight().reduce(function(t,r){return t+r},0)},s.prototype.maxWidth=function(){return Math.max.apply(Math,this.widthsByHeight())},s.prototype.height=function(){return this.root?this.root.height():0},s.prototype.toJson=function(){return this.root?this.root.toJson():""};var l=function(t,r){void 0===r&&(r={});var e=r.id,i=r.parent;this.children=[],this.id=void 0!==e?e:n(),this.parent=i||null,this.data=t,this.children=[]};l.prototype.addChild=function(t,r){void 0===r&&(r={});var e=new l(t,{id:r.id,parent:this});return this.children.push(e),e},l.prototype._removeChildren=function(t){var r=[];return this.children=this.children.filter(function(e){return!t(e)||(r.push(e),!1)}),r},l.prototype.removeChildren=function(t){return this._removeChildren(t)},l.prototype.removeChildrenByData=function(t){return this._removeChildren(function(r){return r.data===t})},l.prototype.removeChildrenById=function(t){return this._removeChildren(function(r){return r.id===t})},l.prototype.isLeaf=function(){return null!==this.parent&&!Boolean(this.children.length)},l.prototype.hasChildren=function(){return Boolean(this.children.length)},l.prototype.toJson=function(){var t=h(this);return JSON.stringify(t)},l.prototype.depth=function(){if(this.parent){for(var t=0,r=this;r.parent;)t+=1,r=r.parent;return t}return 0},l.prototype.widthsByHeight=function(){return a(this)},l.prototype.height=function(){return this.widthsByHeight().length-1},l.prototype.flattenByHeight=function(t){return void 0===t&&(t=null),u(this,t)};var p=function(t,r){void 0===r&&(r={});var e=r.idKey;void 0===e&&(e="id");var n=r.parentIdKey;void 0===n&&(n="parentId");var i=r.childrenKey;void 0===i&&(i="children");var o=[],h={};return t.forEach(function(t){var r=t[e],a=t[n];h[r]=h[r]||[],t[i]=h[r],a?(h[a]=h[a]||[],h[a].push(t)):o.push(t)}),o},d=function(t,r,e){void 0===r&&(r=null),void 0===e&&(e={});var n=e.idKey;void 0===n&&(n="id");var i=e.parentIdKey;void 0===i&&(i="parentId");var h=e.childrenKey;void 0===h&&(h="children");var a=o(t,{disallowedKeys:[n,i,h]});return r?r.addChild(a,{id:t[n]}):new l(a,{id:t[n]})},f=function(t,r,e){if(void 0===r&&(r=null),void 0===e&&(e={}),t.length){var n=e.childrenKey;void 0===n&&(n="children"),t.forEach(function(t){var i=d(t,r,e);f(t[n],i,e)})}},c=function(t,r){if(void 0===r&&(r={}),!t.length)return new s;if(t.length>1)throw new Error("Converting an array to tree only accepts an array with 0 or 1 node currently");var e=r.childrenKey;void 0===e&&(e="children");var n=t[0],i=d(n,null,r),o=new s(i);return f(n[e],i,r),o},v=function(t,r){void 0===r&&(r={});var e=function(t,r){void 0===r&&(r={});var e=r.idKey,n=r.parentIdKey;if(e||n){var i=(e?[e]:[]).concat(n?[n]:[]);return t.map(function(t){var r=o(t,{disallowedKeys:i});return e&&(r.id=t[e]),n&&(r.parentId=t[n]),r})}return t}(t,r),n=p(e);if(n.length){if(n.length=1)return c(n);throw new Error("Converting an array to tree only accepts an array with 0 or 1 node currently")}return new s};export{l as Node,s as Tree,t as nodeData,r as nodesData,e as hasChildren,n as generateId,i as firstArrayElement,o as filterObject,p as createTreeArrayFromFlatArray,d as objectToNode,f as createNodes,c as createTreeFromTreeArray,v as createTreeFromFlatArray}; //# sourceMappingURL=index.mjs.map