From 87c7d1a5f404c0cabe542cd1ea53e7d08232b89a Mon Sep 17 00:00:00 2001 From: venashial Date: Tue, 31 May 2022 21:03:22 -0700 Subject: [PATCH] Update `ago` to respect user's language --- src/package/utils/ago.ts | 93 +++++----------------------------------- 1 file changed, 11 insertions(+), 82 deletions(-) diff --git a/src/package/utils/ago.ts b/src/package/utils/ago.ts index 78a79496f..8e780800a 100644 --- a/src/package/utils/ago.ts +++ b/src/package/utils/ago.ts @@ -1,15 +1,18 @@ /** - * Human readable elapsed or remaining time (example: 3 minutes ago) - * @author https://github.com/victornpb - * @see https://stackoverflow.com/a/67338038/938822 + * Human readable elapsed or remaining time (example: 3 minutes) + * modified from https://stackoverflow.com/a/67338038/938822 */ + +const rft = new Intl.RelativeTimeFormat( + typeof navigator !== 'undefined' ? [...navigator.languages] : ['en'], + { numeric: 'auto' } +) + export function ago( /** A Date object, timestamp or string parsable with Date.parse() */ date: string | number | Date, /** A Date object, timestamp or string parsable with Date.parse() */ - nowDate: string | number | Date = Date.now(), - /** A Intl formater */ - rft: Intl.RelativeTimeFormat = new Intl.RelativeTimeFormat(undefined, { numeric: 'auto' }) + nowDate: string | number | Date = Date.now() ): string { const SECOND = 1000 const MINUTE = 60 * SECOND @@ -25,8 +28,7 @@ export function ago( { ge: DAY, divisor: DAY, unit: 'day' }, { ge: HOUR, divisor: HOUR, unit: 'hour' }, { ge: MINUTE, divisor: MINUTE, unit: 'minute' }, - { ge: 30 * SECOND, divisor: SECOND, unit: 'seconds' }, - { ge: 0, divisor: 1, text: 'just now' }, + { ge: 0, divisor: SECOND, unit: 'seconds' }, ] const now = typeof nowDate === 'object' ? nowDate.getTime() : new Date(nowDate).getTime() const diff = now - (typeof date === 'object' ? date : new Date(date)).getTime() @@ -35,80 +37,7 @@ export function ago( if (diffAbs >= interval.ge) { const x = Math.round(Math.abs(diff) / interval.divisor) const isFuture = diff < 0 - if ( - typeof navigator !== 'undefined' && - (navigator.language === 'zh-CN' || navigator.language === 'zh') - ) { - return chs_format(x, isFuture, interval.unit as Unit) - } - return interval.unit ? rft.format(isFuture ? x : -x, interval.unit as Unit) : interval.text + return rft.format(isFuture ? x : -x, interval.unit as Intl.RelativeTimeFormatUnit) } } } - -type Unit = - | 'second' - | 'seconds' - | 'minute' - | 'minutes' - | 'hour' - | 'hours' - | 'day' - | 'days' - | 'week' - | 'weeks' - | 'month' - | 'months' - | 'year' - | 'years' - -type ChsUnit = '秒' | '分' | '小时' | '天' | '周' | '月' | '年' - -/** - * Convert unit to chinese unit - * @param unit - * @returns {ChsUnit} - */ -function convertUnitToChsUnit(unit: Unit): ChsUnit { - switch (unit) { - case 'second': - case 'seconds': - return '秒' - case 'minute': - case 'minutes': - return '分' - case 'hour': - case 'hours': - return '小时' - case 'day': - case 'days': - return '天' - case 'week': - case 'weeks': - return '周' - case 'month': - case 'months': - return '月' - case 'year': - case 'years': - return '年' - } -} - -/** - * The default converter provided by js does not conform to Chinese typography. - * @param value date value - * @param isFuture - * @param unit - * @returns {string} - */ -function chs_format(value: number, isFuture: boolean, unit: Unit): string { - const chsUnit = convertUnitToChsUnit(unit) - let quantifier = '' - switch (chsUnit) { - case '月': - case '小时': - quantifier = '个' - } - return `${value} ${quantifier}${chsUnit}${isFuture ? '后' : '前'}` -}