fix: 🐛 Parse old aspnet dates instead of unreliable format strings (#108)

Parse old asp.net dates instead of string

Parsing with DateTime.fromFormat are not 100% sure on all devices -
for me I got undefined in iOS simulator but it worked fine in Node.
( see https://moment.github.io/luxon/docs/manual/parsing.html#fromformat)

 Closes: #105
This commit is contained in:
Andreas Eriksson 2021-04-05 20:43:02 +02:00 committed by GitHub
parent fa9e07329a
commit 3c33c75956
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 8 deletions

View File

@ -78,7 +78,7 @@ describe('news', () => {
'Hej, Nu är problemet löst! Alla betyg syns som de ska. God jul!...'
)
expect(item.modified).toEqual('2020-12-18T15:18:00.000Z')
expect(item.published).toEqual('2020-12-18T15:15:00.000Z')
expect(item.published).toEqual('2020-12-18T15:15:42.000Z')
})
it(' body correctly', () => {
const [item] = news(response)
@ -159,8 +159,8 @@ describe('newsItem', () => {
expect(item.intro).toEqual(
'Kära vårdnadshavare! I helgen är det avlusningsdagar!'
)
expect(item.published).toEqual('2021-02-04T13:31:00.000Z')
expect(item.modified).toEqual('2021-02-14T13:37:00.000Z')
expect(item.published).toEqual('2021-02-04T13:31:11.000Z')
expect(item.modified).toEqual('2021-02-04T13:37:32.000Z')
expect(item.author).toEqual('Tieto Evry')
})

View File

@ -12,14 +12,14 @@ export const newsItem = ({
preamble,
body,
bannerImageUrl,
pubDateSe,
modDateSe,
publicationDate,
modifiedDate,
authorDisplayName,
altText,
}: any): NewsItem => ({
header,
published: parseDate(pubDateSe) || '',
modified: parseDate(modDateSe) || '',
published: parseDate(publicationDate) || '',
modified: parseDate(modifiedDate) || '',
id: newsId,
author: authorDisplayName,
intro: preamble.replace(/([!,.])(\w)/gi, '$1 $2'),

View File

@ -5,12 +5,23 @@ const options = {
}
const toISOString = (date: DateTime) => date.toUTC().toISO()
const aspNetJsonRegex = /^\/?Date\((-?\d+)/i
export const parseDate = (input?: string): string | undefined => {
if (!input) {
return undefined
}
// First try and parse old Aps.Net format
// \/Date(1612525846000)\/
// where the numbers are milliseconds from Epoc
const matched = aspNetJsonRegex.exec(input)
if (matched !== null) {
const millisecondsSinceEpoc = parseInt(matched[1], 10)
const date = DateTime.fromMillis(millisecondsSinceEpoc)
return toISOString(date)
}
const dateParse = (format: string) =>
DateTime.fromFormat(input, format, options)

View File

@ -1,4 +1,5 @@
{
"extends": "./tsconfig.json",
"include": ["**/*.ts", "**/*.js"]
"include": ["**/*.ts", "**/*.js"],
"exclude": ["node_modules"]
}

View File

@ -14,6 +14,7 @@
"exclude": [
"node_modules",
"**/__tests__/*",
"**/__mocks__/*",
"**/*.test.ts"
]
}