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.

src/Parser/Icls.js   A
last analyzed

Size

Lines of Code 139

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
nc 1
dl 0
loc 139
ccs 56
cts 56
cp 1
rs 10
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A Icls.js ➔ ??? 0 3 1
1
import xmljs from 'xml-js';
2
import Store from '../Model/Store';
3
import debugRenderer from 'debug';
4
import {autoCast, castString} from '../lib/utils';
5
6 1
let debug = debugRenderer('icls:parser:icls');
7
8
9
export default class IclsParser {
10
    constructor () {
11 6
        this._store = new Store();
12
    }
13
14
    _convertToUnderstandable (xml) {
15 6
        return xmljs.xml2js(xml);
16
    }
17
18
    _buildTree (iclsXmlObj) {
19 6
        let rootNode = iclsXmlObj.elements[0];
20 6
        this._readTree(rootNode);
21
    }
22
23
    _readTree (el, ancestors = []) {
24 127
        if (Array.isArray(el)) {
25 45
            for (let element of el) {
26 76
                this._readTree(element, ancestors);
27
            }
28 45
            return;
29
        }
30
31 82
        this._parseSchemeNode(el, ancestors);
32 82
        this._parseOptionNode(el, ancestors);
33 82
        this._parseColorNode(el, ancestors);
34 82
        this._parseAttributeNode(el, ancestors);
35 82
        this._parseChildNodes(el, ancestors);
36
    }
37
38
    _prepareNodeValues (values) {
39 46
        return Object.keys(values).reduce((accumulator, curr) => {
40 48
            accumulator[castString(curr)] = autoCast(values[curr]);
41 48
            return accumulator;
42
        }, {});
43
    }
44
45
46
    _parseSchemeNode (el, ancestors) {
47 82
        if (el.name !== 'scheme' || el.attributes === undefined) {
48 80
            return;
49
        }
50 2
        debug('Parse Scheme node');
51
52 2
        this._setInStore(el.attributes, ancestors);
53
    }
54
55
    _parseOptionNode (el, ancestors) {
56 82
        if (el.name !== 'option' || el.attributes === undefined) {
57 30
            return;
58
        }
59
60 52
        debug('Parse Option %s node', el.attributes.name);
61
62 52
        if (el.attributes.value === undefined && el.elements !== undefined) {
63 8
            this._readTree(el.elements, [
64
                ...ancestors,
65
                el.attributes.name
66
            ]);
67 8
            return;
68
        }
69 44
        let attrs = {};
70 44
        attrs[el.attributes.name] = el.attributes.value;
71 44
        this._setInStore(attrs, ancestors);
72
    }
73
74
    _parseColorNode (el, ancestors) {
75 82
        if (el.name !== 'colors' || el.elements === undefined) {
76 80
            return;
77
        }
78
79 2
        debug('Parse Colors node');
80
81 2
        this._readTree(
82
            el.elements,
83
            [
84
                ...ancestors,
85
                'colors'
86
            ]
87
        );
88
    }
89
90
    _parseAttributeNode (el, ancestors) {
91 82
        if (el.name !== 'attributes' || el.elements === undefined) {
92 80
            return;
93
        }
94 2
        debug('Parse Attributes node');
95
96 2
        this._readTree(el.elements, ancestors);
97
    }
98
99
    _parseChildNodes (el, ancestors) {
100 82
        debug('Parsing childnodes of %s', el.name);
101 82
        if (ancestors.length) {
102 27
            debug('Ancestors: %o', ancestors);
103
        }
104
105 82
        if (el.elements === undefined) {
106 49
            return;
107
        }
108
109 33
        this._readTree(el.elements, ancestors);
110
    }
111
112
    getStore () {
113 6
        return this._store;
114
    }
115
116
    parse (iclsXmlString) {
117 6
        debug('Parsing started');
118 6
        const iclsJsObj = this._convertToUnderstandable(iclsXmlString);
119 6
        this._buildTree(iclsJsObj);
120 6
        debug('Parsing Finished');
121 6
        return this.getStore();
122
    }
123
124
    _setInStore (attrs, ancestors) {
125 46
        attrs = this._prepareNodeValues(attrs);
126 46
        for (let key in attrs) {
127 48
            let storeKey = this._getStoreKey(key, ancestors);
128 48
            this._store.set(storeKey, attrs[key]);
129
        }
130
    }
131
132
    _getStoreKey (key, ancestors) {
133 48
        let path = [
134
            ...ancestors,
135
            key
136
        ];
137 48
        return castString(path.join('.'));
138
    }
139
}
140