All files / comparator/src reverse.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                                    3x 3x    
import { Comparator } from "./_types";
 
/**
 * A {@link Comparator} that reverses the result of another {@link Comparator}.
 *
 * @example
 * Sort numbers in reverse order
 * ```ts
 * const reverseNumberComparator = reverse(numberComparator);
 * const array = [2, 3, 1];
 * array.sort(reverseNumberComparator);
 * console.log(array); // [3, 2, 1]
 * ```
 *
 * @typeParam T - the {@link Comparator} value type
 * @param comparator - the {@link Comparator} to reverse
 * @returns the reversed {@link Comparator}
 */
export const reverse = <T>(comparator: Comparator<T>): Comparator<T> => {
  return (valueA: T, valueB: T) => comparator(valueA, valueB) * -1;
};