GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#57)
by Benjamin
01:31
created

treeToFlatList.js ➔ ???   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
c 0
b 0
f 0
nc 12
dl 0
loc 63
rs 7.2689
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import { List, Map, fromJS } from 'immutable';
2
3
export const treeToFlatList = (
4
    data,
5
    rootIdentifier = 'root',
6
    childIdentifier = 'children'
7
) => {
8
9
    if (!data) {
10
        throw new Error('Expected data to be defined');
11
    }
12
13
    const result = [];
14
15
    let stack = List();
16
17
    const cfg = { flatIndex: 0 };
18
19
    if (!Map.isMap(data)) {
20
        data = fromJS(data);
21
    }
22
23
    if (data.get(rootIdentifier)) {
24
        data = data.get(rootIdentifier);
25
26
        stack = stack.push(
27
            toItem(List(), childIdentifier, cfg)(data)
28
        );
29
    }
30
    else {
31
        stack = data.get(childIdentifier).map(
32
            toItem(List([-1]), List([0]), childIdentifier)
33
        );
34
    }
35
36
    while (stack.count()) {
37
38
        const item = stack.first();
39
40
        stack = stack.shift();
41
        const children = item.get(childIdentifier);
42
        // console.log(item.get('id'), (children || List()).map(i => i.get('id')).toJS(), stack.map(i => i.get('id')).toJS());
43
44
        if (List.isList(children) && !item.get('_hideChildren')) {
45
            stack = children.map(
46
                toItem(
47
                    item.get('_path').push(item.get('_id')),
48
                    childIdentifier,
49
                    cfg,
50
                    item,
51
                    children
52
                )
53
            ).concat(stack);
54
        }
55
56
        // removing erroneous data since grid uses internal values
57
        result.push(
58
            item.delete(childIdentifier)
59
                .delete('parentId')
60
                .delete('id')
61
        );
62
    }
63
64
    return List(result);
65
};
66
67
const toItem = (
0 ignored issues
show
Unused Code introduced by
The constant toItem seems to be never used. Consider removing it.
Loading history...
68
    path, childIdentifier, cfg, parent, siblings = List()
69
) => (node, index = 0) => {
70
71
    const previousSibling = index - 1 > -1
72
        ? siblings.get(index - 1)
73
        : undefined;
74
75
    const previousSiblingTotalChilden = (
76
        previousSibling && previousSibling.get(childIdentifier)
77
            ? previousSibling.get(childIdentifier).count()
78
            : 0
79
    );
80
81
    return node.merge({
82
        _id: node.get('id'),
83
        _parentId: node.get('parentId', 'root'),
84
        _parentIndex: parent ? parent.get('_index') : 0,
85
        _depth: path.count(),
86
        _hideChildren: node.get('_hideChildren'),
87
        _hasChildren: (
88
            node.get(childIdentifier) && node.get(childIdentifier).count() > 0
89
        ),
90
        _index: index,
91
        _flatIndex: cfg.flatIndex++,
92
        _isFirstChild: index === 0,
93
        _isLastChild: index === siblings.count() - 1,
94
        _previousSiblingId: previousSibling
95
                                ? previousSibling.get('id')
96
                                : undefined,
97
        _previousSiblingTotalChilden: previousSiblingTotalChilden,
98
        _key: `tree-item-${node.get('id')}`,
99
        _isExpanded: (
100
            node.get(childIdentifier)
101
                && node.get(childIdentifier).count() > 0
102
                && !node.get('_hideChildren')
103
        ),
104
        _leaf: !(
105
            (node.get(childIdentifier) && node.get(childIdentifier).count() > 0)
106
            || (node.get('leaf') !== undefined && node.get('leaf') === false)
107
        ),
108
        _path: path
109
    });
110
111
};
112