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.
Passed
Branch feature/parse-icls-file (b20af6)
by Cedric
01:06
created

icls-parser.js ➔ buildTree   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 31
rs 8.8571
1
'use strict';
2
const xmljs = require('xml-js');
3
const Map = require('immutable').Map
0 ignored issues
show
Comprehensibility introduced by
You are shadowing the built-in type Map. This makes code hard to read, consider using a different name.
Loading history...
4
const List = require('immutable').List
5
const fromJS = require('immutable').fromJS
6
7
const castValue = function (val) {
8
    if (!val) {
9
        return val;
10
    }
11
12
    if (parseInt(val, 10) == val) {
13
        return parseInt(val, 10);
14
    }
15
16
    if (parseFloat(val) == val) {
17
        return parseFloat(val);
18
    }
19
20
    if (val.toLowerCase() === 'true') {
21
        return true;
22
    }
23
24
    if (val.toLowerCase() === 'false') {
25
        return false;
26
    }
27
28
    return val;
29
}
30
31
const normalizeKey = function (key) {
32
    if (typeof key === 'string' || key instanceof String) {
33
        return key.toLowerCase();
34
    }
35
36
    return key;
37
}
38
39
const cleanMap = function (map) {
40
    map = map.mapKeys(normalizeKey);
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter map. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
41
    return map.map(castValue);
42
}
43
44
const readTree = function (el, store) {
45
46
    if (List.isList(el)) {
47
48
        for (let element of el) {
49
            store = readTree(element, store);
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter store. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
50
        }
51
        return store;
52
    }
53
54
    if (el.get('name') === 'scheme' && el.get('attributes')) {
55
        let attrs = el.get('attributes');
56
        attrs = cleanMap(attrs);
57
        store = store.merge(attrs);
58
    }
59
60
    if (el.get('name') === 'option' && el.get('attributes')) {
61
        let attrs = new Map();
62
        attrs = attrs.set(el.getIn(['attributes', 'name']), el.getIn(['attributes', 'value']))
63
        attrs = cleanMap(attrs);
64
        store = store.merge(attrs);
65
    }
66
67
    if (el.get('name') === 'colors') {
68
        store = store.set('colors', readTree(el.get('elements'), new Map()))
69
    }
70
71
72
    if (el.get('elements')) {
73
        for (let child of el.get('elements')) {
74
            store = store.merge(readTree(child, store));
75
        }
76
    }
77
78
    return store;
79
}
80
81
82
const buildTree = function (xmlObj) {
83
84
    let store = new Map();
85
    let rootNode = fromJS(xmlObj.elements[0]);
86
87
    return readTree(rootNode, store).toJS()
88
89
90
    /*
91
    for (let el of xmlObj.elements) {
92
        let curElement = {};
93
94
        if (el.attributes && el.attributes.name && el.attributes.value) {
95
            curElement[el.attributes.name.toLowerCase()] = el.attributes.value
96
            rootLevel = _.extend(rootLevel, curElement);
97
            continue;
98
        }
99
100
        if (el.elements) {
101
            curElement.elements = buildTree(el);
102
        }
103
104
        if (_.isObject(curElement)) {
105
            rootLevel = _.extend(rootLevel, curElement);
106
        } else {
107
            rootLevel.push(curElement);
108
        }
109
    }
110
    return rootLevel;
111
*/
112
}
113
114
const convertToUnderstandable = function (xml) {
115
    return xmljs.xml2js(xml);
116
}
117
118
exports.parse = function (xml) {
119
    const jsObj = convertToUnderstandable(xml);
120
    return buildTree(jsObj);
121
}
122