UNPKG

burdy

Version:

Open Source Headless Platform

33 lines (32 loc) 992 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.flatToHierarchy = exports.flatToObj = void 0; const flatToObj = (flat = []) => { const all = {}; [...flat] .filter((item) => !!item) .forEach((item) => { all[item.id] = Object.assign({}, item); }); return all; }; exports.flatToObj = flatToObj; const flatToHierarchy = (flat) => { const roots = []; // things without parent const all = (0, exports.flatToObj)(flat); Object.keys(all).forEach((id) => { const item = all[id]; if (item.parentId === null || item.parentId === undefined) { roots.push(item); } else if (all[item.parentId]) { const p = all[item.parentId]; if (!(p === null || p === void 0 ? void 0 : p.children)) { p.children = []; } p.children.push(item); } }); return roots; }; exports.flatToHierarchy = flatToHierarchy;