Skip to content

Latest commit

 

History

History
55 lines (40 loc) · 1.43 KB

File metadata and controls

55 lines (40 loc) · 1.43 KB
name boolean
description Semantic boolean coercion for stringly-typed inputs such as environment variables and form values.
context common

boolean

Semantic boolean coercion with exact parity to the boolean npm package. Converts environment variable strings, form values, and other stringly-typed inputs to boolean using a well-defined set of truthy tokens.

API

function toBoolean(value: unknown): boolean;

Converts any value to boolean:

  • Strings (case-insensitive, trimmed): "true", "t", "yes", "y", "on", "1"true; everything else → false
  • Numbers: 1true; all other numbers → false
  • Booleans: .valueOf() result
  • Anything else: false
function isTruthy(value: unknown): boolean;

Readable alias for toBoolean(value). Intended for use in array predicates.

function isFalsy(value: unknown): boolean;

Readable inverse: returns !toBoolean(value).

Usage

import { toBoolean, isTruthy, isFalsy } from "@webiny/stdlib";

toBoolean("yes"); // true
toBoolean("no"); // false
toBoolean("1"); // true
toBoolean("2"); // false
toBoolean(1); // true
toBoolean(0); // false
toBoolean(true); // true

const flags = ["on", "off", "true", "false"];
flags.filter(isTruthy); // ["on", "true"]
flags.filter(isFalsy); // ["off", "false"]

// Typical env var usage
const debug = isTruthy(process.env.DEBUG);