UNPKG

st-common-core

Version:

小尾巴前端通用核心库

2 lines (1 loc) 870 B
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=(u,e,s,f=!1,n="path")=>{const l=[],r={},h=(t,c)=>{const o={...t,children:[]};r[o[e]]=o,o[s]?P(o,t,c):(f&&(o[n]=[o[e]]),l.push(o),t.handled=!0)},T=(t,c,o)=>{f&&(t[n]=[...c[n],t[e]]),c.children.push(r[t[e]]),o.handled=!0},P=(t,c,o)=>{let d=r[t[s]];if(d)T(t,d,c);else{for(let i=o+1;i<u.length;i++){const v=u[i];if(!v.handled&&v[e]===t[s]){h(v,i),d=r[t[s]],T(t,d,c);return}}l.push(t),c.handled=!0}};for(let t=0;t<u.length;t++){const c=u[t];c.handled||h(c,t)}return{tree:l,map:r}},A=(u,e)=>{const s={},f=typeof e=="function"?n=>e(n):n=>n[e];for(const n of u)s[f(n)]=n;return s},M=(u,e="children")=>{const s=[],f=n=>{for(const l of n){const{[e]:r,...h}=l;s.push(h),r&&Array.isArray(r)&&r.length>0&&f(r)}};return f(u),s};exports.convert2Map=A;exports.convert2Tree=g;exports.flattenTree=M;