diff --git a/src/guards/__tests__/is-booleanish.guard.spec.ts b/src/guards/__tests__/is-booleanish.guard.spec.ts new file mode 100644 index 00000000..bde1a84b --- /dev/null +++ b/src/guards/__tests__/is-booleanish.guard.spec.ts @@ -0,0 +1,28 @@ +import type { Testcase } from '@tests/utils/types' +import testSubject from '../is-booleanish.guard' + +/** + * @file Unit Tests - isBooleanish + * @module tutils/guards/tests/unit/isBooleanish + */ + +describe('unit:guards/isBooleanish', () => { + type Case = Testcase & { state: string; value: any } + + const cases: Case[] = [ + { expected: false, state: 'array', value: [] }, + { expected: false, state: 'number', value: 13 }, + { expected: false, state: 'object', value: { data: 26 } }, + { expected: true, state: 'boolean', value: false }, + { expected: true, state: "'false'", value: 'false' }, + { expected: true, state: "'true'", value: 'true' } + ] + + it.each(cases)('should return $expected given $state', testcase => { + // Arrange + const { expected, value } = testcase + + // Act + Expect + expect(testSubject(value)).toBe(expected) + }) +}) diff --git a/src/guards/index.ts b/src/guards/index.ts index 56327565..554498e9 100644 --- a/src/guards/index.ts +++ b/src/guards/index.ts @@ -3,5 +3,6 @@ * @module tutils/guards */ +export { default as isBooleanish } from './is-booleanish.guard' export { default as isNIL } from './is-nil.guard' export { default as isNodeEnv } from './is-node-env.guard' diff --git a/src/guards/is-booleanish.guard.ts b/src/guards/is-booleanish.guard.ts new file mode 100644 index 00000000..a2bbeca9 --- /dev/null +++ b/src/guards/is-booleanish.guard.ts @@ -0,0 +1,18 @@ +import { Booleanish } from '@tutils/types' + +/** + * @file Type Guards - isBooleanish + * @module tutils/guards/isBooleanish + */ + +/** + * Checks if `value` is a `boolean`, `'false'`, or `'true'`. + * + * @param {any} [value] - Value to check + * @return {boolean} `true` if `value` is a `boolean`, `'false'`, or `'true'` + */ +const isBooleanish = (value?: any): value is Booleanish => { + return typeof value === 'boolean' || value === 'false' || value === 'true' +} + +export default isBooleanish