All files / iterable/src every.ts

100% Statements 6/6
100% Branches 0/0
100% Functions 3/3
100% Lines 6/6

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 253x 3x                                   13x 3x 13x 13x    
import { not, Predicate } from "@benneq/predicate";
import { some } from "./some";
 
/**
 * Checks if all elements of an {@link Iterable} match a {@link Predicate}.
 *
 * @example
 * ```ts
 * const allValuesEven = every(isEven);
 * const result = allValuesEven([2,4,6]);
 * console.log(result); // true
 * ```
 *
 * @see {@link filter} and {@link some} for similar operations.
 *
 * @typeParam T - the {@link Iterable} value type
 * @param predicate - the {@link Predicate} to match
 * @returns `true` if the {@link Predicate} returns `true` for all elements
 */
export const every =
  <T>(predicate: Predicate<[T]>) =>
  (iterable: Iterable<T>): boolean => {
    return !some<T>(not(predicate))(iterable);
  };