-
Notifications
You must be signed in to change notification settings - Fork 0
/
itsago.js
136 lines (117 loc) · 3.47 KB
/
itsago.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"use strict";
/* defaultMixin
* description: For when no mixin is specified, return English as default.
* parameters: value, interval
* returns: string
*/
let defaultMixin = function (value, interval) {
let phrases = {
seconds: "just now",
minute: "about a minute ago",
minutes: "%d minutes ago",
hour: "an hour ago",
hours: "%d hours ago",
day: "one day ago",
days: "%d days ago",
month: "one month ago",
months: "%d months ago",
year: "one year ago",
years: "%d years ago"
};
/* Select the string and replace the interval values */
let str = phrases[interval];
str = str.replace(/%d/i, value);
return (str);
};
/* itsago
* description: calculate the time since an element or time value
* parameters: el, datetime, mixin, sticky
* returns: string
*/
let itsago = function(el, datetime, mixin, sticky) {
if (!mixin) {
/* Mixin wasn't given */
mixin = defaultMixin;
} else if (typeof mixin !== "function") {
/* Mixin wasn't a function */
let warnObject = {
target: el
};
console.warn ("[itsago] mixin passed is not a function. Type:", typeof mixin);
console.warn (warnObject);
mixin = defaultMixin;
}
let intervals = {
seconds: "seconds",
minute: "minute",
minutes: "minutes",
hour: "hour",
hours: "hours",
day: "day",
days: "days",
month: "month",
months: "months",
year: "year",
years: "years"
};
let time = new Date();
if (!datetime) {
/* Datetime parameter was not set */
if (el) {
/* Look for datetime value in element */
let elementTime = el.getAttribute("datetime");
if (elementTime) {
/* No datetime value found */
time = new Date(elementTime);
}
}
} else {
/* Use datetime parameter */
time = new Date(datetime);
}
/* Get declared time in seconds since Jan 1, 1970 */
let declaredTime = time.getTime();
/* Get current datetime in seconds since Jan 1, 1970*/
let now = new Date();
/* Difference in seconds */
let timeDifference = now - parseInt(declaredTime);
let seconds = Math.floor(timeDifference / 1000);
/* Calculate amounts of time */
let amounts = {
year: seconds / 31536000,
month: seconds / 2592000,
day: seconds / 86400,
hour: seconds / 3600,
minute: seconds / 60
};
let timeSince = intervals.seconds;
let value = 1;
/* Loop through amounts of time */
for (let i in amounts) {
value = Math.floor(amounts[i]);
if (value > 1) {
let interval = i;
timeSince = intervals[i + "s"];
break;
} else if (value === 1) {
let interval = i;
timeSince = intervals[i];
break;
}
}
/* Process the result through the mixin */
timeSince = mixin(value, timeSince);
/* Remove trailing spaces */
let result = timeSince.trim();
if (el) {
/* Update the element */
el.innerText = result;
if (sticky) {
el.setAttribute("datetime", time.toISOString());
}
return result;
} else {
/* Just return the time string */
return result;
}
};