All files / comparator/src entryValueComparator.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                                4x     4x    
import { Entry } from "@benneq/object";
import { Comparator } from "./_types";
 
/**
 * Creates a {@link Comparator} for {@link Entry} values.
 *
 * @example
 * entryValueComparator(numberComparator)([0, 1], [0, 0]) => 1
 * entryValueComparator(numberComparator)([0, 1], [0, 1]) => 0
 * entryValueComparator(numberComparator)([0, 0], [0, 1]) => -1
 *
 * @typeParam K - the {@link Entry} key type
 * @typeParam V - the {@link Entry} value type
 * @param comparator
 * @returns a {@link Comparator} for {@link Entry} values
 */
export const entryValueComparator = <K, V>(
  comparator: Comparator<V>
): Comparator<Entry<K, V>> => {
  return (entryA, entryB) => comparator(entryA[1], entryB[1]);
};