Skip to content

Commit

Permalink
fix: 🐛 Parse old aspnet dates instead of unreliable format strings (k…
Browse files Browse the repository at this point in the history
…olplattformen#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: kolplattformen#105
  • Loading branch information
whyer authored Apr 5, 2021
1 parent fa9e073 commit 3c33c75
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
6 changes: 3 additions & 3 deletions lib/parse/__tests__/news.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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')
})

Expand Down
8 changes: 4 additions & 4 deletions lib/parse/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
11 changes: 11 additions & 0 deletions lib/utils/dateHandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": "./tsconfig.json",
"include": ["**/*.ts", "**/*.js"]
"include": ["**/*.ts", "**/*.js"],
"exclude": ["node_modules"]
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"exclude": [
"node_modules",
"**/__tests__/*",
"**/__mocks__/*",
"**/*.test.ts"
]
}

0 comments on commit 3c33c75

Please sign in to comment.