Passed
Pull Request — master (#50)
by Pieter Epeüs
09:24
created

objects.js ➔ originalHas   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
1
/**
2
 * Object helper
3
 */
4
module.exports = class Obj {
5
    /**
6
     * Set the original and prefix.
7
     *
8
     * @param {object} original
9
     * @param {string} prefix
10
     *
11
     * @return {object}
12
     */
13
    constructor(original, prefix) {
14
        this.original = original;
15
        this.prefix = prefix;
16
        this.flatObject = {};
17
        this.parse();
18
19
        return this;
0 ignored issues
show
Bug introduced by
The constructor does not have a meaningful return value. Are you sure this is correct?
Loading history...
20
    }
21
22
    /**
23
     * flatten the object 1 level per time.
24
     */
25
    parse() {
26
        Object.entries(this.original).forEach(
27
            ([originalRowIndex, originalRow]) => {
28
                let index = originalRowIndex;
29
30
                if (this.prefix) {
31
                    index = [this.prefix, originalRowIndex].join('.');
32
                }
33
34
                if (typeof originalRow === 'object') {
35
                    const childRows = new Obj(originalRow, index).flat;
36
37
                    this.flatObject = Object.assign(this.flatObject, childRows);
38
39
                    return;
40
                }
41
42
                this.flatObject[index] = originalRow;
43
            }
44
        );
45
    }
46
47
    /**
48
     * Get the flat object.
49
     *
50
     * @return {object}
51
     */
52
    get flat() {
53
        return this.flatObject;
54
    }
55
56
    /**
57
     * Get the object entries.
58
     *
59
     * @return {array}
60
     */
61
    entries() {
62
        return Object.entries(this.flatObject);
63
    }
64
65
    /**
66
     * Get the object keys.
67
     *
68
     * @return {array}
69
     */
70
    keys() {
71
        return Object.keys(this.flatObject);
72
    }
73
74
    /**
75
     * Get the object values.
76
     *
77
     * @return {array}
78
     */
79
    values() {
80
        return Object.values(this.flatObject);
81
    }
82
83
    /**
84
     * Get the object length.
85
     *
86
     * @return {number}
87
     */
88
    get length() {
89
        return Object.keys(this.flatObject).length;
90
    }
91
92
    /**
93
     * Get an item by key.
94
     *
95
     * @param {string} key
96
     * @param {object|null} defaultValue
97
     *
98
     * @return {object|null}
99
     */
100
    getByKey(key, defaultValue) {
101
        if (this.originalHas(key)) {
102
            return this.original[key];
103
        }
104
105
        if (this.has(key)) {
106
            return this.flatObject[key];
107
        }
108
109
        if (this.includes(key)) {
110
            return this.entries()
111
                .filter(([currentKey]) => currentKey.startsWith(key))
112
                .reduce((accumulator, [currentKey, currentValue]) => {
113
                    const subKey = currentKey.substring(key.length + 1);
114
                    accumulator[subKey] = currentValue;
115
                    return accumulator;
116
                }, {});
117
        }
118
119
        return defaultValue;
120
    }
121
122
    /**
123
     * Get keys of an item.
124
     *
125
     * @param {array} keys
126
     * @param {object|null} defaultValue
127
     *
128
     * @return {object|null}
129
     */
130
    getFlatKeys(keys, defaultValue) {
131
        const result = this.entries().filter(([currentKey]) =>
132
            keys.some(key => currentKey.startsWith(key))
133
        );
134
135
        if (result.length < 1) {
136
            return defaultValue;
137
        }
138
139
        return Object.fromEntries(result);
140
    }
141
142
    /**
143
     * Get keys of an item.
144
     *
145
     * @param {array} keys
146
     * @param {object|null} defaultValue
147
     *
148
     * @return {object|null}
149
     */
150
    getKeys(keys, defaultValue) {
151
        const result = keys.reduce((accumulator, currentKey) => {
152
            const value = this.getByKey(currentKey);
153
154
            if (value) {
155
                accumulator[currentKey] = value;
156
            }
157
158
            return accumulator;
159
        }, {});
160
161
        if (Object.keys(result).length < 1) {
162
            return defaultValue;
163
        }
164
165
        return result;
166
    }
167
168
    /**
169
     * Check if the original object has a key.
170
     *
171
     * @param {string} key
172
     *
173
     * @return {boolean}
174
     */
175
    originalHas(key) {
176
        return Object.prototype.hasOwnProperty.call(this.original, key);
177
    }
178
179
    /**
180
     * Check if the object has a key.
181
     *
182
     * @param {string} key
183
     *
184
     * @return {boolean}
185
     */
186
    has(key) {
187
        return Object.prototype.hasOwnProperty.call(this.flatObject, key);
188
    }
189
190
    /**
191
     * Check if the object has a key that includes.
192
     *
193
     * @param {string} key
194
     *
195
     * @return {boolean}
196
     */
197
    includes(key) {
198
        return this.keys().filter(item => item.startsWith(key)).length > 0;
199
    }
200
};
201