Skip to content

Commit

Permalink
feat(checks): add isNumericInt/Float() checks
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 7, 2021
1 parent af5d943 commit 7e054c1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/checks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export * from "./is-node";
export * from "./is-not-string-iterable";
export * from "./is-null";
export * from "./is-number";
export * from "./is-numeric";
export * from "./is-object";
export * from "./is-odd";
export * from "./is-plain-object";
Expand Down
16 changes: 16 additions & 0 deletions packages/checks/src/is-numeric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Returns true if given string contains only digits, and optionally, a sign
* prefix.
*
* @param x
*/
export const isNumericInt = (x: string) => /^[-+]?\d+$/.test(x);

/**
* Returns true if given string only contains an integer or floating point
* number, optionally in scientific notiation (e.g. `-123.45e-6`).
*
* @param x
*/
export const isNumericFloat = (x: string) =>
/^[-+]?\d*\.?\d+(e[-+]?\d+)?$/i.test(x);

0 comments on commit 7e054c1

Please sign in to comment.