Total Complexity | 8 |
Complexity/F | 4 |
Lines of Code | 32 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | export const findTreeNode = ( |
||
2 | treeData, |
||
3 | path, |
||
4 | childIdentifier = 'children', |
||
5 | rootIdentifier = 'root' |
||
6 | ) => { |
||
7 | path = [...path]; |
||
8 | const lookingFor = path[path.length - 1]; |
||
9 | let node = treeData[rootIdentifier]; |
||
10 | |||
11 | const firstId = path.shift(); |
||
12 | |||
13 | if (node.id === firstId) { |
||
14 | |||
15 | while (path.length > 0 && node) { |
||
16 | |||
17 | if (node |
||
18 | && node.id !== lookingFor |
||
19 | && node[childIdentifier]) { |
||
20 | |||
21 | const nextId = path.shift(); |
||
22 | node = node[childIdentifier].find(n => n.id === nextId); |
||
23 | } |
||
24 | |||
25 | else { |
||
26 | node = null; |
||
27 | } |
||
28 | } |
||
29 | } |
||
30 | |||
31 | return node; |
||
32 | }; |
||
33 |