lighthouse_garden/templates/assets/js/time.js   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 1.75

Size

Lines of Code 67
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 51
dl 0
loc 67
rs 10
c 0
b 0
f 0
mnd 3
bc 3
fnc 4
bpm 0.75
cpm 1.75
noi 3

1 Function

Rating   Name   Duplication   Size   Complexity  
B time.js ➔ timeAgo 0 63 7
1
document.addEventListener("DOMContentLoaded", function() {
2
    (function timeAgo(selector) {
0 ignored issues
show
Unused Code introduced by
The parameter selector is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
3
4
        var templates = {
5
            prefix: "",
6
            suffix: " ago",
7
            seconds: "less than a minute",
8
            minute: "about a minute",
9
            minutes: "%d minutes",
10
            hour: "about an hour",
11
            hours: "about %d hours",
12
            day: "a day",
13
            days: "%d days",
14
            month: "about a month",
15
            months: "%d months",
16
            year: "about a year",
17
            years: "%d years"
18
        };
19
        var template = function(t, n) {
20
            return templates[t] && templates[t].replace(/%d/i, Math.abs(Math.round(n)));
21
        };
22
23
        var timer = function(time) {
24
            if (!time)
25
                return;
0 ignored issues
show
Comprehensibility Best Practice introduced by
Are you sure this return statement is not missing an argument? If this is intended, consider adding an explicit undefined like return undefined;.
Loading history...
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
26
            time = time.replace(/\.\d+/, ""); // remove milliseconds
27
            time = time.replace(/-/, "/").replace(/-/, "/");
28
            time = time.replace(/T/, " ").replace(/Z/, " UTC");
29
            time = time.replace(/([\+\-]\d\d)\:?(\d\d)/, " $1$2"); // -04:00 -> -0400
30
            time = new Date(time * 1000 || time);
31
32
            var now = new Date();
33
            var seconds = ((now.getTime() - time) * .001) >> 0;
34
            var minutes = seconds / 60;
35
            var hours = minutes / 60;
36
            var days = hours / 24;
37
            var years = days / 365;
38
39
            return templates.prefix + (
40
                    seconds < 45 && template('seconds', seconds) ||
41
                    seconds < 90 && template('minute', 1) ||
42
                    minutes < 45 && template('minutes', minutes) ||
43
                    minutes < 90 && template('hour', 1) ||
44
                    hours < 24 && template('hours', hours) ||
45
                    hours < 42 && template('day', 1) ||
46
                    days < 30 && template('days', days) ||
47
                    days < 45 && template('month', 1) ||
48
                    days < 365 && template('months', days / 30) ||
49
                    years < 1.5 && template('year', 1) ||
50
                    template('years', years)
51
                    ) + templates.suffix;
52
        };
53
54
        var elements = document.getElementsByClassName('timeago');
55
        for (var i = 0; i < elements.length; ++i) {
56
            var $this = elements[i];
57
            if (typeof $this === 'object') {
58
                $this.innerHTML = timer($this.getAttribute('data-datetime'));
59
            }
60
        }
61
        // update time every minute
62
        setTimeout(timeAgo, 60000);
63
64
    })();
65
66
67
});