All files / src/internal natural-order.ts

97.29% Statements 72/74
98.16% Branches 107/109
100% Functions 10/10
96.66% Lines 58/60

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                                                          106x                         106x               6955886x 6955886x 322312x 322312x 322312x   234807x   322312x   6955886x                     7059809x         107905x   6951586x 4784742x 4784742x     318x         52407x       7052782x       2389161x 2389161x 18x   2389123x       25025863x 13833351x 2027154x   48314x 19491x       1013227x                       26345867x 26345867x                                                       7350619x 4112208x 3266699x 455x 455x   3266244x 3266244x 3262108x 3262108x 3261815x 24013x   3237802x 982629x         959957x   78x   22594x                                                 21117189x 413144x 268052x 266881x 266881x 21117189x 9268x       257473x 123485x       123450x   3x   32x      
// Natural order: the library's single definition of "less than" for values compared without a
// comparator. `compareSymbol` is public (re-exported by the comparators module); everything else in
// this file is internal and marked so on each declaration.
 
import { ClassCastError, NullElementError } from '../errors/errors'
import { describeValue } from './checks'
import { createWeakMap, getTime, weakMapGet, weakMapSet } from './intrinsics'
 
/**
 * The key of the protocol by which an object states its own natural order.
 *
 * An object whose `[compareSymbol]` is a method is compared with another such object by calling
 * `a[compareSymbol](b)`, which returns a negative number, zero or a positive number. Only the sign
 * is used. The key is `Symbol.for('javascript-collections-framework.compare')` and does not change
 * between major versions, so classes keep working across upgrades. Implementations must order
 * consistently: if `a` is before `b`, then `b` is after `a`, and the order is transitive.
 *
 * Time: O(1) to read; the method's cost is the implementer's.
 *
 * @example
 * ```ts
 * class Money implements Comparable<Money> {
 *   constructor(readonly cents: number) {}
 *   [compareSymbol](other: Money): number {
 *     return this.cents - other.cents
 *   }
 * }
 * ```
 */
export const compareSymbol: unique symbol = /*#__PURE__*/ Symbol.for(
  'javascript-collections-framework.compare',
)
 
type Kind = 'number' | 'string' | 'bigint' | 'boolean' | 'date' | 'protocol'
 
type ProtocolMethod = (this: object, other: unknown) => unknown
 
/**
 * Whether each object seen so far is a date. Telling a non-date apart costs a thrown exception, and
 * whether an object is a date never changes, so the answer is kept per object. A `WeakMap` neither
 * keeps the object alive nor adds anything to it.
 */
const dateCache: WeakMap<object, boolean> = /*#__PURE__*/ createWeakMap<object, boolean>()
 
/**
 * Returns whether `value` is a date: the built-in `getTime` succeeds on it. True for dates from any
 * realm and for Date subclasses; false for objects that only inherit from `Date.prototype` and for
 * `Proxy` objects. Runs no user code.
 */
function isDate(value: object): boolean {
  let known = weakMapGet(dateCache, value)
  if (known === undefined) {
    try {
      getTime(value as Date)
      known = true
    } catch {
      known = false
    }
    weakMapSet(dateCache, value, known)
  }
  return known
}
 
/**
 * The kind of a value for natural order, or `undefined` when it has no natural order. For an object
 * that implements the protocol, the method itself is returned, so it is read exactly once per
 * comparison (a getter or `Proxy` trap runs once, and the function checked is the one called).
 * Primitives are classified by `typeof` alone; the protocol is looked up only on objects. The
 * caller has already rejected `null` and `undefined`.
 */
function kindOf(value: unknown): Exclude<Kind, 'protocol'> | ProtocolMethod | undefined {
  switch (typeof value) {
    case 'number':
    case 'string':
    case 'bigint':
    case 'boolean':
      return typeof value as Exclude<Kind, 'protocol' | 'date'>
    case 'object': {
      if (isDate(value as object)) return 'date'
      const method: unknown = (value as Record<symbol, unknown>)[compareSymbol]
      return typeof method === 'function' ? (method as ProtocolMethod) : undefined
    }
    default:
      return undefined
  }
}
 
function label(value: unknown): string {
  return typeof value === 'object' && value !== null && isDate(value) ? 'date' : typeof value
}
 
function kindName(kind: Exclude<Kind, 'protocol'> | ProtocolMethod): Kind {
  return typeof kind === 'function' ? 'protocol' : kind
}
 
function compareProtocol(method: ProtocolMethod, a: object, b: unknown, where: string): -1 | 0 | 1 {
  const result = method.call(a, b)
  if (typeof result !== 'number' || Number.isNaN(result)) {
    throw new TypeError(`${where}: [compareSymbol] returned ${describeValue(result)}`)
  }
  return result < 0 ? -1 : result > 0 ? 1 : 0
}
 
function compareNumbers(a: number, b: number): -1 | 0 | 1 {
  if (a < b) return -1
  if (a > b) return 1
  if (a === b) return 0
  // At least one is NaN: NaN equals NaN and comes after every other number.
  if (Number.isNaN(a)) return Number.isNaN(b) ? 0 : 1
  return -1
}
 
function compareOrdered(a: string | bigint, b: string | bigint): -1 | 0 | 1 {
  return a < b ? -1 : a > b ? 1 : 0
}
 
/**
 * Whether `a` and `b` are both numbers or both strings: values whose natural comparison runs no
 * user code (a primitive never consults `[compareSymbol]`) and cannot throw.
 *
 * Time: O(1).
 *
 * @internal
 */
export function isPlainKind(a: unknown, b: unknown): boolean {
  const kind = typeof a
  return (kind === 'number' || kind === 'string') && typeof b === kind
}
 
/**
 * Compares two values in natural order.
 *
 * `null` and `undefined` are rejected first; then both values must be of the same kind. Numbers
 * compare numerically (`-0` equals `0`; `NaN` equals itself and comes after every other number),
 * strings by UTF-16 code units, bigints numerically, booleans with `false` first, dates by time
 * value (invalid dates last), and objects that both implement `[compareSymbol]` by the sign of
 * `a[compareSymbol](b)`, whose result must be a number other than `NaN`. A date is compared by
 * time even when it implements the protocol. Values are never compared by `<` coercion or by
 * conversion to strings.
 *
 * Time: O(1) for numbers and booleans; O(1) amortised for dates and protocol objects (plus the
 * protocol method); O(length of the common prefix) for strings; O(digits) for bigints.
 *
 * @param a - The first value.
 * @param b - The second value.
 * @param where - The name to start error messages with (`naturalOrder`, `TreeMap.put`, …).
 * @returns `-1`, `0` or `1`.
 * @throws NullElementError when either value is `null` or `undefined`.
 * @throws ClassCastError when the values are of different kinds or have no natural order.
 * @throws TypeError when a `[compareSymbol]` method returns a non-number or `NaN`.
 * @internal
 */
export function compareNatural(a: unknown, b: unknown, where: string): -1 | 0 | 1 {
  // Two numbers or two strings: the common case, compared without classifying either value.
  if (typeof a === 'number' && typeof b === 'number') return compareNumbers(a, b)
  if (typeof a === 'string' && typeof b === 'string') return compareOrdered(a, b)
  if (a === null || a === undefined || b === null || b === undefined) {
    const missing = a === null || a === undefined ? a : b
    throw new NullElementError(`${where}: ${describeValue(missing)} cannot be compared`)
  }
  const kind = kindOf(a)
  if (kind === undefined) throw new ClassCastError(`${where}: ${label(a)} cannot be compared`)
  const other = kindOf(b)
  if (other === undefined) throw new ClassCastError(`${where}: ${label(b)} cannot be compared`)
  if (kindName(kind) !== kindName(other)) {
    throw new ClassCastError(`${where}: ${label(a)} and ${label(b)} cannot be compared`)
  }
  if (typeof kind === 'function') return compareProtocol(kind, a as object, b, where)
  switch (kind) {
    case 'number':
      return compareNumbers(a as number, b as number)
    case 'date':
      // Invalid dates have a NaN time value, so they come last, as NaN does among numbers.
      return compareNumbers(getTime(a as Date), getTime(b as Date))
    case 'boolean':
      return a === b ? 0 : a ? 1 : -1
    default:
      return compareOrdered(a as string | bigint, b as string | bigint)
  }
}
 
/**
 * Compares two values in natural order as `compareNatural` does, but returns `undefined` where
 * `compareNatural` would throw `NullElementError` or `ClassCastError` itself: for `null`,
 * `undefined`, values with no natural order and values of different kinds. An error thrown by a
 * `[compareSymbol]` method, or its invalid result, still propagates. Lookups use it to answer
 * "absent" as a native `Set` does.
 *
 * Time: as `compareNatural`.
 *
 * @param a - The first value.
 * @param b - The second value.
 * @param where - The name to start error messages with.
 * @returns `-1`, `0` or `1`, or `undefined` when the values cannot be compared.
 * @throws TypeError when a `[compareSymbol]` method returns a non-number or `NaN`.
 * @internal
 */
export function compareNaturalOrAbsent(
  a: unknown,
  b: unknown,
  where: string,
): -1 | 0 | 1 | undefined {
  if (typeof a === 'number' && typeof b === 'number') return compareNumbers(a, b)
  if (typeof a === 'string' && typeof b === 'string') return compareOrdered(a, b)
  if (a === null || a === undefined || b === null || b === undefined) return undefined
  const kind = kindOf(a)
  const other = kind === undefined ? undefined : kindOf(b)
  if (kind === undefined || other === undefined || kindName(kind) !== kindName(other)) {
    return undefined
  }
  // The same steps as the end of `compareNatural`, kept separate so code that never needs this
  // function does not pay for sharing them.
  if (typeof kind === 'function') return compareProtocol(kind, a as object, b, where)
  switch (kind) {
    case 'number':
      return compareNumbers(a as number, b as number)
    case 'date':
      return compareNumbers(getTime(a as Date), getTime(b as Date))
    case 'boolean':
      return a === b ? 0 : a ? 1 : -1
    default:
      return compareOrdered(a as string | bigint, b as string | bigint)
  }
}