Passed
Push — develop ( 315b35...61dfc9 )
by Daniel
01:27 queued 11s
created

src/javascript/utils/helpers.js   A

Complexity

Total Complexity 15
Complexity/F 1.5

Size

Lines of Code 54
Function Count 10

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 15
mnd 5
bc 5
fnc 10
bpm 0.5
cpm 1.5
noi 2

8 Functions

Rating   Name   Duplication   Size   Complexity  
A helpers.js ➔ isHtmlElement 0 3 1
A helpers.js ➔ initHelpers 0 4 3
A helpers.js ➔ onReady 0 7 2
A helpers.js ➔ handleKeyUp 0 3 1
A helpers.js ➔ handleKeyToggle 0 7 3
A helpers.js ➔ handleKeyDown 0 3 1
A helpers.js ➔ assigned 0 3 1
A helpers.js ➔ findTextNodes 0 14 3
1
export var isShiftPressed = false;
2
export var isCtrlPressed = false;
3
4
export function isHtmlElement(object) {
5
    return object instanceof HTMLElement;
0 ignored issues
show
Bug introduced by
The variable HTMLElement seems to be never declared. If this is a global, consider adding a /** global: HTMLElement */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
6
}
7
8
export function initHelpers() {
9
    document.addEventListener('keydown', event => handleKeyDown(event));
10
    document.addEventListener('keyup', event => handleKeyUp(event));
11
}
12
13
export function onReady(fn) {
14
    if (document.readyState != 'loading') {
15
        fn();
16
    } else {
17
        document.addEventListener('DOMContentLoaded', fn);
18
    }
19
}
20
21
export function assigned(obj) {
22
    return !(typeof obj === 'undefined' || obj === null);
23
}
24
25
function handleKeyDown(event) {
26
    handleKeyToggle(event, true);
27
}
28
29
function handleKeyUp(event) {
30
    handleKeyToggle(event, false);
31
}
32
33
function handleKeyToggle(event, isPressed) {
34
    if (event.key === 'Shift') {
35
        isShiftPressed = isPressed;
36
    } else if (event.key === 'Control') {
37
        isCtrlPressed = isPressed;
38
    }
39
}
40
41
export function findTextNodes(baseNode) {
42
    if (!assigned(baseNode)) {
43
        baseNode = document.documentElement;
44
    }
45
46
    let walker = document.createTreeWalker(baseNode, NodeFilter.SHOW_TEXT, null, false);
0 ignored issues
show
Bug introduced by
The variable NodeFilter seems to be never declared. If this is a global, consider adding a /** global: NodeFilter */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
47
    let node;
48
    let results = [];
49
    while (node = walker.nextNode()) {
50
        results.push(node);
51
    }
52
53
    return results;
54
}