UNPKG

3.45 kBMarkdownView Raw
1# boolean
2
3boolean converts lots of things to boolean.
4
5## Status
6
7| Category | Status |
8| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
9| Version | [![npm](https://img.shields.io/npm/v/boolean)](https://www.npmjs.com/package/boolean) |
10| Dependencies | ![David](https://img.shields.io/david/thenativeweb/boolean) |
11| Dev dependencies | ![David](https://img.shields.io/david/dev/thenativeweb/boolean) |
12| Build | ![GitHub Actions](https://github.com/thenativeweb/boolean/workflows/Release/badge.svg?branch=main) |
13| License | ![GitHub](https://img.shields.io/github/license/thenativeweb/boolean) |
14
15## Installation
16
17```shell
18$ npm install boolean
19```
20
21## Quick start
22
23First you need to add a reference to boolean in your application:
24
25```javascript
26const { boolean, isBooleanable } = require('boolean');
27```
28
29If you use TypeScript, use the following code instead:
30
31```typescript
32import { boolean, isBooleanable } from 'boolean';
33```
34
35To verify a value for its boolean value, call the `boolean` function and provide the value in question as parameter:
36
37```javascript
38console.log(boolean('true')); // => true
39```
40
41The `boolean` function considers the following values to be equivalent to `true`:
42
43- `true` (boolean)
44- `'true'` (string)
45- `'TRUE'` (string)
46- `'t'` (string)
47- `'T'` (string)
48- `'yes'` (string)
49- `'YES'` (string)
50- `'y'` (string)
51- `'Y'` (string)
52- `'on'` (string)
53- `'ON'` (string)
54- `'1'` (string)
55- `1` (number)
56
57In addition to the primitive types mentioned above, boolean also supports their object wrappers `Boolean`, `String`, and `Number`.
58
59_Please note that if you provide a `string` or a `String` object, it will be trimmed._
60
61All other values, including `undefined` and `null` are considered to be `false`.
62
63### Figuring out whether a value can be considered to be boolean
64
65From time to time, you may not want to directly convert a value to its boolean equivalent, but explicitly check whether it looks like a boolean. E.g., although `boolean('F')` returns `false`, the string `F` at least looks like a boolean, in contrast to something such as `123` (for which `boolean(123)` would also return `false`).
66
67To figure out whether a value can be considered to be a boolean, use the `isBooleanable` function:
68
69```javascript
70console.log(isBooleanable('true')); // => true
71```
72
73The `isBooleanable` function considers all of the above mentioned values to be reasonable boolean values, and additionally, also the following ones:
74
75- `false` (boolean)
76- `'false'` (string)
77- `'FALSE'` (string)
78- `'f'` (string)
79- `'F'` (string)
80- `'no'` (string)
81- `'NO'` (string)
82- `'n'` (string)
83- `'N'` (string)
84- `'off'` (string)
85- `'OFF'` (string)
86- `'0'` (string)
87- `0` (number)
88
89## Running quality assurance
90
91To run quality assurance for this module use [roboter](https://www.npmjs.com/package/roboter):
92
93```shell
94$ npx roboter
95```