Completed
Branch test (745d7d)
by Jonathan
08:28
created

resources/js/rich_microtime.js   A

Complexity

Total Complexity 15
Complexity/F 5

Size

Lines of Code 65
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 13
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 15
mnd 3
bc 13
fnc 3
bpm 4.3333
cpm 5
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A window.addEventListener(ꞌloadꞌ) 0 62 1
1
if (typeof window.kintRichMicrotimeInitialized === 'undefined') {
2
    window.kintRichMicrotimeInitialized = 1;
3
    window.addEventListener('load', function() {
4
        'use strict';
5
6
        var sums = {};
7
        var microtimes = Array.prototype.slice.call(
8
            document.querySelectorAll('[data-kint-microtime-group]'),
9
            0
10
        );
11
12
        microtimes.forEach(function(el) {
13
            if (!el.querySelector('.kint-microtime-lap')) {
14
                return;
15
            }
16
17
            var group = el.getAttribute('data-kint-microtime-group');
18
            var lap = parseFloat(el.querySelector('.kint-microtime-lap').innerHTML);
19
            var avg = parseFloat(el.querySelector('.kint-microtime-avg').innerHTML);
20
21
            if (typeof sums[group] === 'undefined') {
22
                sums[group] = {};
23
            }
24
            if (typeof sums[group].min === 'undefined' || sums[group].min > lap) {
25
                sums[group].min = lap;
26
            }
27
            if (typeof sums[group].max === 'undefined' || sums[group].max < lap) {
28
                sums[group].max = lap;
29
            }
30
            sums[group].avg = avg;
31
        });
32
33
        microtimes = Array.prototype.slice.call(
34
            document.querySelectorAll('[data-kint-microtime-group]>.kint-microtime-lap'),
35
            0
36
        );
37
38
        microtimes.forEach(function(el) {
39
            var group = el.parentNode.getAttribute('data-kint-microtime-group');
40
            var value = parseFloat(el.innerHTML);
41
            var avg = sums[group].avg;
42
            var max = sums[group].max;
43
            var min = sums[group].min;
44
            var ratio;
45
46
            el.parentNode.querySelector('.kint-microtime-avg').innerHTML = avg;
47
48
            if (value === avg && value === min && value === max) {
49
                return; // Only one result, no need to color
50
            }
51
52
            if (value > avg) {
53
                ratio = (value - avg) / (max - avg);
54
                el.style.background = 'hsl(' + (40 - 40 * ratio) + ', 100%, 65%)';
55
            } else {
56
                if (avg === min) {
57
                    ratio = 0;
58
                } else {
59
                    ratio = (avg - value) / (avg - min);
60
                }
61
                el.style.background = 'hsl(' + (40 + 80 * ratio) + ', 100%, 65%)';
62
            }
63
        });
64
    });
65
}
66