Skip to content

Commit

Permalink
refactor(utils)!: [isNumber] check typeof value
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Aug 10, 2023
1 parent 9bec8ac commit ac17bfd
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
7 changes: 5 additions & 2 deletions src/utils/__tests__/is-number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ describe('unit:utils/isNumber', () => {
const cases: Parameters<typeof testSubject>[] = [
[[INTEGER]],
[Symbol('is-number')],
[Number.NaN],
[faker.string.numeric()]
]

Expand All @@ -23,7 +22,11 @@ describe('unit:utils/isNumber', () => {

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

// Act + Expect
cases.forEach(([value]) => expect(testSubject(value)).to.be.true)
Expand Down
4 changes: 2 additions & 2 deletions src/utils/is-nan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @module tutils/utils/isNaN
*/

import equal from './equal'
import isNumber from './is-number'

/**
* Checks if `value` is {@linkcode Number.NaN}.
Expand All @@ -13,6 +13,6 @@ import equal from './equal'
* @param {unknown} value - Value to check
* @return {boolean} `true` if `value` is `Number.NaN`
*/
const isNaN = (value: unknown): boolean => equal(Number.NaN, value)
const isNaN = (value: unknown): boolean => isNumber(value) && value !== +value

export default isNaN
8 changes: 1 addition & 7 deletions src/utils/is-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
* @param {unknown} value - Value to check
* @return {value is number} `true` if `value` is a number
*/
const isNumber = (value: unknown): value is number => {
try {
return Number(value) === value
} catch {
return false
}
}
const isNumber = (value: unknown): value is number => typeof value === 'number'

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

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

Expand All @@ -19,7 +20,7 @@ import isString from './is-string'
* @return {value is Numeric} `true` if `value` is a numeric
*/
const isNumeric = (value: unknown): value is Numeric => {
return isString(value) && isNumber(+value)
return isString(value) && isNumber((value = +value)) && !isNaN(value)
}

export default isNumeric

0 comments on commit ac17bfd

Please sign in to comment.