When it comes to invalid date objects e.g. when using new Date() with an invalid date string, the stringify function throws an error.
See the example here:
https://svelte.dev/repl/db77cddd51d84149b7ddb9ce29a87ec0?version=3.49.0
But the same input does not throw an error when using the uneval function.
I guess it makes sense to have a consistent behavior there.
A solution to this problem could look like this:
// https://github.com/Rich-Harris/devalue/blob/master/src/stringify.js#L68
case 'Date':
const isValidDate = !isNaN(thing.getDate())
str = `["Date","${isValidDate ? thing.toISOString() : ''}"]`;
break;
This gets rid of the error and when using parse you'll get back an invalid date.
What do you think?
I can submit PR if you want.
When it comes to invalid date objects e.g. when using
new Date()with an invalid date string, thestringifyfunction throws an error.See the example here:
https://svelte.dev/repl/db77cddd51d84149b7ddb9ce29a87ec0?version=3.49.0
But the same input does not throw an error when using the
unevalfunction.I guess it makes sense to have a consistent behavior there.
A solution to this problem could look like this:
This gets rid of the error and when using
parseyou'll get back an invalid date.What do you think?
I can submit PR if you want.