All files / predicate/src not.ts

100% Statements 4/4
100% Branches 0/0
100% Functions 3/3
100% Lines 2/2

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                                                    2x     2x    
import { Predicate } from "./_types";
 
/**
 * Inverts a {@link Predicate} with a logical `NOT`.
 *
 * The inverted {@link Predicate} returns `true` if the {@link Predicate}
 * returns `false` and vice versa.
 *
 * @example
 * ```ts
 * const isOdd = not(isEven);
 * const result = isOdd(-1);
 * console.log(result); // true
 * ```
 *
 * @example
 * ```ts
 * const isNotEmpty = not(isEmpty);
 * const result = isNotEmpty([]);
 * console.log(result); // false
 * ```
 *
 * @typeParam T - the type of the {@link Predicate}
 * @param predicate - the {@link Predicate} to negate
 * @returns `true` if the {@link Predicate} returns `false`, otherwise `false`
 */
export const not = <T extends unknown[]>(
  predicate: Predicate<T>
): Predicate<T> => {
  return (...args) => !predicate(...args);
};