1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 export type Tree<T> = Record<string, TreeNode<T>>;
7 export const TREE_ROOT_ID = '';
9 export interface TreeNode<T = any> {
16 export const createTree = <T>(): Tree<T> => ({});
18 export const getNode = (id: string) => <T>(tree: Tree<T>): TreeNode<T> | undefined => tree[id];
20 export const setNode = <T>(node: TreeNode<T>) => (tree: Tree<T>): Tree<T> => {
21 const [newTree] = [tree]
22 .map(tree => getNode(node.id)(tree) === node
24 : { ...tree, [node.id]: node })
25 .map(addChild(node.parent, node.id));
29 export const getNodeValue = (id: string) => <T>(tree: Tree<T>) => {
30 const node = getNode(id)(tree);
31 return node ? node.value : undefined;
34 export const setNodeValue = (id: string) => <T>(value: T) => (tree: Tree<T>) => {
35 const node = getNode(id)(tree);
37 ? setNode(mapNodeValue(() => value)(node))(tree)
41 export const setNodeValueWith = <T>(mapFn: (value: T) => T) => (id: string) => (tree: Tree<T>) => {
42 const node = getNode(id)(tree);
44 ? setNode(mapNodeValue(mapFn)(node))(tree)
48 export const mapTreeValues = <T, R>(mapFn: (value: T) => R) => (tree: Tree<T>): Tree<R> =>
49 getNodeDescendantsIds('')(tree)
50 .map(id => getNode(id)(tree))
51 .map(mapNodeValue(mapFn))
52 .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
54 export const mapTree = <T, R = T>(mapFn: (node: TreeNode<T>) => TreeNode<R>) => (tree: Tree<T>): Tree<R> =>
55 getNodeDescendantsIds('')(tree)
56 .map(id => getNode(id)(tree))
58 .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
60 export const getNodeAncestors = (id: string) => <T>(tree: Tree<T>) =>
61 mapIdsToNodes(getNodeAncestorsIds(id)(tree))(tree);
64 export const getNodeAncestorsIds = (id: string) => <T>(tree: Tree<T>): string[] => {
65 const node = getNode(id)(tree);
66 return node && node.parent
67 ? [...getNodeAncestorsIds(node.parent)(tree), node.parent]
71 export const getNodeDescendants = (id: string, limit = Infinity) => <T>(tree: Tree<T>) =>
72 mapIdsToNodes(getNodeDescendantsIds(id, limit)(tree))(tree);
74 export const getNodeDescendantsIds = (id: string, limit = Infinity) => <T>(tree: Tree<T>): string[] => {
75 const node = getNode(id)(tree);
76 const children = node ? node.children :
78 ? getRootNodeChildren(tree)
85 .map(id => getNodeDescendantsIds(id, limit - 1)(tree))
86 .reduce((nodes, nodeChildren) => [...nodes, ...nodeChildren], []));
89 export const getNodeChildren = (id: string) => <T>(tree: Tree<T>) =>
90 mapIdsToNodes(getNodeChildrenIds(id)(tree))(tree);
92 export const getNodeChildrenIds = (id: string) => <T>(tree: Tree<T>): string[] =>
93 getNodeDescendantsIds(id, 0)(tree);
95 export const mapIdsToNodes = (ids: string[]) => <T>(tree: Tree<T>) =>
96 ids.map(id => getNode(id)(tree)).filter((node): node is TreeNode<T> => node !== undefined);
98 const mapNodeValue = <T, R>(mapFn: (value: T) => R) => (node: TreeNode<T>): TreeNode<R> =>
99 ({ ...node, value: mapFn(node.value) });
101 const getRootNodeChildren = <T>(tree: Tree<T>) =>
104 .filter(id => getNode(id)(tree)!.parent === TREE_ROOT_ID);
106 const addChild = (parentId: string, childId: string) => <T>(tree: Tree<T>): Tree<T> => {
107 const node = getNode(parentId)(tree);
109 const children = node.children.some(id => id === childId)
111 : [...node.children, childId];
113 const newNode = children === node.children
115 : { ...node, children };
117 return setNode(newNode)(tree);