summaryrefslogtreecommitdiff
path: root/std/node
diff options
context:
space:
mode:
authorPeter Evers <pevers90@gmail.com>2020-06-02 00:43:43 +0200
committerGitHub <noreply@github.com>2020-06-01 18:43:43 -0400
commit6b0d286a3d213fb1781558cacbd4135a9b381ae9 (patch)
tree502f17248f27b56eb60d54dbc8f17a303d80721b /std/node
parent30785ed592d97717922ab65cbffb2353ed466350 (diff)
feat(std/node): add util.type.isDate (#6029)
Diffstat (limited to 'std/node')
-rw-r--r--std/node/_util/_util_types.ts31
-rw-r--r--std/node/_util/_util_types_test.ts60
-rw-r--r--std/node/util.ts3
-rw-r--r--std/node/util_test.ts8
4 files changed, 102 insertions, 0 deletions
diff --git a/std/node/_util/_util_types.ts b/std/node/_util/_util_types.ts
new file mode 100644
index 000000000..387f22f97
--- /dev/null
+++ b/std/node/_util/_util_types.ts
@@ -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]";
+}
diff --git a/std/node/_util/_util_types_test.ts b/std/node/_util/_util_types_test.ts
new file mode 100644
index 000000000..39be8200d
--- /dev/null
+++ b/std/node/_util/_util_types_test.ts
@@ -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);
+});
diff --git a/std/node/util.ts b/std/node/util.ts
index 8c3fdabe8..b115b061b 100644
--- a/std/node/util.ts
+++ b/std/node/util.ts
@@ -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);
diff --git a/std/node/util_test.ts b/std/node/util_test.ts
index 7036368b6..b64396441 100644
--- a/std/node/util_test.ts
+++ b/std/node/util_test.ts
@@ -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()));
+ },
+});