Skip to content

Commit 0154624

Browse files
committed
🐞 fix: Handle invalid Date instances
1 parent a4e6335 commit 0154624

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

packages/lite/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,11 @@ function buildTree(
421421
// `Date`, `RegExp` and `Error`
422422
if (value instanceof Date || value instanceof RegExp) {
423423
const type = value instanceof Date ? "Date" : "RegExp";
424-
let str = type === "Date" ? (value as Date).toISOString() : value.toString();
424+
let str =
425+
type === "Date" ?
426+
isNaN(value as any) ? "Invalid Date"
427+
: (value as Date).toISOString()
428+
: value.toString();
425429
// The rule of `Symbol.toStringTag` for `Date`s and `RegExp`s is different from other
426430
// objects, so we should handle it here.
427431
const toStringTag = getToStringTag(value, showHidden);

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,11 @@ function buildTree(
523523
// `Date`, `RegExp` and `Error`
524524
if (value instanceof Date || value instanceof RegExp) {
525525
const type = value instanceof Date ? "Date" : "RegExp";
526-
let str = type === "Date" ? (value as Date).toISOString() : value.toString();
526+
let str =
527+
type === "Date" ?
528+
isNaN(value as any) ? "Invalid Date"
529+
: (value as Date).toISOString()
530+
: value.toString();
527531
// The rule of `Symbol.toStringTag` for `Date`s and `RegExp`s is different from other
528532
// objects, so we should handle it here.
529533
const toStringTag = getToStringTag(value, showHidden);

0 commit comments

Comments
 (0)