1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-26 16:09:27 -05:00

fix(console): don't throw RangeError when an invalid date is passed (#4929)

This commit is contained in:
uki00a 2020-04-28 02:39:39 +09:00 committed by GitHub
parent 62976a1c94
commit d440495b6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 3 deletions

View file

@ -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 // console.dir test
unitTest(function consoleDir(): void { unitTest(function consoleDir(): void {
mockConsole((console, out): void => { mockConsole((console, out): void => {

View file

@ -1,5 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // 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 { cliTable } from "./console_table.ts";
import { exposeForTest } from "../internals.ts"; import { exposeForTest } from "../internals.ts";
import { PromiseState } from "./promise.ts"; import { PromiseState } from "./promise.ts";
@ -409,8 +409,7 @@ function createWeakMapString(): string {
} }
function createDateString(value: Date): string { function createDateString(value: Date): string {
// without quotes, ISO format return isInvalidDate(value) ? "Invalid Date" : value.toISOString(); // without quotes, ISO format
return value.toISOString();
} }
function createRegExpString(value: RegExp): string { function createRegExpString(value: RegExp): string {

View file

@ -18,6 +18,11 @@ export function isTypedArray(x: unknown): x is TypedArray {
return ArrayBuffer.isView(x) && !(x instanceof DataView); return ArrayBuffer.isView(x) && !(x instanceof DataView);
} }
// @internal
export function isInvalidDate(x: Date): boolean {
return isNaN(x.getTime());
}
// @internal // @internal
export function requiredArguments( export function requiredArguments(
name: string, name: string,