All files / predicate/src or.ts

100% Statements 5/5
100% Branches 0/0
100% Functions 4/4
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34                                                        7x 1x 7x 8x    
import { Predicate } from "./_types";
 
/**
 * Combines {@link Predicate}s with a logical `OR`.
 *
 * The combined {@link Predicate} returns `true` if any of the
 * {@link Predicate}s returns `true`.
 *
 * @example
 * Combine two Predicates with a logical `OR`
 * ```
 * const predicate = or(numberIsPositive, numberIsEven);
 * const result = p(-2);
 * console.log(result); // true
 * ```
 *
 * @example
 * Returns an {@link alwaysFalse} Predicate if no args provided
 * ```
 * const predicate = or();
 * const result = p();
 * console.log(result); // false
 * ```
 *
 * @typeParam T - the type of the {@link Predicate}s
 * @param predicates - the {@link Predicate}s to combine
 * @returns `true` if any of the {@link Predicate}s returned `true`, otherwise `false`
 */
export const or =
  <T extends unknown[]>(...predicates: Predicate<T>[]): Predicate<T> =>
  (...args) => {
    return predicates.some((predicate) => predicate(...args));
  };