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