Skip to content

Commit

Permalink
Merge pull request #61 from ivanhofer/invalid-date
Browse files Browse the repository at this point in the history
support to stringify invalid dates
  • Loading branch information
Rich-Harris committed Apr 19, 2024
2 parents 49a88ec + 238a7fd commit 127d655
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export function stringify(value, reducers) {
break;

case 'Date':
str = `["Date","${thing.toISOString()}"]`;
const valid = !isNaN(thing.getDate());
str = `["Date","${valid ? thing.toISOString() : ''}"]`;
break;

case 'RegExp':
Expand Down
9 changes: 9 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ const fixtures = {
js: 'new Date(1000000000000)',
json: '[["Date","2001-09-09T01:46:40.000Z"]]'
},
{
name: 'invalid Date',
value: new Date(''),
js: 'new Date(NaN)',
json: '[["Date",""]]',
validate: (value) => {
assert.ok(isNaN(value.valueOf()));
}
},
{
name: 'Array',
value: ['a', 'b', 'c'],
Expand Down

0 comments on commit 127d655

Please sign in to comment.