simple-tree-utils
Version:
Simple Tree Utils is the library to convert and manipulate with tree-like structures.
2 lines (1 loc) • 4.18 kB
JavaScript
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u="id",p="parentId",P="children";class o{constructor(e){this.idProp=(e==null?void 0:e.idProp)||u,this.parentIdProp=(e==null?void 0:e.parentIdProp)||p,this.childrenProp=(e==null?void 0:e.childrenProp)||P}list2Tree(e,t=null){return e.filter(r=>r[this.parentIdProp]===t).map(r=>({...r,[this.childrenProp]:this.list2Tree(e,r[this.idProp])}))}tree2List(e){return this._tree2List(o.deepCopy(e))}_tree2List(e,t=null){return o.deepCopy(e).reduce((r,s)=>{const{[this.childrenProp]:i,...h}=s;return[...r,{...h,[this.parentIdProp]:t},...i!=null&&i.length?this._tree2List(i,h[this.idProp]):[]]},[])}get(e,t){return this.find(e,r=>r[this.idProp]===t)}find(e,t){const r=e.find(s=>t(s));return r||e.reduce((s,i)=>s||this.find(i[this.childrenProp]||[],t),null)}forEach(e,t){e==null||e.forEach(r=>{t(r),this.forEach(r[this.childrenProp],t)})}filter(e,t){const r=e.filter(s=>t(s));return e.reduce((s,i)=>[...s,...i[this.childrenProp].length?this.filter(i[this.childrenProp],t):[]],r)}delete(e,t){return this.deleteBy(e,r=>r[this.idProp]===t)[0]||null}deleteBy(e,t){const s=e.map((i,h)=>({index:h,filter:t(i)})).filter(({filter:i})=>i).map(({index:i})=>i).reverse().reduce((i,h)=>[...i,...e.splice(h,1)],[]);return e.reduce((i,h)=>[...i,...this.deleteBy(h[this.childrenProp]??[],t)],s)}add(e,t,r,...s){this._add("push",e,t,r,...s)}addUnshift(e,t,r,...s){this._add("unshift",e,t,r,...s)}_add(e,t,r,s,...i){if(r==null){t[e](s,...i);return}const h=t.findIndex(n=>n[this.idProp]==r);if(h!=-1){t[h][this.childrenProp][e]({[this.childrenProp]:[],...s},...i.map(n=>({[this.childrenProp]:[],...n})));return}t.forEach(n=>this.add(n[this.childrenProp],r,s,...i))}edit(e,t,r){const s=e.findIndex(i=>i[this.idProp]==t);if(s!=-1){e[s]={[this.idProp]:e[s][this.idProp],[this.childrenProp]:[],...r};return}e.forEach(i=>this.edit(i[this.childrenProp],t,r))}getDescendants(e,t){const r=this.get(e,t);return r?this._getDescendants(r):[]}_getDescendants(e){return[...e[this.childrenProp],...e[this.childrenProp].reduce((t,r)=>[...t,...this._getDescendants(r)],[])]}getAncestors(e,t){const r=[];let s=this.getParent(e,t);for(;s;)r.push(s),s=this.getParent(e,s[this.idProp]);return r.reverse()}getPathNodes(e,t){return this.getAncestors(e,t)}getParent(e,t){return this._getParent(e,t)}_getParent(e,t,r=null){return e.find(i=>i[this.idProp]===t)?r:e.reduce((i,h)=>i||this._getParent(h[this.childrenProp]||[],t,h),null)}getChildren(e,t){var r;return((r=this.get(e,t))==null?void 0:r[this.childrenProp])||[]}getNeighbours(e,t){return[this.getParent(e,t),...this.getChildren(e,t)].filter(r=>r)}getSiblings(e,t){var r;return(((r=this.getParent(e,t))==null?void 0:r[this.childrenProp])||[]).filter(s=>s[this.idProp]!==t)}getLeafs(e,t){return this.filter(this.getSubTree(e,t),r=>!r[this.childrenProp].length)}getSubTree(e,t){return this.getChildren(e,t)}getSize(e,t){return this.tree2List(this.getSubTree(e,t)).length+1}getBreath(e,t){return this.getLeafs(e,t).length}getDepth(e,t){return this.getPathNodes(e,t).length}getLevel(e,t){return this.getDepth(e,t)+1}getDegree(e,t){return this.getChildren(e,t).length}getTreeDegree(e){return e.reduce((t,r)=>Math.max(t,r[this.childrenProp].length,this.getTreeDegree(r[this.childrenProp])),0)}getNodesAtLevel(e,t){return this._getNodesAtLevel(e,t)}_getNodesAtLevel(e,t,r=0){return e.reduce((s,i)=>[...s,...t===r?[i]:[],...r<t?this._getNodesAtLevel(i[this.childrenProp],t,r+1):[]],[])}getWidth(e,t){return this.getNodesAtLevel(e,t).length}getHeight(e,t){return this.getHeightNode(this.getSubTree(e,t))}getHeightNode(e,t=0){return e.reduce((r,s)=>Math.max(r,this.getHeightNode(s[this.childrenProp],t+1)),t)}getDistance(e,t,r){const s=[...this.getPathNodes(e,t),this.get(e,t)],i=[...this.getPathNodes(e,r),this.get(e,r)],h=[...s].reverse().find(d=>i.includes(d));if(!h)return-1;const n=s.findIndex(d=>d.id===h.id),l=i.findIndex(d=>d.id===h.id);return s.length-n-1+(i.length-l-1)}computePaths(e,t,r="/",s="path",i="/"){e==null||e.forEach(h=>{h[s]=i,this.computePaths(h[this.childrenProp],t,r,s,`${h.path}${h[t]}${r}`)})}static deepCopy(e){return JSON.parse(JSON.stringify(e))}}exports.TreeUtils=o;