import { NotFoundError } from '../error/NotFoundError'; /** * Unset value. */ /* eslint-disable-next-line @typescript-eslint/ban-types */ export type Nil = null | undefined; /** * Value that may be nil. */ export type Optional = T | Nil; /** * Comparison (filter) predicate for a single value. */ export type PredicateC1 = (val: TVal, idx: number, list: Array) => boolean; /** * Comparison (sort) predicate for two values. */ export type PredicateC2 = (pval: TVal, nval: TVal, idx: number, list: Array) => number; /** * Reduction predicate for two values. */ export type PredicateR2 = (pval: TVal, nval: TVal, idx: number, list: Array) => TVal; /* eslint-disable-next-line @typescript-eslint/ban-types */ export function isNil(val: Optional): val is Nil { /* eslint-disable-next-line no-null/no-null */ return val === null || val === undefined; } /** * Calculate the "length" of an array or value. * * Arrays return their length, single values return 1, and nil values return 0. * This counts the number of elements that setOrPush would add. */ export function countOf(val: unknown): number { if (Array.isArray(val)) { return val.length; } if (doesExist(val)) { return 1; } return 0; } export function defaultWhen(condition: boolean, ...items: Array): TVal { if (condition) { return items[0]; } else { return items[1]; } } /** * Remove any null or undefined items from the list. */ export function filterNil(list: ArrayLike>): Array { return Array.from(list).filter(doesExist); } /** * Merge arguments, which may or may not be arrays, into one return that is definitely an array. */ export function mergeList(...parts: Array>): Array { const out = []; for (const part of parts) { if (Array.isArray(part)) { out.push(...part); } else { out.push(part); } } return out; } /** * Find a value matching the given predicate or throw. */ export function mustFind(list: Array>, predicate: PredicateC1): TVal { const val = filterNil(list).find(predicate); return mustExist(val); } /** * Check if a variable is not nil. */ export function doesExist(val: Optional): val is T { return !isNil(val); } /** * Assert that a variable is not nil and return the value. * * @throws NotFoundError * @returns val */ export function mustExist(val: Optional): T { if (isNil(val)) { throw new NotFoundError(); } return val; } /** * Return the first value that is not nil. */ export function mustCoalesce(...values: Array>): T { return mustFind(values, doesExist); }