diff options
author | uki00a <uki00a@gmail.com> | 2020-04-28 02:39:39 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-27 13:39:39 -0400 |
commit | d440495b6bc20eb8008397f3c744d8fa1ab578f5 (patch) | |
tree | 25c8114e6ead31a5154a78bfd9455b6317b40942 /cli/js | |
parent | 62976a1c948e35e14a518e0134d20b497f8f9910 (diff) |
fix(console): don't throw RangeError when an invalid date is passed (#4929)
Diffstat (limited to 'cli/js')
-rw-r--r-- | cli/js/tests/console_test.ts | 9 | ||||
-rw-r--r-- | cli/js/web/console.ts | 5 | ||||
-rw-r--r-- | cli/js/web/util.ts | 5 |
3 files changed, 16 insertions, 3 deletions
diff --git a/cli/js/tests/console_test.ts b/cli/js/tests/console_test.ts index bceee9419..4a227aafe 100644 --- a/cli/js/tests/console_test.ts +++ b/cli/js/tests/console_test.ts @@ -1067,6 +1067,15 @@ unitTest(function consoleLogShouldNotThrowError(): void { }); }); +// console.log(Invalid Date) test +unitTest(function consoleLogShoultNotThrowErrorWhenInvalidDateIsPassed(): void { + mockConsole((console, out) => { + const invalidDate = new Date("test"); + console.log(invalidDate); + assertEquals(out.toString(), "Invalid Date\n"); + }); +}); + // console.dir test unitTest(function consoleDir(): void { mockConsole((console, out): void => { diff --git a/cli/js/web/console.ts b/cli/js/web/console.ts index 5544ded3c..4709697d2 100644 --- a/cli/js/web/console.ts +++ b/cli/js/web/console.ts @@ -1,5 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { isTypedArray, TypedArray } from "./util.ts"; +import { isInvalidDate, isTypedArray, TypedArray } from "./util.ts"; import { cliTable } from "./console_table.ts"; import { exposeForTest } from "../internals.ts"; import { PromiseState } from "./promise.ts"; @@ -409,8 +409,7 @@ function createWeakMapString(): string { } function createDateString(value: Date): string { - // without quotes, ISO format - return value.toISOString(); + return isInvalidDate(value) ? "Invalid Date" : value.toISOString(); // without quotes, ISO format } function createRegExpString(value: RegExp): string { diff --git a/cli/js/web/util.ts b/cli/js/web/util.ts index 824d00d4b..0ab367554 100644 --- a/cli/js/web/util.ts +++ b/cli/js/web/util.ts @@ -19,6 +19,11 @@ export function isTypedArray(x: unknown): x is TypedArray { } // @internal +export function isInvalidDate(x: Date): boolean { + return isNaN(x.getTime()); +} + +// @internal export function requiredArguments( name: string, length: number, |