-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (131 loc) · 2.88 KB
/
index.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
137
138
139
140
141
142
143
144
145
146
"use strict";
const formatValues = ["short", "long"];
const startUnitValues = ["s", "min", "h", "d", "mon", "y"];
const maxDurations = {
s: 60 * 1000 - 1,
min: 60 * 60 * 1000 - 1,
h: 60 * 60 * 24 * 1000 - 1,
d: 60 * 60 * 24 * 30 * 1000 - 1,
mon: 60 * 60 * 24 * 30 * 12 * 1000 - 1,
y: 60 * 60 * 24 * 30 * 12 * 1000 * 1000 - 1
};
const durationConverstionFactor = {
s: 1000,
min: 1000 * 60,
h: 1000 * 60 * 60,
d: 1000 * 60 * 60 * 24,
mon: 1000 * 60 * 60 * 24 * 30,
y: 1000 * 60 * 60 * 24 * 30 * 120
};
const durationNames = {
s: {
long: {
singular: "second",
plural: "seconds"
},
short: {
singular: "sec",
plural: "sec"
}
},
min: {
long: {
singular: "minute",
plural: "minutes"
},
short: {
singular: "min",
plural: "mins"
}
},
h: {
long: {
singular: "hour",
plural: "hours"
},
short: {
singular: "hr",
plural: "hrs"
}
},
d: {
long: {
singular: "day",
plural: "days"
},
short: {
singular: "dy",
plural: "dys"
}
},
mon: {
long: {
singular: "month",
plural: "months"
},
short: {
singular: "mon",
plural: "months"
}
},
y: {
long: {
singular: "year",
plural: "years"
},
short: {
singular: "yr",
plural: "yrs"
}
}
};
/**
* @param {Date} date Input date that needs to be humanized.
* @param {Object} config Configuration for humanizing.
* @param {string} [config.format=short] - Format of duration.
* @param {string} [config.startUnit=s] - On what unit of time does the duration start.
* @param {Date} now Reference Time against which input date is compared
*/
module.exports = function(
date,
config = { format: "short", startUnit: "s" },
now = new Date()
) {
if (!(date instanceof Date))
throw new TypeError("Input date is not an instance of Date");
if (!(now instanceof Date))
throw new TypeError("Reference date is not an instance of Date");
let { startUnit, format } = config;
if (startUnitValues.indexOf(startUnit) === -1) {
startUnit = "s";
}
if (formatValues.indexOf(format) === -1) {
format = "short";
}
const nowInMs = now.getTime();
const differenceInMs = nowInMs - date.getTime();
let applicableDuration = null;
Object.keys(maxDurations).every((durationKey, index, keysArr) => {
if (differenceInMs < maxDurations[durationKey]) {
applicableDuration =
keysArr.indexOf(durationKey) > keysArr.indexOf(startUnit)
? durationKey
: startUnit;
return false;
}
return true;
});
const durationInUnits =
differenceInMs / durationConverstionFactor[applicableDuration];
if (durationInUnits < 1) {
return `less than ${applicableDuration === "h" ? "an " : "a "} ${
durationNames[applicableDuration][format]["singular"]
} ago`;
}
const durationInUnitsFloor = Math.floor(durationInUnits);
return `${durationInUnitsFloor} ${
durationNames[applicableDuration][format][
durationInUnitsFloor > 1 ? "plural" : "singular"
]
} ago`;
};