From b1ef8ac371377d2c60ea9f0befe8b25baa7e1d7a Mon Sep 17 00:00:00 2001 From: George Czabania <1140167+stayradiated@users.noreply.github.com> Date: Mon, 8 Apr 2024 20:21:51 +0200 Subject: [PATCH] feat: support "X days late" format (#13) --- src/parse-date-from-message.test.ts | 16 ++++++++++++++-- src/parse-date-from-message.ts | 14 ++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/parse-date-from-message.test.ts b/src/parse-date-from-message.test.ts index c5898c1..7554d29 100644 --- a/src/parse-date-from-message.test.ts +++ b/src/parse-date-from-message.test.ts @@ -40,6 +40,18 @@ describe('GetDateFromTsOptions', () => { message: '_(1 day late):_ hello', }) }) + test('1 day late (NZ)', () => { + const dateString = parseDateFromMessage({ + messageText: '(1 day late): hello', + ts, + timeZone: 'Pacific/Auckland', + }) + expect(dateString).toEqual({ + type: 'MATCH', + date: '2023-07-17T00:00:00+00:00', + message: '_(1 day late):_ hello', + }) + }) test('2 days ago (NZ)', () => { const dateString = parseDateFromMessage({ messageText: '(2 days ago): hello', @@ -52,9 +64,9 @@ describe('GetDateFromTsOptions', () => { message: '_(2 days late):_ hello', }) }) - test('3 days ago (NZ)', () => { + test('3 days late (NZ)', () => { const dateString = parseDateFromMessage({ - messageText: '(3 days ago): hello world', + messageText: '(3 days late): hello world', ts, timeZone: 'Pacific/Auckland', }) diff --git a/src/parse-date-from-message.ts b/src/parse-date-from-message.ts index 33612f1..7f39dd5 100644 --- a/src/parse-date-from-message.ts +++ b/src/parse-date-from-message.ts @@ -25,7 +25,7 @@ const parseDateFromMessage = ( const instant = Number.parseInt(ts, 10) * 1000 // Match if text starts with "(date):" - const regexMatch = /^\((today|yesterday|(\d+) days? ago)\): */.exec( + const regexMatch = /^\((today|yesterday|(\d+) days? (:?ago|late))\): */.exec( messageText, ) const relativeDate = regexMatch?.[1] @@ -45,13 +45,11 @@ const parseDateFromMessage = ( } else { return new Error(`⚠️ Sorry, that date is not supported: "${relativeDate}". -Supported dates: -- today -- yesterday -- 1 day ago -- 2 days ago -- 3 days ago -- X days ago`) +Supported date formats: +- (today): +- (yesterday): +- (1 day late): +- (2 days ago):`) } if (date > instant) {