Skip to content

@absoluteui/helpers/boolean

Lenient boolean parsing, logic operators, and accessible labels.

import { ... } from '@absoluteui/helpers/boolean';

Parse yes/no values from text, combine rule checks, and label booleans for display. Use it where flags arrive as strings (CSV imports, query strings, environment variables, legacy APIs) or where a decision depends on several conditions. The Helpers and services guide covers the shared conventions.

How do I read yes/no values from an import or configuration?

Section titled “How do I read yes/no values from an import or configuration?”

parseBoolean accepts true/false, yes/no, y/n, on/off, and 1/0 (trimmed, any case) and returns undefined for anything else, so a typo is reported instead of read as false. Add words for other languages with trueValues and falseValues.

import { parseBoolean } from '@absoluteui/helpers/boolean';
const cells = [' Yes', 'N', 'ja', 'nein', 'maybe'];
cells.map((c) => parseBoolean(c, { trueValues: ['ja'], falseValues: ['nein'] })); // => [true, false, true, false, undefined]
parseBoolean('yes', { strict: true }); // => undefined
parseBoolean('TRUE', { strict: true }); // => true

How do I combine the checks behind a decision?

Section titled “How do I combine the checks behind a decision?”

all, any, and none accept booleans or functions; functions are evaluated lazily and stop at the first decisive result, so expensive checks run only when needed. countTrue and majority summarise several answers.

import { all, countTrue, implies, majority } from '@absoluteui/helpers/boolean';
const request = { days: 12, hasBalance: true, managerApproved: true, hrApproved: false };
const canAutoApprove = all([
request.hasBalance,
request.managerApproved,
() => implies(request.days > 10, request.hrApproved), // long leave also needs HR
]);
canAutoApprove; // => false
const panel = [true, false, true];
[countTrue(panel), majority(panel)]; // => [2, true]

How do I show booleans in a grid or an export?

Section titled “How do I show booleans in a grid or an export?”

booleanToLabel and booleanToYesNo map true, false, and a missing value to labels you choose, so null does not silently appear as “No”.

import { booleanToLabel, booleanToYesNo } from '@absoluteui/helpers/boolean';
const remote: (boolean | null)[] = [true, false, null];
remote.map((v) => booleanToLabel(v, { true: 'Remote', false: 'On site', null: 'Not set' })); // => ['Remote', 'On site', 'Not set']
booleanToYesNo(true, { true: 'Ja', false: 'Nein' }); // => 'Ja'

all

True when every item is true (thunks are evaluated lazily, short-circuiting). Empty -> true.

function all(values: Iterable<BooleanLike>): boolean;

Example:

all([true, () => 1 < 2]) // => true

any

True when at least one item is true (short-circuiting). Empty -> false.

function any(values: Iterable<BooleanLike>): boolean;

Example:

any([false, true]) // => true

booleanToLabel

Text label for a boolean (never an icon alone; pass localized labels). The default labels are the locale-neutral JSON literals 'true'/'false', and '' for nullish.

function booleanToLabel(value: boolean | null | undefined, labels?: BooleanLabels): string;

Example:

booleanToLabel(true, { true: 'Active', false: 'Inactive', null: 'Unknown' }) // => 'Active'

booleanToYesNo

'Yes'/'No' (English defaults; pass localized labels for other languages), '' for nullish.

function booleanToYesNo(value: boolean | null | undefined, labels?: BooleanLabels): string;

Example:

booleanToYesNo(false) // => 'No'

Example:

booleanToYesNo(true, { true: 'Oui', false: 'Non' }) // => 'Oui'

boolToInt

1 for true, 0 for false.

function boolToInt(value: boolean): 0 | 1;

Example:

boolToInt(true) // => 1

countTrue

Counts true items, or items passing predicate.

function countTrue<T = boolean>(values: Iterable<T>, predicate?: (value: T) => boolean): number;

Example:

countTrue([true, false, true]) // => 2

implies

Material implication a → b.

function implies(a: boolean, b: boolean): boolean;

Example:

implies(true, false) // => false

isFalsy

JavaScript falsiness.

function isFalsy(value: unknown): boolean;

Example:

isFalsy(0) // => true

isTruthy

JavaScript truthiness as a type-friendly predicate.

function isTruthy<T>(value: T): value is Exclude<T, false | 0 | '' | null | undefined | 0n>;

Example:

[0, 1, ''].filter(isTruthy) // => [1]

majority

True when strictly more than half of the items are true. Empty or tied -> false.

function majority(values: Iterable<boolean>): boolean;

Example:

majority([true, true, false]) // => true

nand

Not and.

function nand(a: boolean, b: boolean): boolean;

Example:

nand(true, true) // => false

none

True when no item is true (short-circuiting). Empty -> true.

function none(values: Iterable<BooleanLike>): boolean;

Example:

none([false, false]) // => true

nor

Not or.

function nor(a: boolean, b: boolean): boolean;

Example:

nor(false, false) // => true

parseBoolean

Parses true/false/yes/no/y/n/on/off/1/0 (case-insensitive, trimmed), booleans and the numbers 1/0. Returns undefined for anything else.

function parseBoolean(value: unknown, options?: ParseBooleanOptions): boolean | undefined;

Example:

parseBoolean(' Yes ') // => true

Example:

parseBoolean('maybe') // => undefined

toggle

Negation.

function toggle(value: boolean): boolean;

Example:

toggle(false) // => true

xnor

Equivalence (not xor).

function xnor(a: boolean, b: boolean): boolean;

Example:

xnor(true, true) // => true

xor

Exclusive or.

function xor(a: boolean, b: boolean): boolean;

Example:

xor(true, false) // => true

BooleanLabels

Labels for booleanToLabel.

interface BooleanLabels {
readonly true: string;
readonly false: string;
readonly null?: string;
}

ParseBooleanOptions

Boolean helpers (docs/PLAN-HELPERS.md the specification boolean): lenient parsing, logic operators, counting and text labels.

interface ParseBooleanOptions {
readonly strict?: boolean;
readonly trueValues?: readonly string[];
readonly falseValues?: readonly string[];
}

BooleanLike

A boolean or a lazily evaluated boolean.

type BooleanLike = boolean | (() => boolean);