parse-string-boolean

1.0.1 • Public • Published

parse-string-boolean NPM Version Build Status Dependency Monitor

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

Package Sidebar

Install

npm i parse-string-boolean

Weekly Downloads

170

Version

1.0.1

License

MIT

Unpacked Size

5.42 kB

Total Files

5

Last publish

Collaborators

  • stevenvachon