Packages@accelint/coreLogical
nullishOr
Nullish coalescing (??) for values and function results
Usage
import { nullishOr, nullishOrFn, swappedNullishOr, swappedNullishOrFn } from '@accelint/core';
// Value-based nullish coalescing
nullishOr(null)(42);
// 42
nullishOr(0)(42);
// 0 (0 is not nullish)
// Function-based nullish coalescing
const getFromCache = (key: string): number | null => cache.get(key);
const computeValue = (key: string): number => expensiveCalculation(key);
const getValue = nullishOrFn(getFromCache)(computeValue);
getValue('user-123');
// Returns cached value, or computes if cache is null/undefinedReference
nullishOr
function nullishOr<A>(a: A): <B>(b: B) => A | BParameters
| Parameter | Type | Description |
|---|---|---|
a | A | The possibly nullish value |
Returns
Returns a curried function that accepts a fallback value and returns the first value if not nullish, otherwise the fallback.
Type Parameters
A- The type of the first input valueB- The type of the second input value
nullishOrFn
function nullishOrFn<T, A>(
a: (x: T) => A
): <B>(b: (y: T) => B) => (c: T) => A | BParameters
| Parameter | Type | Description |
|---|---|---|
a | (x: T) => A | The function that may return nullish |
Returns
Returns a curried function that accepts a fallback function, then accepts a value to pass to both functions, returning the first function result if not nullish, otherwise the second.
Type Parameters
T- The type of the input value passed to both functionsA- The return type of the first functionB- The return type of the second function
swappedNullishOr
function swappedNullishOr<A>(a: A): <B>(b: B) => A | BEvaluates b ?? a instead of a ?? b.
swappedNullishOrFn
function swappedNullishOrFn<T, A>(
a: (x: T) => A
): <B>(b: (y: T) => B) => (c: T) => A | BEvaluates b(x) ?? a(x) instead of a(x) ?? b(x).
Examples
Example: Default values for optional fields
import { nullishOr } from '@accelint/core';
interface User {
name: string;
age?: number;
}
const getAge = (user: User) =>
nullishOr(user.age)(18);
getAge({ name: 'Alice', age: 25 });
// 25
getAge({ name: 'Bob' });
// 18
getAge({ name: 'Charlie', age: 0 });
// 0 (not nullish, so 0 is returned)Example: Fallback computation
import { nullishOrFn } from '@accelint/core';
const getUserPreference = (user: User): Theme | null =>
user.preferences?.theme ?? null;
const getSystemTheme = (): Theme =>
window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
const getTheme = nullishOrFn(getUserPreference)(getSystemTheme);
getTheme(user);
// User theme if set, otherwise system themeExample: Difference from logical OR
import { nullishOr, or } from '@accelint/core';
// nullishOr preserves falsy values (except null/undefined)
nullishOr(0)('fallback');
// 0
nullishOr(false)('fallback');
// false
nullishOr('')('fallback');
// ''
// or converts to boolean
or(0)('fallback');
// 'fallback'Good to know: Unlike
or,nullishOronly treatsnullandundefinedas absent values. Falsy values like0,false, and''are preserved. This matches JavaScript's??operator.