Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/liplum/str2bool.js
Convert a string to boolean
https://github.com/liplum/str2bool.js
javascript npm typescript
Last synced: 23 days ago
JSON representation
Convert a string to boolean
- Host: GitHub
- URL: https://github.com/liplum/str2bool.js
- Owner: liplum
- Created: 2024-08-28T12:38:12.000Z (2 months ago)
- Default Branch: master
- Last Pushed: 2024-09-07T16:04:53.000Z (2 months ago)
- Last Synced: 2024-10-01T16:16:15.975Z (about 1 month ago)
- Topics: javascript, npm, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@liplum/str2bool
- Size: 47.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# str2bool.js
Convert a string to boolean value.
## Installation
```sh
yarn add @liplum/str2bool
# or
npm i @liplum/str2bool
# or
pnpm i @liplum/str2bool
```## Usage
Basic usage
```js
import str2bool from "@liplum/str2bool"console.log(str2bool("true") === true)
console.log(str2bool("false") === false)console.log(str2bool("") === false)
console.log(str2bool("0") === false)
console.log(str2bool("1") === true)
```Yes or No
```js
console.log(str2bool("yes",{ yesOrNo: true }) === true)
console.log(str2bool("y",{ yesOrNo: true }) === true)console.log(str2bool("no",{ yesOrNo: true }) === false)
console.log(str2bool("n",{ yesOrNo: true }) === false)
```The following strings will be considered as `true`:
- "true"
- "1", "-1", "0.1" and other strings can be converted to a number that isn't zero
- [Non-strict mode] "javascript", "node.js", and any non-empty stringThe following strings will be considered as `false`:
- "" (empty string)
- "false"
- "0"[strict mode] The following strings will be considered as `undefined`:
- any non-empty string like "javascript" and "node.js"
If `trim` is true, the strings containing only whitespace characters will be trimmed to an empty string,
and be considered as `false`.If `truthy` or `falsy` are given,
then they will be tested after the above cases.Configure the options:
```ts
export interface Str2BoolOptions {
/**
* In strict mode, strings that cannot be considered either `true` or `false` result in `undefined`.
* In non-strict mode, any other non-empty strings will be consider as `true`.
*
* false by default.
*/
strict?: boolean
/**
* The string will be trimmed before being converted.
*
* true by default.
*/
trim?: boolean
/**
* Ignore the case.
*
* true by default.
*/
ignoreCase?: boolean/**
* If enabled, "yes" and "y" will be considered as `true`,
* while "no" and "n" will be considered as `false`.
*
* false by default.
*/
yesOrNo?: boolean/**
* The strings will be considered as `true`
*/
truthy?: string[]
/**
* The strings will be considered as `false`
*/
falsy?: string[]
}
```