Skip to content

Commit

Permalink
Merge pull request #268 from piman51277/patch-1
Browse files Browse the repository at this point in the history
Optimize isNumber util func
  • Loading branch information
danocmx authored Jul 14, 2023
2 parents 08b7239 + e082730 commit f53befe
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/util/isNumber.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export default function (str: string | number): str is number {
// Although it doesn't check for decimals, it is not required here.
return typeof str === 'number' || /^\d+$/.test(str);
/** Why this works:
* 1. Any non-NaN number is not NaN
* 2. isNaN considers any numeric string to be not NaN
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN
* for further reference
*/
return !isNaN(str as number);
}

0 comments on commit f53befe

Please sign in to comment.