Skip to content

Commit

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

import type { Numeric } from '#src/types'
import type testSubject from '../is-numeric'

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

import testSubject from '../is-numeric'

describe('unit:guards/isNumeric', () => {
it('should return false if value is not a numeric', () => {
expect(testSubject(faker.string.sample())).to.be.false
})

it('should return true if value is a numeric', () => {
expect(testSubject(faker.string.numeric())).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 @@ -17,5 +17,6 @@ export { default as isNIL } from './is-nil'
export { default as isNodeEnv } from './is-node-env'
export { default as isNull } from './is-null'
export { default as isNumber } from './is-number'
export { default as isNumeric } from './is-numeric'
export { default as isString } from './is-string'
export { default as isUndefined } from './is-undefined'
22 changes: 22 additions & 0 deletions src/guards/is-numeric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @file Type Guards - isNumeric
* @module tutils/guards/isNumeric
*/

import type { Numeric } from '#src/types'
import isNumber from './is-number'
import isString from './is-string'

/**
* Checks if the given `value` is a numeric.
*
* A numeric is a string that contains only numbers.
*
* @param {unknown} value - Value to evaluate
* @return {value is Numeric} `true` if `value` is a {@linkcode Numeric}
*/
const isNumeric = (value: unknown): value is Numeric => {
return isString(value) && isNumber(+value)
}

export default isNumeric

0 comments on commit 10e807f

Please sign in to comment.