Skip to content

Commit

Permalink
feat(guards): isNumber
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 e7cfedd commit d64f34c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/guards/__tests__/is-number.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @file Type Tests - isNumber
* @module tutils/guards/tests/unit-d/isNumber
*/

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

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

import testSubject from '../is-number'

describe('unit:guards/isNumber', () => {
it('should return false if value is not a number', () => {
// Arrange
const cases: Parameters<typeof testSubject>[] = [
[Number.NaN],
[faker.datatype.array()],
[faker.string.numeric()]
]

// Act + Expect
cases.forEach(([value]) => expect(testSubject(value)).to.be.false)
})

it('should return true if value is a number', () => {
// Arrange
const cases: Parameters<typeof testSubject>[] = [
[faker.number.float()],
[faker.number.int()]
]

// 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 @@ -12,3 +12,4 @@ export { default as isEmptyValue } from './is-empty-value'
export { default as isJwtType } from './is-jwt-type'
export { default as isNIL } from './is-nil'
export { default as isNodeEnv } from './is-node-env'
export { default as isNumber } from './is-number'
14 changes: 14 additions & 0 deletions src/guards/is-number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @file Type Guards - isNumber
* @module tutils/guards/isNumber
*/

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

export default isNumber

0 comments on commit d64f34c

Please sign in to comment.