Skip to content

Commit

Permalink
fix: do not eat new lines (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
stayradiated authored Apr 8, 2024
1 parent 3a15d65 commit 7546094
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/parse-date-from-message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,16 @@ describe('GetDateFromTsOptions', () => {
message: '_(3 days late):_ (4 days ago): (5 days ago): hello',
})
})
test('allow new lines', () => {
const dateString = parseDateFromMessage({
messageText: '(3 days ago): \nhello world',
ts,
timeZone: 'Pacific/Auckland',
})
expect(dateString).toEqual({
type: 'MATCH',
date: '2023-07-15T00:00:00+00:00',
message: '_(3 days late):_ \nhello world',
})
})
})
8 changes: 4 additions & 4 deletions src/parse-date-from-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const parseDateFromMessage = (
const instant = Number.parseInt(ts, 10) * 1000

// Match if text starts with "(date):"
const regexMatch = /^\((today|yesterday|(\d+) days? ago)\):/.exec(messageText)
const regexMatch = /^\((today|yesterday|(\d+) days? ago)\): */.exec(
messageText,
)
const relativeDate = regexMatch?.[1]
const daysAgoString = regexMatch?.[2]
if (!regexMatch || !relativeDate) {
Expand Down Expand Up @@ -64,9 +66,7 @@ Supported dates:
timeZone,
})

const messageWithoutRelativeDate = messageText
.slice(regexMatch[0].length)
.trim()
const messageWithoutRelativeDate = messageText.slice(regexMatch[0].length)

const daysLater = dateFns.differenceInDays(instant, date)

Expand Down

0 comments on commit 7546094

Please sign in to comment.