UNPKG

@blocklet/ui-react

Version:

Some useful front-end web components that can be used in Blocklets.

55 lines (54 loc) 2.03 kB
const p = (n, r, e = "children") => n.map((t) => Array.isArray(t[e]) ? r({ ...t, [e]: p(t[e], r, e) }) : r(t)), A = (n, r = "children") => { const e = []; return p(n, (t) => e.push(t), r), e; }, C = (n, r = "children") => { let e = 0; return p(n, () => e++, r), e; }, R = (n, r, e = "children") => n.map((t) => ({ ...t })).filter((t) => { const s = t[e]; if (Array.isArray(s)) { const a = R(s, r, e); t[e] = a?.length ? a : void 0; } const l = { filteredChildren: t[e], isLeaf: !s?.length }; return r(t, l); }), P = (n) => /^https?:\/\//.test(n), S = (n) => /^mailto:/i.test(n.trim()), x = (n) => /^[\w-]+:[\w-]+$/.test(n), v = (n) => { if (!n || !n?.startsWith("/")) return !1; const r = (s) => s.endsWith("/") ? s : `${s}/`, e = r(window.location.pathname), t = r(new URL(n, window.location.origin).pathname); return e.startsWith(t); }, W = (n = []) => { const r = n.map((t, s) => ({ path: t, index: s })).filter((t) => v(t.path)); return r?.length ? r.slice(1).reduce((t, s) => t.path.length >= s.path.length ? t : s, r[0]).index : -1; }, k = (n, r = {}) => { const { columns: e = 1, breakInside: t = !1, groupHeight: s = 48, itemHeight: l = 24, childrenKey: a = "items" } = r, m = n.length, w = n.reduce((i, g) => i + s + (g[a]?.length || 0) * l, 0), h = Math.ceil(w / e), u = [[]]; let o = 0, c = 0; const d = h * 0.2, f = (i) => c > h - d && o < e - 1 && c + i > h + d; return n.forEach((i) => { const g = s + (i[a]?.length || 0) * l; t && f(s) && (o++, c = 0, u[o] = []), !t && c > 0 && (m <= e || f(g)) && (o++, c = 0, u[o] = []), u[o].push({ ...i, group: !0 }), c += s, i[a] && i[a].forEach((H) => { t && f(l) && (o++, c = 0, u[o] = []), u[o].push({ ...H, group: !1 }), c += l; }); }), u; }; export { C as countRecursive, R as filterRecursive, A as flatRecursive, x as isIconifyString, S as isMailProtocol, P as isUrl, p as mapRecursive, v as matchPath, W as matchPaths, k as splitNavColumns };