From d64f34c71f5b745d0218d4854ac476c7146964e9 Mon Sep 17 00:00:00 2001 From: Lexus Drumgold Date: Wed, 1 Feb 2023 14:22:14 -0500 Subject: [PATCH] feat(guards): `isNumber` Signed-off-by: Lexus Drumgold --- src/guards/__tests__/is-number.spec-d.ts | 12 +++++++++ src/guards/__tests__/is-number.spec.ts | 31 ++++++++++++++++++++++++ src/guards/index.ts | 1 + src/guards/is-number.ts | 14 +++++++++++ 4 files changed, 58 insertions(+) create mode 100644 src/guards/__tests__/is-number.spec-d.ts create mode 100644 src/guards/__tests__/is-number.spec.ts create mode 100644 src/guards/is-number.ts diff --git a/src/guards/__tests__/is-number.spec-d.ts b/src/guards/__tests__/is-number.spec-d.ts new file mode 100644 index 00000000..c7b45953 --- /dev/null +++ b/src/guards/__tests__/is-number.spec-d.ts @@ -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().guards.toBeNumber() + }) +}) diff --git a/src/guards/__tests__/is-number.spec.ts b/src/guards/__tests__/is-number.spec.ts new file mode 100644 index 00000000..704251c2 --- /dev/null +++ b/src/guards/__tests__/is-number.spec.ts @@ -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[] = [ + [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[] = [ + [faker.number.float()], + [faker.number.int()] + ] + + // Act + Expect + cases.forEach(([value]) => expect(testSubject(value)).to.be.true) + }) +}) diff --git a/src/guards/index.ts b/src/guards/index.ts index 48413c26..443002eb 100644 --- a/src/guards/index.ts +++ b/src/guards/index.ts @@ -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' diff --git a/src/guards/is-number.ts b/src/guards/is-number.ts new file mode 100644 index 00000000..0a604530 --- /dev/null +++ b/src/guards/is-number.ts @@ -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