parse-string-boolean

Parse a string representation of a boolean.

Usage no npm install needed!

<script type="module">
  import parseStringBoolean from 'https://cdn.skypack.dev/parse-string-boolean';
</script>

README

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