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