Skip to content

Commit b65ef24

Browse files
committed
support to stringify invalid dates
1 parent 99e66d0 commit b65ef24

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
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 isValidDate = !isNaN(thing.getDate())
85+
str = `["Date","${isValidDate ? thing.toISOString() : ''}"]`;
8586
break;
8687

8788
case 'RegExp':

test/test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ const fixtures = {
107107
js: 'new Date(1000000000000)',
108108
json: '[["Date","2001-09-09T01:46:40.000Z"]]'
109109
},
110+
{
111+
name: 'invalid Date',
112+
value: new Date(''),
113+
js: 'new Date(NaN)',
114+
json: '[["Date",""]]',
115+
validate: (value) => {
116+
assert.ok(isNaN(value.valueOf()));
117+
}
118+
},
110119
{
111120
name: 'Array',
112121
value: ['a', 'b', 'c'],
@@ -455,7 +464,6 @@ for (const [name, tests] of Object.entries(fixtures)) {
455464
test(t.name, () => {
456465
const actual = unflatten(JSON.parse(t.json), t.revivers);
457466
const expected = t.value;
458-
459467
if (t.validate) {
460468
t.validate(actual);
461469
} else {

0 commit comments

Comments
 (0)