carbon-components-svelte
Version:
Svelte implementation of the Carbon Design System
17 lines (14 loc) • 394 B
TypeScript
type NodeLike = {
id: string | number;
nodes?: NodeLike[];
[key: string]: unknown;
};
/** Create a hierarchical tree from a flat array. */
export function toHierarchy<
T extends NodeLike,
K extends keyof Omit<T, "id" | "nodes">,
>(
flatArray: T[] | readonly T[],
getParentId: (node: T) => T[K] | null,
): (T & { nodes?: (T & { nodes?: T[] })[] })[];
export default toHierarchy;