Skip to content

Commit

Permalink
feat: support "X days late" format (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
stayradiated authored Apr 8, 2024
1 parent 7546094 commit b1ef8ac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
16 changes: 14 additions & 2 deletions src/parse-date-from-message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
})
Expand Down
14 changes: 6 additions & 8 deletions src/parse-date-from-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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) {
Expand Down

0 comments on commit b1ef8ac

Please sign in to comment.