forked from kolplattformen/skolplattformen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { parseDate } from '../dateHandling' | ||
|
||
test.each([ | ||
['2020-12-21 09:00', '2020-12-21T08:00:00.000Z'], | ||
['2021-05-28', '2021-05-27T22:00:00.000Z'], | ||
['2 oktober 2020', '2020-10-01T22:00:00.000Z'], | ||
['12 oktober 2020', '2020-10-11T22:00:00.000Z'], | ||
['This is an invalid date', undefined], | ||
])('handles date parsing of %s', (input, expected) => { | ||
expect(parseDate(input)).toEqual(expected) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { DateTime } from 'luxon' | ||
|
||
const options = { | ||
locale: 'sv', | ||
} | ||
|
||
export const parseDate = (input?: string): string | undefined => { | ||
if (!input) { | ||
return undefined | ||
} | ||
|
||
const dateParse = (format: string) => | ||
DateTime.fromFormat(input, format, options) | ||
|
||
const dateAndTime = dateParse('yyyy-MM-dd HH:mm') | ||
|
||
if (dateAndTime.isValid) { | ||
return dateAndTime.toUTC().toISO() | ||
} | ||
|
||
const onlyDate = dateParse('yyyy-MM-dd') | ||
|
||
if (onlyDate.isValid) { | ||
return onlyDate.toUTC().toISO() | ||
} | ||
|
||
const dateLongForm = dateParse('dd MMMM yyyy') | ||
|
||
if (dateLongForm.isValid) { | ||
return dateLongForm.toUTC().toISO() | ||
} | ||
|
||
const dateLongFormOneDigit = dateParse('d MMMM yyyy') | ||
|
||
if (dateLongFormOneDigit.isValid) { | ||
return dateLongFormOneDigit.toUTC().toISO() | ||
} | ||
|
||
// Explicit return to satisfy ESLint | ||
return undefined | ||
} |