Skip to main content

Boolean

One coercion helper. toBoolean understands the string forms 'true', 'false', '1' and '0' that Boolean() gets wrong for env vars and query params — Boolean('false') is true, which is almost never what a config parser wants.

The named logical operators (and, or, not, xor) were removed in 4.0; use &&, ||, ! and !== directly.

Example

import { toBoolean } from '@rtorcato/js-common/boolean'

// Env vars and query params arrive as strings, where `Boolean('false')` is `true`.
toBoolean(process.env.FEATURE_FLAG) // 'false' -> false, '1' -> true
toBoolean('') // false

Import

import { toBoolean } from '@rtorcato/js-common/boolean'

Exports

NameSummary
toBooleanConverts a value to a boolean.

See also

  • validation — type guards, including isBoolean
  • numbers — sum, average, clamp, roundTo
  • strings — slugify, truncate, casing, emoji stripping