fix(quick add magic): date parsing with a date at the beginning
Resolves https://github.com/go-vikunja/frontend/issues/110
This commit is contained in:
parent
3b05ce3f10
commit
35a52ef01b
@ -129,7 +129,7 @@ const addTimeToDate = (text: string, date: Date, previousMatch: string | null):
|
||||
}
|
||||
|
||||
export const getDateFromText = (text: string, now: Date = new Date()) => {
|
||||
const fullDateRegex = / ([0-9][0-9]?\/[0-9][0-9]?\/[0-9][0-9]([0-9][0-9])?|[0-9][0-9][0-9][0-9]\/[0-9][0-9]?\/[0-9][0-9]?|[0-9][0-9][0-9][0-9]-[0-9][0-9]?-[0-9][0-9]?)/ig
|
||||
const fullDateRegex = /(^| )([0-9][0-9]?\/[0-9][0-9]?\/[0-9][0-9]([0-9][0-9])?|[0-9][0-9][0-9][0-9]\/[0-9][0-9]?\/[0-9][0-9]?|[0-9][0-9][0-9][0-9]-[0-9][0-9]?-[0-9][0-9]?)/ig
|
||||
|
||||
// 1. Try parsing the text as a "usual" date, like 2021-06-24 or 06/24/2021
|
||||
let results: string[] | null = fullDateRegex.exec(text)
|
||||
@ -138,7 +138,7 @@ export const getDateFromText = (text: string, now: Date = new Date()) => {
|
||||
let containsYear = true
|
||||
if (result === null) {
|
||||
// 2. Try parsing the date as something like "jan 21" or "21 jan"
|
||||
const monthRegex = new RegExp(` (${monthsRegexGroup} [0-9][0-9]?|[0-9][0-9]? ${monthsRegexGroup})`, 'ig')
|
||||
const monthRegex = new RegExp(`(^| )(${monthsRegexGroup} [0-9][0-9]?|[0-9][0-9]? ${monthsRegexGroup})`, 'ig')
|
||||
results = monthRegex.exec(text)
|
||||
result = results === null ? null : `${results[0]} ${now.getFullYear()}`.trim()
|
||||
foundText = results === null ? '' : results[0].trim()
|
||||
@ -146,7 +146,7 @@ export const getDateFromText = (text: string, now: Date = new Date()) => {
|
||||
|
||||
if (result === null) {
|
||||
// 3. Try parsing the date as "27/01" or "01/27"
|
||||
const monthNumericRegex = / ([0-9][0-9]?\/[0-9][0-9]?)/ig
|
||||
const monthNumericRegex = /(^| )([0-9][0-9]?\/[0-9][0-9]?)/ig
|
||||
results = monthNumericRegex.exec(text)
|
||||
|
||||
// Put the year before or after the date, depending on what works
|
||||
@ -299,7 +299,7 @@ const getDateFromWeekday = (text: string): dateFoundResult => {
|
||||
}
|
||||
|
||||
const getDayFromText = (text: string) => {
|
||||
const matcher = /($| )(([1-2][0-9])|(3[01])|(0?[1-9]))(st|nd|rd|th|\.)($| )/ig
|
||||
const matcher = /(^| )(([1-2][0-9])|(3[01])|(0?[1-9]))(st|nd|rd|th|\.)($| )/ig
|
||||
const results = matcher.exec(text)
|
||||
if (results === null) {
|
||||
return {
|
||||
|
@ -438,41 +438,50 @@ describe('Parse Task Text', () => {
|
||||
now.setFullYear(2021, 5, 24)
|
||||
|
||||
const cases = {
|
||||
'Lorem Ipsum 06/08/2021 ad': '2021-6-8',
|
||||
'Lorem Ipsum 6/7/21 ad': '2021-6-7',
|
||||
'dolor sit amet 27/07/2021,': null,
|
||||
'dolor sit amet 2021/07/06,': '2021-7-6',
|
||||
'dolor sit amet 2021-07-06': '2021-7-6',
|
||||
'dolor sit amet 27 jan': '2022-1-27',
|
||||
'dolor sit amet 27/1': '2022-1-27',
|
||||
'dolor sit amet 27/01': '2022-1-27',
|
||||
'dolor sit amet 16/12': '2021-12-16',
|
||||
'dolor sit amet 01/27': '2022-1-27',
|
||||
'dolor sit amet 1/27': '2022-1-27',
|
||||
'dolor sit amet Jan 27': '2022-1-27',
|
||||
'dolor sit amet jan 27': '2022-1-27',
|
||||
'dolor sit amet feb 21': '2022-2-21',
|
||||
'dolor sit amet mar 21': '2022-3-21',
|
||||
'dolor sit amet apr 21': '2022-4-21',
|
||||
'dolor sit amet may 21': '2022-5-21',
|
||||
'dolor sit amet jun 21': '2022-6-21',
|
||||
'dolor sit amet jul 21': '2021-7-21',
|
||||
'dolor sit amet aug 21': '2021-8-21',
|
||||
'dolor sit amet sep 21': '2021-9-21',
|
||||
'dolor sit amet oct 21': '2021-10-21',
|
||||
'dolor sit amet nov 21': '2021-11-21',
|
||||
'dolor sit amet dec 21': '2021-12-21',
|
||||
'06/08/2021': '2021-6-8',
|
||||
'6/7/21': '2021-6-7',
|
||||
'27/07/2021,': null,
|
||||
'2021/07/06,': '2021-7-6',
|
||||
'2021-07-06': '2021-7-6',
|
||||
'27 jan': '2022-1-27',
|
||||
'27/1': '2022-1-27',
|
||||
'27/01': '2022-1-27',
|
||||
'16/12': '2021-12-16',
|
||||
'01/27': '2022-1-27',
|
||||
'1/27': '2022-1-27',
|
||||
'Jan 27': '2022-1-27',
|
||||
'jan 27': '2022-1-27',
|
||||
'feb 21': '2022-2-21',
|
||||
'mar 21': '2022-3-21',
|
||||
'apr 21': '2022-4-21',
|
||||
'may 21': '2022-5-21',
|
||||
'jun 21': '2022-6-21',
|
||||
'jul 21': '2021-7-21',
|
||||
'aug 21': '2021-8-21',
|
||||
'sep 21': '2021-9-21',
|
||||
'oct 21': '2021-10-21',
|
||||
'nov 21': '2021-11-21',
|
||||
'dec 21': '2021-12-21',
|
||||
} as Record<string, string | null>
|
||||
|
||||
for (const c in cases) {
|
||||
it(`should parse '${c}' as '${cases[c]}'`, () => {
|
||||
const {date} = getDateFromText(c, now)
|
||||
it(`should parse '${c}' as '${cases[c]}' with the date at the end`, () => {
|
||||
const {date} = getDateFromText(`Lorem Ipsum ${c}`, now)
|
||||
if (date === null && cases[c] === null) {
|
||||
expect(date).toBeNull()
|
||||
return
|
||||
}
|
||||
|
||||
expect(`${date?.getFullYear()}-${date.getMonth() + 1}-${date?.getDate()}`).toBe(cases[c])
|
||||
expect(`${date?.getFullYear()}-${date?.getMonth() + 1}-${date?.getDate()}`).toBe(cases[c])
|
||||
})
|
||||
it(`should parse '${c}' as '${cases[c]}' with the date at the beginning`, () => {
|
||||
const {date} = getDateFromText(`${c} Lorem Ipsum`, now)
|
||||
if (date === null && cases[c] === null) {
|
||||
expect(date).toBeNull()
|
||||
return
|
||||
}
|
||||
|
||||
expect(`${date?.getFullYear()}-${date?.getMonth() + 1}-${date?.getDate()}`).toBe(cases[c])
|
||||
})
|
||||
}
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user