Repository: marpple/FxTS
Pull request #307 (2025-02-28 06:10:00)

feat: Add `isDate` function
Add `isDate` function


Patch:
diff --git a/src/index.ts b/src/index.ts
index 44772196..829a7397 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -22,6 +22,7 @@ import includes from "./includes";
 import indexBy from "./indexBy";
 import isArray from "./isArray";
 import isBoolean from "./isBoolean";
+import isDate from "./isDate";
 import isEmpty from "./isEmpty";
 import isNil from "./isNil";
 import isNull from "./isNull";
@@ -93,6 +94,7 @@ export {
   indexBy,
   isArray,
   isBoolean,
+  isDate,
   isEmpty,
   isNil,
   isNumber,
diff --git a/src/isDate.ts b/src/isDate.ts
new file mode 100644
index 00000000..cdd58584
--- /dev/null
+++ b/src/isDate.ts
@@ -0,0 +1,14 @@
+/**
+ * Returns true if `input` is a Date object.
+ *
+ * @example
+ * ```ts
+ * isDate(new Date()); // true
+ * isDate("2024-01-01"); // false
+ * ```
+ */
+const isDate = <T>(input: T): input is Extract<T, Date> => {
+  return input instanceof Date;
+};
+
+export default isDate;
diff --git a/website/function.json b/website/function.json
index 902836aa..67866d2a 100644
--- a/website/function.json
+++ b/website/function.json
@@ -70,6 +70,7 @@
     "indexBy",
     "isArray",
     "isBoolean",
+    "isDate",
     "isEmpty",
     "isNil",
     "isNumber",
diff --git a/website/markdown-plugin/src/function.json b/website/markdown-plugin/src/function.json
index 902836aa..67866d2a 100644
--- a/website/markdown-plugin/src/function.json
+++ b/website/markdown-plugin/src/function.json
@@ -70,6 +70,7 @@
     "indexBy",
     "isArray",
     "isBoolean",
+    "isDate",
     "isEmpty",
     "isNil",
     "isNumber",


Test patch:
diff --git a/test/isDate.spec.ts b/test/isDate.spec.ts
new file mode 100644
index 00000000..993f0ece
--- /dev/null
+++ b/test/isDate.spec.ts
@@ -0,0 +1,30 @@
+import { isDate } from "../src";
+
+describe("isDate", function () {
+  it.each([
+    undefined,
+    null,
+    true,
+    1,
+    "2024-01-01",
+    Symbol("a"),
+    () => null,
+    {},
+    new Map(),
+  ])("given non Date then should return false", function (value) {
+    const result = isDate(value);
+    expect(result).toEqual(false);
+  });
+
+  it("given Date then should return true", function () {
+    const date = new Date();
+    const result = isDate(date);
+    expect(result).toEqual(true);
+  });
+
+  it("given invalid Date then should return true", function () {
+    const invalidDate = new Date("invalid");
+    const result = isDate(invalidDate);
+    expect(result).toEqual(true);
+  });
+});
diff --git a/type-check/isDate.test.ts b/type-check/isDate.test.ts
new file mode 100644
index 00000000..92123c19
--- /dev/null
+++ b/type-check/isDate.test.ts
@@ -0,0 +1,32 @@
+import { filter, isDate, pipe, toArray } from "../src";
+import * as Test from "../src/types/Test";
+
+const { checks, check } = Test;
+
+const res1 = isDate(new Date());
+const res2 = isDate("2024-01-01");
+const res3 = pipe(new Date(), isDate);
+
+const res4 = pipe(
+  [new Date(), "2025-01-01", new Date()] as const,
+
+  filter(isDate),
+
+  toArray,
+);
+
+const res5 = pipe(
+  [1, null, undefined, "1", {}, "2025-01-01"] as const,
+
+  filter(isDate),
+
+  toArray,
+);
+
+checks([
+  check<typeof res1, boolean, Test.Pass>(),
+  check<typeof res2, boolean, Test.Pass>(),
+  check<typeof res3, boolean, Test.Pass>(),
+  check<typeof res4, Date[], Test.Pass>(),
+  check<typeof res5, never[], Test.Pass>(),
+]);