Skip to content

Commit

Permalink
feat(guards): isBooleanish
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed Nov 12, 2021
1 parent 011f516 commit 7d53fcd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/guards/__tests__/is-booleanish.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> & { 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<Case>(cases)('should return $expected given $state', testcase => {
// Arrange
const { expected, value } = testcase

// Act + Expect
expect(testSubject(value)).toBe(expected)
})
})
1 change: 1 addition & 0 deletions src/guards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
18 changes: 18 additions & 0 deletions src/guards/is-booleanish.guard.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7d53fcd

Please sign in to comment.