-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathutils.ts
More file actions
30 lines (30 loc) · 840 Bytes
/
utils.ts
File metadata and controls
30 lines (30 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
* Copyright (c) 2022, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
type RecursiveList<K extends string, T extends RecursiveList<K, T>> = {id: string} & Partial<
Record<K, T[]>
>
/**
* Flattens a tree data structure
* @param {*} node
* @param key
* @returns
*/
export const flatten = <K extends string, T extends RecursiveList<K, T>>(
node: T,
key: K
): Record<string, T> => {
const base = {[node.id]: node}
const children = node[key]
if (!Array.isArray(children) || children.length === 0) return base
return children.reduce(
(result, child) => ({
...result,
...flatten(child, key)
}),
base
)
}