0.0.1 • Published 11 months ago

@liplum/str2bool v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

str2bool.js

Convert a string to boolean value.

Installation

yarn add @liplum/str2bool
# or
npm i @liplum/str2bool
# or
pnpm i @liplum/str2bool

Usage

Basic usage

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

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 string

The following strings will be considered as false:

  • "" (empty string)
  • "false"
  • "0"

In strict mode, any non-empty string like "javascript" and "node.js" will result in undefined.

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:

export interface Str2BoolOptions {
  /**
   * The string will be trimed before being converted.
   * 
   * true by default.
   */
  trim?: boolean
  /**
   * 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
  /**
   * 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[]
}
0.0.1

11 months ago