Skip to content

Commit 127d655

Browse files
authored
Merge pull request #61 from ivanhofer/invalid-date
support to stringify invalid dates
2 parents 49a88ec + 238a7fd commit 127d655

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/stringify.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ export function stringify(value, reducers) {
8181
break;
8282

8383
case 'Date':
84-
str = `["Date","${thing.toISOString()}"]`;
84+
const valid = !isNaN(thing.getDate());
85+
str = `["Date","${valid ? thing.toISOString() : ''}"]`;
8586
break;
8687

8788
case 'RegExp':

test/test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ const fixtures = {
109109
js: 'new Date(1000000000000)',
110110
json: '[["Date","2001-09-09T01:46:40.000Z"]]'
111111
},
112+
{
113+
name: 'invalid Date',
114+
value: new Date(''),
115+
js: 'new Date(NaN)',
116+
json: '[["Date",""]]',
117+
validate: (value) => {
118+
assert.ok(isNaN(value.valueOf()));
119+
}
120+
},
112121
{
113122
name: 'Array',
114123
value: ['a', 'b', 'c'],

0 commit comments

Comments
 (0)