-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathidage.js
41 lines (35 loc) · 1005 Bytes
/
idage.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
const ages = require("./ages.json");
const ids = Object.keys(ages);
const nids = ids.map((e) => parseInt(e));
const minId = nids[0];
const maxId = nids[nids.length - 1];
const getDate = (id) => {
if (id < minId) {
return [-1, new Date(ages[ids[0]])];
} else if (id > maxId) {
return [1, new Date(ages[ids[ids.length - 1]])];
} else {
let lid = nids[0];
for (let i = 0; i < ids.length; i++) {
if (id <= nids[i]) {
// calculate middle date
const uid = nids[i];
const lage = ages[lid];
const uage = ages[uid];
const idratio = (id - lid) / (uid - lid);
const midDate = Math.floor(idratio * (uage - lage) + lage);
return [0, new Date(midDate)];
} else {
lid = nids[i];
}
}
}
};
const getAge = (id) => {
const d = getDate(id);
return [
d[0] < 0 ? "older_than" : d[0] > 0 ? "newer_than" : "aprox",
`${d[1].getUTCMonth() + 1}/${d[1].getUTCFullYear()}`,
];
};
module.exports = getAge;