parent
e918b82cfa
commit
403d77ce14
2 changed files with 45 additions and 3 deletions
|
@ -222,7 +222,7 @@ export const getDateFromTextIn = (text: string, now: Date = new Date()) => {
|
|||
}
|
||||
|
||||
const getDateFromWeekday = (text: string): dateFoundResult => {
|
||||
const matcher: RegExp = / (mon|monday|tue|tuesday|wed|wednesday|thu|thursday|fri|friday|sat|saturday|sun|sunday)/ig
|
||||
const matcher: RegExp = / (next )?(monday|mon|tuesday|tue|wednesday|wed|thursday|thu|friday|fri|saturday|sat|sunday|sun)/ig
|
||||
const results: string[] | null = matcher.exec(text)
|
||||
if (results === null) {
|
||||
return {
|
||||
|
@ -235,7 +235,7 @@ const getDateFromWeekday = (text: string): dateFoundResult => {
|
|||
const currentDay: number = date.getDay()
|
||||
let day: number = 0
|
||||
|
||||
switch (results[1]) {
|
||||
switch (results[2]) {
|
||||
case 'mon':
|
||||
case 'monday':
|
||||
day = 1
|
||||
|
@ -275,7 +275,7 @@ const getDateFromWeekday = (text: string): dateFoundResult => {
|
|||
date.setDate(date.getDate() + distance)
|
||||
|
||||
return {
|
||||
foundText: results[1],
|
||||
foundText: results[0],
|
||||
date: date,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -207,6 +207,48 @@ describe('Parse Task Text', () => {
|
|||
expect(result.date).toBeNull()
|
||||
})
|
||||
|
||||
describe('Parse weekdays', () => {
|
||||
|
||||
const days = {
|
||||
'mon': 1,
|
||||
'monday': 1,
|
||||
'tue': 2,
|
||||
'tuesday': 2,
|
||||
'wed': 3,
|
||||
'wednesday': 3,
|
||||
'thu': 4,
|
||||
'thursday': 4,
|
||||
'fri': 5,
|
||||
'friday': 5,
|
||||
'sat': 6,
|
||||
'saturday': 6,
|
||||
'sun': 7,
|
||||
'sunday': 7,
|
||||
}
|
||||
|
||||
const prefix = [
|
||||
'next ',
|
||||
'',
|
||||
]
|
||||
|
||||
prefix.forEach(p => {
|
||||
for (const d in days) {
|
||||
it(`should recognize ${p}${d}`, () => {
|
||||
const result = parseTaskText(`Lorem Ipsum ${p}${d}`)
|
||||
|
||||
const next = new Date()
|
||||
const distance = (days[d] + 7 - next.getDay()) % 7
|
||||
next.setDate(next.getDate() + distance)
|
||||
|
||||
expect(result.text).toBe('Lorem Ipsum')
|
||||
expect(result.date.getFullYear()).toBe(next.getFullYear())
|
||||
expect(result.date.getMonth()).toBe(next.getMonth())
|
||||
expect(result.date.getDate()).toBe(next.getDate())
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('Parse date from text', () => {
|
||||
const now = new Date()
|
||||
now.setFullYear(2021, 5, 24)
|
||||
|
|
Loading…
Reference in a new issue