Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(isBoolean) Add loose option to isBoolean validator (#1676)
* Add loose option to isBoolean validator * Move boolean array definitions outside of function
- Loading branch information
Bryan Brophy
committed
Jul 16, 2021
1 parent
c87956a
commit 044159d
Showing
3 changed files
with
43 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
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,6 +1,15 @@ | ||
import assertString from './util/assertString'; | ||
|
||
export default function isBoolean(str) { | ||
const defaultOptions = { loose: false }; | ||
const strictBooleans = ['true', 'false', '1', '0']; | ||
const looseBooleans = [...strictBooleans, 'yes', 'no']; | ||
|
||
export default function isBoolean(str, options = defaultOptions) { | ||
assertString(str); | ||
return (['true', 'false', '1', '0'].indexOf(str) >= 0); | ||
|
||
if (options.loose) { | ||
return looseBooleans.includes(str.toLowerCase()); | ||
} | ||
|
||
return strictBooleans.includes(str); | ||
} |
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