-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
allow_leading_zeroes
option to isInt()
- Loading branch information
1 parent
f624d46
commit a3e3880
Showing
2 changed files
with
39 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,23 @@ | ||
import assertString from './util/assertString'; | ||
|
||
const int = /^[-+]?[0-9]+$/; | ||
const int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/; | ||
const intLeadingZeroes = /^[-+]?[0-9]+$/; | ||
|
||
export default function isInt(str, options) { | ||
assertString(str); | ||
options = options || {}; | ||
return int.test(str) && (!options.hasOwnProperty('min') || | ||
str >= options.min) && (!options.hasOwnProperty('max') || str <= options.max); | ||
|
||
// Get the regex to use for testing, based on whether | ||
// leading zeroes are allowed or not. | ||
let regex = ( | ||
options.hasOwnProperty('allow_leading_zeroes') && options.allow_leading_zeroes ? | ||
intLeadingZeroes : | ||
int | ||
); | ||
|
||
// Check min/max | ||
let minCheckPassed = (!options.hasOwnProperty('min') || str >= options.min); | ||
let maxCheckPassed = (!options.hasOwnProperty('max') || str <= options.max); | ||
|
||
return regex.test(str) && minCheckPassed && maxCheckPassed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters