Skip to content

Commit e6c532f

Browse files
authored
Merge pull request #3161 from OpenNeuroOrg/3150-tsv-date-parse-bug
fix(app): Catch parsing errors for extractDateString
2 parents 639a663 + 4040b93 commit e6c532f

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

packages/openneuro-app/src/scripts/components/__tests__/data-table.spec.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react"
22
import { render } from "@testing-library/react"
3-
import { DataTable } from "../data-table"
3+
import { DataTable, extractDateString } from "../data-table"
44

55
const tableData = [
66
{ col1: "text", col2: 50, col3: "string value" },
@@ -13,3 +13,9 @@ describe("DataTable component", () => {
1313
expect(asFragment()).toMatchSnapshot()
1414
})
1515
})
16+
17+
describe("extractDateString()", () => {
18+
it("handles invalid date format exceptions", () => {
19+
expect(() => extractDateString("1893-09-18T08:27:33")).not.toThrowError()
20+
})
21+
})

packages/openneuro-app/src/scripts/components/data-table.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const TD = styled.td`
3333
padding: 3px;
3434
`
3535

36-
function extractDateString(dateString) {
36+
export function extractDateString(dateString) {
3737
const formats = [
3838
"yyyy-MM-dd", // ISO 8601
3939
"yyyy-MM-ddTHH:mm:ss", // ISO 8601 with time
@@ -42,9 +42,13 @@ function extractDateString(dateString) {
4242
]
4343

4444
for (const format of formats) {
45-
const parsedDate = parse(dateString, format, new Date())
46-
if (isValid(parsedDate)) {
47-
return parsedDate
45+
try {
46+
const parsedDate = parse(dateString, format, new Date())
47+
if (isValid(parsedDate)) {
48+
return parsedDate
49+
}
50+
} catch (_err) {
51+
continue
4852
}
4953
}
5054

0 commit comments

Comments
 (0)