UNPKG

@prostojs/tree

Version:
2 lines (1 loc) 1.85 kB
var ProstoTree=function(e){"use strict";function n(e){return"+ "+e.toString()+" item"+(1===e?"":"s")+""}return e.ProstoTree=class{constructor(e){var n,l,r,i,t;const o=(null==e?void 0:e.label)||"label",d=(null==e?void 0:e.children)||"children",h="number"==typeof(null==e?void 0:e.branchWidth)?null==e?void 0:e.branchWidth:2,s=((null===(n=null==e?void 0:e.chars)||void 0===n?void 0:n.hLine)||"─").repeat(h),c={end:(null===(l=null==e?void 0:e.chars)||void 0===l?void 0:l.end)||"└",middle:(null===(r=null==e?void 0:e.chars)||void 0===r?void 0:r.middle)||"├",vLine:(null===(i=null==e?void 0:e.chars)||void 0===i?void 0:i.vLine)||"│",hLine:s,node:(null===(t=null==e?void 0:e.chars)||void 0===t?void 0:t.node)||"•"};this.options={label:o,children:d,renderLabel:(null==e?void 0:e.renderLabel)||(e=>"string"==typeof e?e:e[o]),chars:c,branchWidth:h}}_render(e,l){const{children:r,renderLabel:i,branchWidth:t,chars:o}=this.options;return function e(d,h="",s=0,c=[]){let u="",v="";for(let n=0;n<s-1;n++){v+=`${c[n]?o.vLine:" "}${" ".repeat(t)}`}const a=[i(d,s)].flat(),$=d[r]||[];c[s]=!!$.length;let L="";for(let n=0;n<=s;n++){L+=`${c[n]?o.vLine:" "}${" ".repeat(t)}`}L=L.slice(0,-t)+" ";for(let n=0;n<a.length;n++){const e=a[n];u+=0===n?`${v}${h}${o.node} ${e}\n`:`${L}${e}\n`}if($.length&&(null==l?void 0:l.level)&&s>=l.level-1)u+=L+n($.length)+"\n",c[s]=!1;else for(let r=0;r<$.length;r++){const i=r===$.length-1;if((null==l?void 0:l.childrenLimit)&&r===l.childrenLimit&&(!l.showLast||!i)){u+=L+n($.length-r)+"\n",c[s]=!1,l.showLast&&(u+=`${e($[$.length-1],`${o.end}${o.hLine}`,s+1,c)}`);break}c[s]=!i,u+=`${e($[r],`${i?o.end:o.middle}${o.hLine}`,s+1,c)}`}return u}(e)}render(e,n){return this._render(e,n)}print(e,n){const l=this.render(e,n);return console.log(l),l}},Object.defineProperty(e,"__esModule",{value:!0}),e}({});