Passed
Branch feature/phase1 (a6d58a)
by Pieter Epeüs
09:26 queued 43s
created

src/modules/getByKey.js   A

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 20
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 13
mnd 2
bc 2
fnc 1
dl 0
loc 20
rs 10
bpm 2
cpm 3
noi 0
c 0
b 0
f 0
1
export default function getByKey(original, key, defaultValue) {
2
    const keys = key.split('.');
3
4
    let reference = original;
5
6
    while (keys.length > 0) {
7
        const referenceKey = keys.shift();
8
9
        if (
10
            reference === null ||
11
            reference === undefined ||
12
            !Object.prototype.hasOwnProperty.call(reference, referenceKey)
13
        ) {
14
            return defaultValue;
15
        }
16
        reference = reference[referenceKey];
17
    }
18
19
    return reference;
20
}
21