Bool — Boolean Utilities
1.88 kB minified + gzipped
Working with boolean logic in standard JavaScript and TypeScript frequently leads to nested ternary expressions, repetitive inline arrow functions inside pipelines, and subtle coercion bugs:
When integrating boolean conditions into functional pipelines, four friction points appear repeatedly:
- JavaScript string coercion traps: Passing
"false"or"0"toBoolean(...)evaluates totruebecause any non-empty string is truthy. - Inline negation and combinator noise: Negating or combining flags inside
piperequires inline lambdas likeb => !borb => b && isEnabled. - Eager evaluation of expensive checks: Passing
Bool.and(checkPermissions())evaluatescheckPermissions()immediately, even when the upstream boolean is alreadyfalse. - Manual ternaries to enter
MaybeorResult: Turning a boolean validation check into aMaybe<A>orResult<E, A>requires writingisValid ? Result.make.ok(val) : Result.make.err(err)by hand.
Bool solves these challenges by providing curried, data-last boolean algebra, lazy
short-circuiting operators, exhaustive named-case matching, and direct conversion bridges to Maybe
and Result.
Type Guards and Truthiness Narrowing
Section titled “Type Guards and Truthiness Narrowing”The Bool.is namespace provides type guards for runtime checks and compile-time type narrowing:
Bool.is.truthy narrows a type T by excluding false, 0, 0n, "", null, and undefined,
while Bool.is.falsy narrows unknown inputs to those falsy primitive values.
Curried Boolean Algebra
Section titled “Curried Boolean Algebra”All logical operators in Bool are data-last, placing the boolean condition being piped as the
final argument:
Lazy evaluation and short-circuiting
Section titled “Lazy evaluation and short-circuiting”When the second condition in an AND or OR check involves an expensive computation, use
Bool.andLazy and Bool.orLazy. The callback thunk is only invoked if the upstream boolean cannot
decide the outcome on its own:
Array aggregations (all and any)
Section titled “Array aggregations (all and any)”To combine an array of booleans into a single result, Bool.all and Bool.any evaluate the list
from left to right and short-circuit as soon as the answer is known:
Exhaustive Branching with match
Section titled “Exhaustive Branching with match”Instead of writing ternary expressions inside a pipeline, Bool.match branches on named
{ true, false } handlers:
Safe Parsing and Conversions
Section titled “Safe Parsing and Conversions”Parsing with Bool.from
Section titled “Parsing with Bool.from”To parse environment variables, query flags, or numeric bit flags without falling victim to
JavaScript’s truthiness coercion, Bool.from.string and Bool.from.number return Maybe<boolean>:
Formatting and lifting with Bool.to
Section titled “Formatting and lifting with Bool.to”Bool.to.number and Bool.to.string convert booleans to literal 1 | 0 or "true" | "false":
When a boolean check gates access to a value or decides whether a pipeline succeeds or fails,
Bool.to.Maybe and Bool.to.Result convert the boolean directly into a container using lazy
callbacks:
Pipeline Composition
Section titled “Pipeline Composition”Combining parsing, boolean algebra, and container lifting lets you express feature-flag and authorization checks as a linear pipeline:
Problems it solves
Section titled “Problems it solves”- Eliminating nested ternary chains: Replaces
a ? (b ? c : d) : eexpressions with readableBool.and,Bool.or,Bool.xor, andBool.matchcombinators. - Preventing JavaScript boolean parsing bugs: Using standard
Boolean("false")evaluates totruein JavaScript.Bool.from.stringandBool.from.numberstrictly parse"true"/"false"and1/0intoMaybe<boolean>, returningNonefor unexpected inputs. - Short-circuiting in pipelines without imperative
ifblocks:Bool.andLazyandBool.orLazyavoid executing secondary expensive checks unless the upstream boolean requires it. - Lazy bridges to
MaybeandResult:Bool.to.MaybeandBool.to.Resultturn boolean conditions into structured containers inside pipelines while only evaluating their value or error thunks when needed. - Type-safe truthiness and falsiness filtering:
Bool.is.truthyandBool.is.falsyprovide narrowing type guards for filtering collections without manual type assertions.