https://github.com/stevenvachon/parse-string-boolean
Parse a string representation of a boolean.
https://github.com/stevenvachon/parse-string-boolean
boolean html nodejs
Last synced: 10 months ago
JSON representation
Parse a string representation of a boolean.
- Host: GitHub
- URL: https://github.com/stevenvachon/parse-string-boolean
- Owner: stevenvachon
- License: mit
- Created: 2018-02-27T20:38:46.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-06-02T09:08:01.000Z (over 5 years ago)
- Last Synced: 2025-03-20T04:03:18.497Z (10 months ago)
- Topics: boolean, html, nodejs
- Language: JavaScript
- Homepage:
- Size: 27.3 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# parse-string-boolean [![NPM Version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Monitor][greenkeeper-image]][greenkeeper-url]
> 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](http://nodejs.org/) `>= 6` is required. To install, type this at the command line:
```shell
npm install parse-string-boolean
```
## Usage
`parseBoolean(string[, defaultValue])`
```js
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:
```js
parseBoolean('', true); //-> true
```
[npm-image]: https://img.shields.io/npm/v/parse-string-boolean.svg
[npm-url]: https://npmjs.com/package/parse-string-boolean
[travis-image]: https://img.shields.io/travis/stevenvachon/parse-string-boolean.svg
[travis-url]: https://travis-ci.org/stevenvachon/parse-string-boolean
[greenkeeper-image]: https://badges.greenkeeper.io/stevenvachon/parse-string-boolean.svg
[greenkeeper-url]: https://greenkeeper.io/