Convert a boolean from a string, keeping undefined and null values
- Parses "true" as
true
- Parses "false" as
false
- Case-insensitive
- Ignores leading and trailing whitespace
- Parses
undefined
andnull
asundefined
- Returns
undefined
when a boolean could not be found - Configurable
defaultValue
(replacesundefined
)
npm install boobool
boobool(string[, {defaultValue}])
const boobool = require('boobool');
boobool('true'); //-> true
boobool(' TRUE '); //-> true
boobool('false'); //-> false
boobool('yes'); //-> undefined
boobool('1'); //-> undefined
boobool(''); //-> undefined
boobool(null); //-> undefined
boobool(undefined); //-> undefined
Optionally, you can override the default value:
boobool('', {defaultValue: true}); //-> true