Total Complexity | 4 |
Complexity/F | 4 |
Lines of Code | 44 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | import { findTreeNode } from './findTreeNode'; |
||
2 | |||
3 | export const moveTreeNode = ( |
||
4 | treeData, |
||
5 | currentIndex, |
||
6 | currentPath, |
||
7 | nextIndex, |
||
8 | nextPath, |
||
9 | childIdentifier = 'children', |
||
10 | rootIdentifier = 'root' |
||
11 | ) => { |
||
12 | const currentParent = findTreeNode( |
||
13 | treeData, |
||
14 | currentPath, |
||
15 | childIdentifier, |
||
16 | rootIdentifier |
||
17 | ); |
||
18 | |||
19 | const nextParent = findTreeNode( |
||
20 | treeData, |
||
21 | nextPath, |
||
22 | childIdentifier, |
||
23 | rootIdentifier |
||
24 | ); |
||
25 | |||
26 | if (!currentParent || !nextParent) { |
||
27 | return treeData; |
||
28 | } |
||
29 | |||
30 | const node = currentParent[childIdentifier].splice( |
||
31 | currentIndex, |
||
32 | 1 |
||
33 | )[0]; |
||
34 | |||
35 | if (!Array.isArray(nextParent[childIdentifier])) { |
||
36 | nextParent[childIdentifier] = []; |
||
37 | } |
||
38 | |||
39 | nextParent[childIdentifier].splice(nextIndex, 0, node); |
||
40 | |||
41 | node.parentId = nextPath[nextPath.length - 1]; |
||
42 | |||
43 | return treeData; |
||
44 | }; |
||
45 |