From 5724903ba3366f82228f3136f2c2d548c03f3440 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Thu, 29 Aug 2024 17:00:40 -0500 Subject: [PATCH] Remove old ago --- __tests__/lib/string.test.ts | 75 ------------------------------ src/lib/strings/time.ts | 88 ------------------------------------ 2 files changed, 163 deletions(-) diff --git a/__tests__/lib/string.test.ts b/__tests__/lib/string.test.ts index 95beaf13b1..f226de992b 100644 --- a/__tests__/lib/string.test.ts +++ b/__tests__/lib/string.test.ts @@ -1,5 +1,4 @@ import {RichText} from '@atproto/api' -import {I18n} from '@lingui/core' import {parseEmbedPlayerFromUrl} from 'lib/strings/embed-player' import { @@ -147,80 +146,6 @@ describe('makeRecordUri', () => { }) }) -describe('ago', () => { - const oneYearDate = new Date( - new Date().setMonth(new Date().getMonth() - 11), - ).setDate(new Date().getDate() - 28) - - const inputs = [ - 1671461038, - '04 Dec 1995 00:12:00 GMT', - new Date(), - new Date().setSeconds(new Date().getSeconds() - 10), - new Date().setMinutes(new Date().getMinutes() - 10), - new Date().setHours(new Date().getHours() - 1), - new Date().setDate(new Date().getDate() - 1), - new Date().setDate(new Date().getDate() - 20), - new Date().setDate(new Date().getDate() - 25), - new Date().setDate(new Date().getDate() - 28), - new Date().setDate(new Date().getDate() - 29), - new Date().setDate(new Date().getDate() - 30), - new Date().setMonth(new Date().getMonth() - 1), - new Date(new Date().setMonth(new Date().getMonth() - 1)).setDate( - new Date().getDate() - 20, - ), - new Date(new Date().setMonth(new Date().getMonth() - 1)).setDate( - new Date().getDate() - 25, - ), - new Date(new Date().setMonth(new Date().getMonth() - 1)).setDate( - new Date().getDate() - 28, - ), - new Date(new Date().setMonth(new Date().getMonth() - 1)).setDate( - new Date().getDate() - 29, - ), - new Date().setMonth(new Date().getMonth() - 11), - new Date(new Date().setMonth(new Date().getMonth() - 11)).setDate( - new Date().getDate() - 20, - ), - new Date(new Date().setMonth(new Date().getMonth() - 11)).setDate( - new Date().getDate() - 25, - ), - oneYearDate, - ] - const outputs = [ - new Date(1671461038).toLocaleDateString(), - new Date('04 Dec 1995 00:12:00 GMT').toLocaleDateString(), - 'now', - '10s', - '10m', - '1h', - '1d', - '20d', - '25d', - '28d', - '29d', - '1mo', - '1mo', - '1mo', - '1mo', - '2mo', - '2mo', - '11mo', - '11mo', - '11mo', - new Date(oneYearDate).toLocaleDateString(), - ] - - it('correctly calculates how much time passed, in a string', () => { - const i18n = new I18n({locale: 'en'}) - - for (let i = 0; i < inputs.length; i++) { - const result = ago(i18n, inputs[i]) - expect(result).toEqual(outputs[i]) - } - }) -}) - describe('makeValidHandle', () => { const inputs = [ 'test-handle-123', diff --git a/src/lib/strings/time.ts b/src/lib/strings/time.ts index 16ffd7649b..e505b7892e 100644 --- a/src/lib/strings/time.ts +++ b/src/lib/strings/time.ts @@ -1,92 +1,4 @@ import {I18n} from '@lingui/core' -import {defineMessage, msg} from '@lingui/macro' - -const NOW = 5 -const MINUTE = 60 -const HOUR = MINUTE * 60 -const DAY = HOUR * 24 -const MONTH_30 = DAY * 30 -const MONTH = DAY * 30.41675 // This results in 365.001 days in a year, which is close enough for nearly all cases -export function ago(i18n: I18n, date: number | string | Date): string { - let ts: number - if (typeof date === 'string') { - ts = Number(new Date(date)) - } else if (date instanceof Date) { - ts = Number(date) - } else { - ts = date - } - - const diffSeconds = Math.floor((Date.now() - ts) / 1e3) - - // We can't use `i18n.number` with a `unit` format here because ICU (which - // provides localization data) doesn't seem to have narrow unit display - // formatting for locales like Japanese, Spanish, French and Turkish. - - // We'll use `defineMessage` here instead of our usual `msg` as we can pass - // an object with a `comment` property that better describes what these - // strings are supposed to be. These comments get stripped out in production. - - // Intermediate variables are created to improve the interpolation keys. - - if (diffSeconds < NOW) { - return i18n._(msg`now`) - } else if (diffSeconds < MINUTE) { - const seconds = diffSeconds - - return i18n._( - defineMessage({ - message: `${seconds}s`, - comment: `How many seconds has passed, displayed in a narrow form`, - }), - ) - } else if (diffSeconds < HOUR) { - const minutes = Math.floor(diffSeconds / MINUTE) - - return i18n._( - defineMessage({ - message: `${minutes}m`, - comment: `How many minutes has passed, displayed in a narrow form`, - }), - ) - } else if (diffSeconds < DAY) { - const hours = Math.floor(diffSeconds / HOUR) - - return i18n._( - defineMessage({ - message: `${hours}h`, - comment: `How many hours has passed, displayed in a narrow form`, - }), - ) - } else if (diffSeconds < MONTH_30) { - const days = Math.round(diffSeconds / DAY) - - return i18n._( - defineMessage({ - message: `${days}d`, - comment: `How many days has passed, displayed in a narrow form`, - }), - ) - } else { - let months = diffSeconds / MONTH - if (months % 1 >= 0.9) { - months = Math.ceil(months) - } else { - months = Math.floor(months) - } - - if (months < 12) { - return i18n._( - defineMessage({ - message: `${months}mo`, - comment: `How many months has passed, displayed in a narrow form`, - }), - ) - } else { - return i18n.date(new Date(ts)) - } - } -} export function niceDate(i18n: I18n, date: number | string | Date) { const d = new Date(date)