st-common-core
Version:
小尾巴前端通用核心库
48 lines (47 loc) • 1.19 kB
JavaScript
const v = (f, c, o, u = !1, n = "path") => {
const h = [], r = {}, l = (t, s) => {
const e = { ...t, children: [] };
r[e[c]] = e, e[o] ? W(e, t, s) : (u && (e[n] = [e[c]]), h.push(e), t.handled = !0);
}, P = (t, s, e) => {
u && (t[n] = [...s[n], t[c]]), s.children.push(r[t[c]]), e.handled = !0;
}, W = (t, s, e) => {
let d = r[t[o]];
if (d) P(t, d, s);
else {
for (let i = e + 1; i < f.length; i++) {
const A = f[i];
if (!A.handled && A[c] === t[o]) {
l(A, i), d = r[t[o]], P(t, d, s);
return;
}
}
h.push(t), s.handled = !0;
}
};
for (let t = 0; t < f.length; t++) {
const s = f[t];
s.handled || l(s, t);
}
return {
tree: h,
map: r
};
}, H = (f, c) => {
const o = {}, u = typeof c == "function" ? (n) => c(n) : (n) => n[c];
for (const n of f)
o[u(n)] = n;
return o;
}, T = (f, c = "children") => {
const o = [], u = (n) => {
for (const h of n) {
const { [c]: r, ...l } = h;
o.push(l), r && Array.isArray(r) && r.length > 0 && u(r);
}
};
return u(f), o;
};
export {
H as convert2Map,
v as convert2Tree,
T as flattenTree
};