From c74281abde19a4ab216809d03d377bc93c7dc425 Mon Sep 17 00:00:00 2001 From: JaeMeDev Date: Wed, 29 Jun 2022 16:09:57 +0900 Subject: [PATCH] feat: add `isBoolean` function --- src/index.ts | 2 ++ src/isBoolean.ts | 15 +++++++++++++++ test/isBoolean.spec.ts | 16 ++++++++++++++++ type-check/isBoolean.test.ts | 14 ++++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 src/isBoolean.ts create mode 100644 test/isBoolean.spec.ts create mode 100644 type-check/isBoolean.test.ts diff --git a/src/index.ts b/src/index.ts index 7c0ba6d4..e3324edd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,6 +23,7 @@ import isArray from "./isArray"; import isEmpty from "./isEmpty"; import isNil from "./isNil"; import isNumber from "./isNumber"; +import isBoolean from "./isBoolean"; import isObject from "./isObject"; import isString from "./isString"; import isUndefined from "./isUndefined"; @@ -82,6 +83,7 @@ export { isEmpty, isNil, isNumber, + isBoolean, isObject, isString, isUndefined, diff --git a/src/isBoolean.ts b/src/isBoolean.ts new file mode 100644 index 00000000..0bf4494c --- /dev/null +++ b/src/isBoolean.ts @@ -0,0 +1,15 @@ +/** + * Returns true if `n` is a Boolean. + * + * @example + * ```ts + * isBoolean(true); // true + * isBoolean(null); // false + * isBoolean("FxTS"); // false + * ``` + */ +function isBoolean(n: unknown): n is boolean { + return typeof n === "boolean"; +} + +export default isBoolean; diff --git a/test/isBoolean.spec.ts b/test/isBoolean.spec.ts new file mode 100644 index 00000000..af210e4c --- /dev/null +++ b/test/isBoolean.spec.ts @@ -0,0 +1,16 @@ +import { isBoolean } from "../src/index"; + +describe("isBoolean", function () { + it.each([undefined, null, 1, "1", Symbol("a"), () => null])( + "given non boolean then should return false", + function (s) { + const result = isBoolean(s); + expect(result).toEqual(false); + }, + ); + + it("given boolean then should return true", function () { + const result = isBoolean(true); + expect(result).toEqual(true); + }); +}); diff --git a/type-check/isBoolean.test.ts b/type-check/isBoolean.test.ts new file mode 100644 index 00000000..25f7782d --- /dev/null +++ b/type-check/isBoolean.test.ts @@ -0,0 +1,14 @@ +import * as Test from "../src/types/Test"; +import { isBoolean, pipe } from "../src"; + +const { checks, check } = Test; + +const res1 = isBoolean(true); +const res2 = isBoolean("a"); +const res3 = pipe(true, isBoolean); + +checks([ + check(), + check(), + check(), +]);