1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 21:50:00 -05:00

feat(std/node): add util.type.isDate (#6029)

This commit is contained in:
Peter Evers 2020-06-02 00:43:43 +02:00 committed by GitHub
parent 30785ed592
commit 6b0d286a3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 102 additions and 0 deletions

View file

@ -0,0 +1,31 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
//
// Adapted from Node.js. Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
const _toString = Object.prototype.toString;
const _isObjectLike = (value: unknown): boolean =>
value !== null && typeof value === "object";
export function isDate(value: unknown): boolean {
return _isObjectLike(value) && _toString.call(value) === "[object Date]";
}

View file

@ -0,0 +1,60 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
//
// Adapted from Node.js. Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
import { assertStrictEq } from "../../testing/asserts.ts";
import { isDate } from "./_util_types.ts";
const { test } = Deno;
test("New date instance with no arguments", () => {
assertStrictEq(isDate(new Date()), true);
});
test("New date instance with value 0", () => {
assertStrictEq(isDate(new Date(0)), true);
});
test("New date instance in new context", () => {
assertStrictEq(isDate(new (eval("Date"))()), true);
});
test("Date function is not of type Date", () => {
assertStrictEq(isDate(Date()), false);
});
test("Object is not of type Date", () => {
assertStrictEq(isDate({}), false);
});
test("Array is not of type Date", () => {
assertStrictEq(isDate([]), false);
});
test("Error is not of type Date", () => {
assertStrictEq(isDate(new Error()), false);
});
test("New object from Date prototype is not of type Date", () => {
assertStrictEq(isDate(Object.create(Date.prototype)), false);
});

View file

@ -1,4 +1,7 @@
export { callbackify } from "./_util/_util_callbackify.ts";
import * as types from "./_util/_util_types.ts";
export { types };
export function isArray(value: unknown): boolean {
return Array.isArray(value);

View file

@ -166,3 +166,11 @@ test({
assert(te instanceof TextEncoder);
},
});
test({
name: "[util] isDate",
fn() {
// Test verifies the method is exposed. See _util/_util_types_test for details
assert(util.types.isDate(new Date()));
},
});