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:
parent
fa9e07329a
commit
3c33c75956
|
@ -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')
|
||||
})
|
||||
|
||||
|
|
|
@ -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'),
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"include": ["**/*.ts", "**/*.js"]
|
||||
"include": ["**/*.ts", "**/*.js"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
|
@ -14,6 +14,7 @@
|
|||
"exclude": [
|
||||
"node_modules",
|
||||
"**/__tests__/*",
|
||||
"**/__mocks__/*",
|
||||
"**/*.test.ts"
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue