Skip to content

Commit

Permalink
feat(guards): isBoolean
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Feb 1, 2023
1 parent 70d2b0f commit e7cfedd
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/guards/__tests__/is-boolean.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @file Type Tests - isBoolean
* @module tutils/guards/tests/unit-d/isBoolean
*/

import type testSubject from '../is-boolean'

describe('unit-d:guards/isBoolean', () => {
it('should guard boolean', () => {
expectTypeOf<typeof testSubject>().guards.toBeBoolean()
})
})
20 changes: 20 additions & 0 deletions src/guards/__tests__/is-boolean.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @file Unit Tests - isBoolean
* @module tutils/guards/tests/unit/isBoolean
*/

import testSubject from '../is-boolean'

describe('unit:guards/isBoolean', () => {
it('should return false if value is not a boolean', () => {
expect(testSubject(faker.number.int())).to.be.false
})

it('should return true if value is a boolean', () => {
// Arrange
const cases: Parameters<typeof testSubject>[] = [[false], [true]]

// Act + Expect
cases.forEach(([value]) => expect(testSubject(value)).to.be.true)
})
})
1 change: 1 addition & 0 deletions src/guards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

export { default as isAppEnv } from './is-app-env'
export { default as isBigInt } from './is-big-int'
export { default as isBoolean } from './is-boolean'
export { default as isBooleanish } from './is-booleanish'
export { default as isEmptyString } from './is-empty-string'
export { default as isEmptyValue } from './is-empty-value'
Expand Down
16 changes: 16 additions & 0 deletions src/guards/is-boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @file Type Guards - isBoolean
* @module tutils/guards/isBoolean
*/

/**
* Checks if the given `value` is a boolean.
*
* @param {unknown} value - Value to evaluate
* @return {value is boolean} `true` if `value` is a boolean
*/
const isBoolean = (value: unknown): value is boolean => {
return typeof value === 'boolean'
}

export default isBoolean
3 changes: 2 additions & 1 deletion src/guards/is-booleanish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import type { Booleanish } from '#src/types'
import isBoolean from './is-boolean'

/**
* Checks if the given `value` is a boolean, `'false'`, or `'true'`.
Expand All @@ -12,7 +13,7 @@ import type { Booleanish } from '#src/types'
* @return {value is Booleanish} `true` if `value` is {@linkcode Booleanish}
*/
const isBooleanish = (value: unknown): value is Booleanish => {
return typeof value === 'boolean' || value === 'false' || value === 'true'
return isBoolean(value) || value === 'false' || value === 'true'
}

export default isBooleanish

0 comments on commit e7cfedd

Please sign in to comment.