Bool — Boolean Utilities
Working with boolean logic in standard JavaScript and TypeScript frequently leads to nested ternary
expressions, repetitive inline arrow functions inside pipelines, and awkward conversions between
boolean flags and structured data types like Maybe and Result.
Simple pipeline steps — such as combining feature flags, evaluating permissions, parsing query
parameter flags, or conditionally lifting a value into a safe container — often force developers to
write imperative if/else statements or inline lambdas like b => !b and
condition ? Maybe.make.some(data) : Maybe.make.none().
Bool provides curried, data-last boolean algebra, lazy short-circuiting operators, exhaustive
pattern matching, and type-safe conversion bridges designed specifically for pipelines.
The problem with boolean handling in pipelines
Section titled “The problem with boolean handling in pipelines”Consider a service verifying whether a user is allowed to access a protected workspace feature:
While simple, this imperative pattern breaks down when integrated into linear pipelines:
- Combining multiple boolean conditions requires temporary variables or inline closures.
- In-line ternaries like
condition ? computeExpensive() : fallbackcannot easily be composed or reused across pipe steps. - Parsing string flags from query parameters (e.g.
"?debug=true") usingBoolean("false")producestrue— a classic JavaScript coercion trap. - Converting a truthy condition into a
Maybe<T>orResult<E, T>requires manual ternaries that obscure business logic.
Bool solves these challenges by treating booleans as first-class values with composable algebra
and safe boundaries.
Type guards and truthiness narrowing
Section titled “Type guards and truthiness narrowing”The Bool.is namespace provides type guards for runtime verification and compile-time narrowing:
Bool.is.truthy excludes false, 0, -0, 0n, "", null, undefined, and NaN, giving
TypeScript a reliable filter predicate for arrays and streams.
Curried boolean algebra
Section titled “Curried boolean algebra”All logical operators in Bool are data-last, meaning the data being evaluated is placed last. This
enables clean, point-free composition inside pipe:
Lazy evaluation and short-circuiting
Section titled “Lazy evaluation and short-circuiting”When combining conditions where the second evaluation is computationally expensive, use
Bool.andLazy and Bool.orLazy. The secondary thunk will only run if required:
Array aggregations
Section titled “Array aggregations”To aggregate collections of booleans, Bool.all and Bool.any evaluate an array with
short-circuiting:
Branching and pattern matching
Section titled “Branching and pattern matching”Rather than relying on nested ternaries, Bool offers declarative catamorphic branching via fold
and exhaustive pattern matching via match.
Positional branching with fold
Section titled “Positional branching with fold”Following Pipelined’s standard catamorphism convention (falsy/err case first, truthy/ok case
second), Bool.fold takes (onFalse, onTrue):
Named-case matching with match
Section titled “Named-case matching with match”Bool.match matches on { true, false } cases, making branching explicit and readable:
Safe parsing and conversions
Section titled “Safe parsing and conversions”Intake with Bool.from
Section titled “Intake with Bool.from”JavaScript coercion quirks are avoided through explicit parsing functions:
Exporting with Bool.to
Section titled “Exporting with Bool.to”Convert boolean values into primitive representations:
Lifting booleans into Maybe and Result
Section titled “Lifting booleans into Maybe and Result”One of the most frequent patterns in functional TypeScript is converting a boolean check into a container:
Both Bool.to.Maybe and Bool.to.Result evaluate their value thunks lazily, ensuring expensive
object constructions or string interpolations only execute when the condition holds.
Composing boolean pipelines
Section titled “Composing boolean pipelines”Combining these primitives allows complex access-control and validation logic to be modeled as a single, readable pipeline:
Problems it solves
Section titled “Problems it solves”- Eliminating nested ternary chains: Replaces complex
a ? (b ? c : d) : eexpressions with readableBool.and,Bool.or,Bool.fold, andBool.matchcombinators. - Preventing JavaScript boolean parsing bugs: Using standard
Boolean("false")evaluates totruein JavaScript.Bool.from.stringreliably parses"true"and"false"intoMaybe<boolean>, returningNonefor invalid strings. - Short-circuiting in pipelines without imperative
ifblocks:Bool.andLazyandBool.orLazyprevent executing secondary expensive operations unless the prior condition warrants it. - Streamlined bridges to
MaybeandResult:Bool.to.MaybeandBool.to.Resultturn validation flags into structured error-handling types point-free and lazily. - Reliable truthiness array filtering:
Bool.is.truthyprovides a type-safe narrowing guard for removing falsy values from collections without manual type assertions.