mirror of https://github.com/sveltejs/svelte
site: fix type errors in JS files (#9354)
parent
ac7505d81b
commit
d8e382712c
@ -1,30 +1,28 @@
|
|||||||
// adapted from https://github.com/digplan/time-ago
|
const formatter = new Intl.RelativeTimeFormat(undefined, {
|
||||||
// https://github.com/digplan/time-ago/blob/master/license.txt
|
numeric: 'auto'
|
||||||
const o = {
|
});
|
||||||
second: 1000,
|
|
||||||
minute: 60 * 1000,
|
const DIVISIONS = {
|
||||||
hour: 60 * 1000 * 60,
|
seconds: 60,
|
||||||
day: 24 * 60 * 1000 * 60,
|
minutes: 60,
|
||||||
week: 7 * 24 * 60 * 1000 * 60,
|
hours: 24,
|
||||||
month: 30 * 24 * 60 * 1000 * 60,
|
days: 7,
|
||||||
year: 365 * 24 * 60 * 1000 * 60
|
weeks: 4.34524,
|
||||||
|
months: 12,
|
||||||
|
years: Number.POSITIVE_INFINITY
|
||||||
};
|
};
|
||||||
|
|
||||||
export const ago = (nd, s) => {
|
/**
|
||||||
var r = Math.round,
|
* @param {Date} date
|
||||||
dir = ' ago',
|
*/
|
||||||
pl = function (v, n) {
|
export const ago = (date) => {
|
||||||
return s === undefined ? n + ' ' + v + (n > 1 ? 's' : '') + dir : n + v.substring(0, 1);
|
let duration = (date.getTime() - new Date().getTime()) / 1000;
|
||||||
},
|
|
||||||
ts = Date.now() - new Date(nd).getTime(),
|
for (const [name, amount] of Object.entries(DIVISIONS)) {
|
||||||
ii;
|
if (Math.abs(duration) < amount) {
|
||||||
if (ts < 0) {
|
const format = /** @type {keyof(DIVISIONS)} */ (name);
|
||||||
ts *= -1;
|
return formatter.format(Math.round(duration), format);
|
||||||
dir = ' from now';
|
}
|
||||||
}
|
duration /= amount;
|
||||||
for (var i in o) {
|
|
||||||
if (r(ts) < o[i]) return pl(ii || 'm', r(ts / (o[ii] || 1)));
|
|
||||||
ii = i;
|
|
||||||
}
|
}
|
||||||
return pl(i, r(ts / o[i]));
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in new issue