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 | 9x 9x 9x 33x 50x 27x 23x 12x 11x | import { Comparator } from "./_types";
/**
* Compares values first using a {@link @benneq/predicate!Predicate}, and if both values don't
* match the {@link @benneq/predicate!Predicate} it will fall back to a {@link Comparator}.
*
* @example
* predicateComparator(isNull)(numberComparator)(0, null) => 1
* predicateComparator(isNull)(numberComparator)(null, -1) => -1
* predicateComparator(isNull)(numberComparator)(null, null) => 0
*
* @typeParam T - the {@link Comparator} value type
* @typeParam U - the {@link @benneq/predicate!Predicate} value type
* @param predicate
* @param trueFirst
* @returns a {@link Comparator}
*/
export const predicateComparator =
<U>(predicate: (value: unknown) => value is U, trueFirst = true) =>
<T>(comparator: Comparator<T>): Comparator<U | T> => {
return (valueA, valueB) => {
if (predicate(valueA)) {
return predicate(valueB) ? 0 : trueFirst ? -1 : 1;
} else if (predicate(valueB)) {
return trueFirst ? 1 : -1;
} else {
return comparator(valueA, valueB);
}
};
};
|