parse-string-boolean
v1.0.1
Published
Parse a string representation of a boolean.
Downloads
814
Maintainers
Readme
parse-string-boolean
Parse a string representation of a boolean.
- Parses "true" as
true
- Parses "false" as
false
- Case-insensitive
- Ignores leading and trailing whitespace
- Returns
null
(customizable) if the string does not represent a boolean - Throws an error if input is not a string
Installation
Node.js >= 6
is required. To install, type this at the command line:
npm install parse-string-boolean
Usage
parseBoolean(string[, defaultValue])
const parseBoolean = require('parse-string-boolean');
parseBoolean('true'); //-> true
parseBoolean(' TRUE '); //-> true
parseBoolean('false'); //-> false
parseBoolean('yes'); //-> null
parseBoolean('1'); //-> null
parseBoolean(''); //-> null
Optionally, you can override the default value for strings that do not represent a boolean:
parseBoolean('', true); //-> true