Skip to content

Commit

Permalink
fix: handle invalid Date objects (algolia#605)
Browse files Browse the repository at this point in the history
calling toISOString() on an invalid Date object will throw a Range Error
  • Loading branch information
ttoohey committed Mar 21, 2021
1 parent d7465fa commit c77f446
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/formatter/formatPropValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ const formatPropValue = (
}

if (propValue instanceof Date) {
if (isNaN(propValue.valueOf())) {
return `{new Date(NaN)}`;
}
return `{new Date("${propValue.toISOString()}")}`;
}

Expand Down
6 changes: 6 additions & 0 deletions src/formatter/formatPropValue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ describe('formatPropValue', () => {
).toBe('{new Date("2017-01-01T11:00:00.000Z")}');
});

it('should format an invalid date prop value', () => {
expect(formatPropValue(new Date(NaN), false, 0, {})).toBe(
'{new Date(NaN)}'
);
});

it('should format an object prop value', () => {
expect(formatPropValue({ foo: 42 }, false, 0, {})).toBe(
'{*Mocked formatComplexDataStructure result*}'
Expand Down

0 comments on commit c77f446

Please sign in to comment.