From 10e807f15b26dc9b7034bc312bb21fa4233362db Mon Sep 17 00:00:00 2001 From: Lexus Drumgold Date: Wed, 1 Feb 2023 14:54:20 -0500 Subject: [PATCH] feat(guards): `isNumeric` Signed-off-by: Lexus Drumgold --- src/guards/__tests__/is-numeric.spec-d.ts | 13 +++++++++++++ src/guards/__tests__/is-numeric.spec.ts | 16 ++++++++++++++++ src/guards/index.ts | 1 + src/guards/is-numeric.ts | 22 ++++++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 src/guards/__tests__/is-numeric.spec-d.ts create mode 100644 src/guards/__tests__/is-numeric.spec.ts create mode 100644 src/guards/is-numeric.ts diff --git a/src/guards/__tests__/is-numeric.spec-d.ts b/src/guards/__tests__/is-numeric.spec-d.ts new file mode 100644 index 00000000..b0302f92 --- /dev/null +++ b/src/guards/__tests__/is-numeric.spec-d.ts @@ -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().guards.toEqualTypeOf() + }) +}) diff --git a/src/guards/__tests__/is-numeric.spec.ts b/src/guards/__tests__/is-numeric.spec.ts new file mode 100644 index 00000000..17acb97d --- /dev/null +++ b/src/guards/__tests__/is-numeric.spec.ts @@ -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 + }) +}) diff --git a/src/guards/index.ts b/src/guards/index.ts index 68752ffc..3dda0fda 100644 --- a/src/guards/index.ts +++ b/src/guards/index.ts @@ -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' diff --git a/src/guards/is-numeric.ts b/src/guards/is-numeric.ts new file mode 100644 index 00000000..4b21554c --- /dev/null +++ b/src/guards/is-numeric.ts @@ -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