Packages@accelint/coreLogical
or
Logical disjunction (||) for values and function results
Usage
import { or, orFn, swappedOr, swappedOrFn } from '@accelint/core';
// Value-based disjunction
or(true)(0);
// true
or(false)(false);
// false
// Function-based disjunction
const isEmpty = (s: string) => s.length === 0;
const isWhitespace = (s: string) => s.trim().length === 0;
orFn(isEmpty)(isWhitespace)(" ");
// true
// Swapped variants evaluate right operand first
swappedOr(0)(true);
// trueReference
or
function or<A>(a: A): <B>(b: B) => booleanParameters
| Parameter | Type | Description |
|---|---|---|
a | A | The first value to evaluate |
Returns
Returns a curried function that accepts a second value and returns true if either value is truthy, false otherwise.
orFn
function orFn<T, A>(
a: (x: T) => A
): <B>(b: (y: T) => B) => (c: T) => booleanParameters
| Parameter | Type | Description |
|---|---|---|
a | (x: T) => A | The first function to evaluate |
Returns
Returns a curried function that accepts a second function, then accepts a value to pass to both functions, returning true if either function result is truthy, false otherwise.
swappedOr
function swappedOr<A>(a: A): <B>(b: B) => booleanEvaluates b || a instead of a || b.
Parameters
| Parameter | Type | Description |
|---|---|---|
a | A | The fallback value |
Returns
Returns a curried function that accepts a value and returns true if that value OR the first value is truthy.
swappedOrFn
function swappedOrFn<T, A>(
a: (x: T) => A
): <B>(b: (y: T) => B) => (c: T) => booleanEvaluates b(x) || a(x) instead of a(x) || b(x).
Examples
Example: Default values
import { or } from '@accelint/core';
const getUserName = (user: { name?: string }) =>
or(user.name)("Anonymous");
// Returns user.name if truthy, otherwise "Anonymous"Example: Combining validation rules
import { orFn } from '@accelint/core';
const isAdmin = (user: User) => user.role === 'admin';
const isModerator = (user: User) => user.role === 'moderator';
const canModerate = orFn(isAdmin)(isModerator);
canModerate({ role: 'admin' });
// true
canModerate({ role: 'user' });
// falseExample: Swapped evaluation order
import { swappedOr } from '@accelint/core';
// Useful when you want to check the second argument first
const hasValue = swappedOr("default");
hasValue(null);
// "default"
hasValue("custom");
// "custom"Good to know: All variants convert their operands to boolean values using
Boolean(), so they work with any truthy/falsy values. The swapped variants are useful when the evaluation order matters semantically.
Related
- and - Logical conjunction (&&)
- not - Logical negation (!)
- nor - Logical NOR (!(a || b))
- xor - Logical exclusive OR
- nullish-or - Nullish coalescing (??)