All files / src/functions comparators.ts

100% Statements 41/41
100% Branches 22/22
100% Functions 15/15
100% Lines 29/29

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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242                                                                                                      2217702x                                                       20x     105x     81x                                           22x 8x 8x                                                     25x 25x 9x 9x                                                   11x 11x 21x   6x 8x 11x 11x   1x                 31x 31x 18x 18x   23x 56x 32x 17x                                                   17x                                           14x    
/**
 * Comparators: the `(a, b) => number` functions that order values, and helpers to build them.
 *
 * A comparator returns a negative number when `a` comes first, a positive number when `b` comes
 * first, and zero when they are equal in the order; only the sign matters. It is the same shape
 * `Array.prototype.sort` accepts, so every comparator here works with native `sort` unchanged.
 *
 * @packageDocumentation
 */
 
import { compareNatural, compareSymbol } from '../internal/natural-order'
 
export { compareSymbol }
 
/**
 * A function that orders two values: negative when `a` comes first, positive when `b` comes
 * first, zero when they are equal in this order. Only the sign is used.
 *
 * It is the shape `Array.prototype.sort` accepts.
 *
 * @example
 * ```ts
 * const byLength: Comparator<string> = (a, b) => a.length - b.length
 * ['ccc', 'a', 'bb'].sort(byLength) // ['a', 'bb', 'ccc']
 * ```
 */
export type Comparator<T> = (a: T, b: T) => number
 
/**
 * An object that states its own natural order through the `compareSymbol` protocol.
 *
 * `a[compareSymbol](b)` returns a negative number when `a` comes first, a positive number when `b`
 * comes first, and zero when they are equal in the order. Natural order uses it only when both
 * values implement it, and only its sign. Keep the order consistent: if `a` is before `b`, then `b`
 * is after `a`, and if `a` is before `b` and `b` before `c`, then `a` is before `c`.
 *
 * @example
 * ```ts
 * class Version implements Comparable<Version> {
 *   constructor(readonly major: number, readonly minor: number) {}
 *   [compareSymbol](other: Version): number {
 *     return this.major - other.major || this.minor - other.minor
 *   }
 * }
 * [new Version(2, 0), new Version(1, 4)].sort(naturalOrder()) // 1.4, then 2.0
 * ```
 */
export interface Comparable<T> {
  [compareSymbol](other: T): number
}
 
const natural: Comparator<unknown> = (a, b) => compareNatural(a, b, 'naturalOrder')
 
/**
 * Returns the comparator for natural order.
 *
 * Numbers compare numerically (`-0` equals `0`; `NaN` comes after every other number), strings by
 * UTF-16 code units (the order of `<` on two strings, identical on every runtime; use
 * `Intl.Collator().compare` for a locale's order), bigints numerically and booleans with `false`
 * first, dates by time value (invalid dates last), and objects that implement `[compareSymbol]` by
 * that method. Values are never converted to strings or compared with `<` coercion.
 *
 * Time: O(1) per comparison for numbers and booleans; O(1) amortised for dates and
 * `[compareSymbol]` objects (plus the method); O(length of the common prefix) for strings;
 * O(digits) for bigints.
 *
 * @returns A comparator for natural order.
 * @throws NullElementError (from the comparator) when a value is `null` or `undefined`.
 * @throws ClassCastError (from the comparator) when the values are of different kinds, such as a
 *   number and a string, or a number and a bigint, or have no natural order.
 * @throws TypeError (from the comparator) when a `[compareSymbol]` method returns a non-number or
 *   `NaN`.
 *
 * @example
 * ```ts
 * [10, 9, 1].sort(naturalOrder()) // [1, 9, 10] — native sort without a comparator gives [1, 10, 9]
 * ```
 */
export function naturalOrder<T>(): Comparator<T> {
  return natural
}
 
const reverseNatural: Comparator<unknown> = (a, b) => compareNatural(b, a, 'naturalOrder')
 
function requireFunction(value: unknown, where: string, what: string): void {
  if (typeof value !== 'function') throw new TypeError(`${where}: ${what} is not a function`)
}
 
/**
 * Returns a comparator for the reverse of `comparator`, or of natural order when it is omitted.
 *
 * The result for `(a, b)` is `comparator(b, a)`, returned unchanged.
 *
 * Time: O(1) plus one call of `comparator` per comparison.
 *
 * @param comparator - The order to reverse; natural order when omitted.
 * @returns The reversed comparator.
 * @throws TypeError when `comparator` is given but is not a function (including `null`).
 * @throws NullElementError, ClassCastError or TypeError (from the comparator, when natural order is
 *   used) as described for {@link naturalOrder}.
 *
 * @example
 * ```ts
 * [1, 3, 2].sort(reverseOrder()) // [3, 2, 1]
 * ```
 */
export function reverseOrder<T>(comparator?: Comparator<T>): Comparator<T> {
  if (comparator === undefined) return reverseNatural
  requireFunction(comparator, 'reverseOrder', 'comparator')
  return (a, b) => comparator(b, a)
}
 
/**
 * Returns a comparator that orders values by a key derived from each of them.
 *
 * `key` is called exactly once for each of the two values in every comparison, and its errors
 * propagate. The keys are compared with `comparator`, or in natural order when it is omitted (so
 * keys of mixed kinds throw `ClassCastError`). Wrap the key comparator with {@link nullsFirst} or
 * {@link nullsLast} to allow missing keys.
 *
 * Time: O(1) plus two calls of `key` and one of `comparator` per comparison.
 *
 * @param key - Derives the value to compare.
 * @param comparator - Orders the keys; natural order when omitted.
 * @returns The comparator.
 * @throws TypeError when `key` is not a function, or `comparator` is given but is not a function.
 * @throws NullElementError, ClassCastError or TypeError (from the comparator, when natural order is
 *   used) as described for {@link naturalOrder}.
 *
 * @example
 * ```ts
 * const byAge = comparing((user: { age: number }) => user.age)
 * const byNickname = comparing((user: { nickname?: string }) => user.nickname, nullsFirst())
 * ```
 */
export function comparing<T, U>(key: (value: T) => U, comparator?: Comparator<U>): Comparator<T> {
  requireFunction(key, 'comparing', 'key')
  if (comparator === undefined) return (a, b) => compareNatural(key(a), key(b), 'naturalOrder')
  requireFunction(comparator, 'comparing', 'comparator')
  return (a, b) => comparator(key(a), key(b))
}
 
/**
 * Returns a comparator that applies `first`, then each of `next` in turn to break ties.
 *
 * The result is the first one that is not `0`, returned unchanged; later comparators are not
 * called once one result is not `0`. When every result is `0`, the result is `0`.
 *
 * Time: O(k) for k comparators, plus at most one call of each per comparison.
 *
 * @param first - The primary order.
 * @param next - Tie-breakers, in order.
 * @returns The combined comparator.
 * @throws TypeError when any argument is not a function.
 *
 * @example
 * ```ts
 * type Person = { last: string; first: string }
 * const byName = thenComparing<Person>(
 *   comparing((p) => p.last),
 *   comparing((p) => p.first),
 * )
 * ```
 */
export function thenComparing<T>(first: Comparator<T>, ...next: Comparator<T>[]): Comparator<T> {
  const all = [first, ...next]
  for (let i = 0; i < all.length; i++) {
    requireFunction(all[i], 'thenComparing', `comparator ${i + 1}`)
  }
  return (a, b) => {
    for (const comparator of all) {
      const result = comparator(a, b)
      if (result !== 0) return result
    }
    return 0
  }
}
 
function nulls<T>(
  comparator: Comparator<T> | undefined,
  where: string,
  nullResult: -1 | 1,
): Comparator<T | null | undefined> {
  let compareRest: Comparator<T> = natural
  if (comparator !== undefined) {
    requireFunction(comparator, where, 'comparator')
    compareRest = comparator
  }
  return (a, b) => {
    if (a === null || a === undefined) return b === null || b === undefined ? 0 : nullResult
    if (b === null || b === undefined) return -nullResult
    return compareRest(a, b)
  }
}
 
/**
 * Returns a comparator that puts `null` and `undefined` before every other value, treats them as
 * equal to each other, and orders the rest with `comparator` (natural order when omitted).
 * `comparator` is never called with `null` or `undefined`.
 *
 * Native `Array.prototype.sort` always moves `undefined` and empty slots to the end without calling
 * the comparator, so with native `sort` this moves only `null` to the front.
 *
 * Time: O(1) plus at most one call of `comparator` per comparison.
 *
 * @param comparator - Orders the values that are not `null` or `undefined`.
 * @returns The comparator.
 * @throws TypeError when `comparator` is given but is not a function (including `null`).
 * @throws NullElementError, ClassCastError or TypeError (from the comparator, when natural order is
 *   used) as described for {@link naturalOrder}.
 *
 * @example
 * ```ts
 * [3, null, 1].sort(nullsFirst()) // [null, 1, 3]
 * ```
 */
export function nullsFirst<T>(comparator?: Comparator<T>): Comparator<T | null | undefined> {
  return nulls(comparator, 'nullsFirst', -1)
}
 
/**
 * Returns a comparator that puts `null` and `undefined` after every other value, treats them as
 * equal to each other, and orders the rest with `comparator` (natural order when omitted).
 * `comparator` is never called with `null` or `undefined`.
 *
 * Time: O(1) plus at most one call of `comparator` per comparison.
 *
 * @param comparator - Orders the values that are not `null` or `undefined`.
 * @returns The comparator.
 * @throws TypeError when `comparator` is given but is not a function (including `null`).
 * @throws NullElementError, ClassCastError or TypeError (from the comparator, when natural order is
 *   used) as described for {@link naturalOrder}.
 *
 * @example
 * ```ts
 * [null, 3, 1].sort(nullsLast()) // [1, 3, null]
 * ```
 */
export function nullsLast<T>(comparator?: Comparator<T>): Comparator<T | null | undefined> {
  return nulls(comparator, 'nullsLast', 1)
}