All files / src/internal equivalence.ts

100% Statements 33/33
100% Branches 20/20
100% Functions 10/10
100% Lines 28/28

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                                                        109x 2x 1x         73915x 21767x     109x 9499x 21767x                                       3540x 3538x 1577x 25x 1577x 1564x 1564x 1564x 1564x 1552x 1552x 1552x   1273769x 1273769x 5x   1273741x     362364x 362364x 24x   362333x        
// Checking an `equivalence` option given to a collection, and calling it safely.
 
import { describeValue } from './checks'
import {
  equalsAt,
  type HashFunction,
  hashAt,
  type ProtocolMethods,
  sameValueZero,
  sameValueZeroEquivalence,
  valueEquivalence,
} from './equality'
import { hashPrimitiveOrIdentity } from './hashing'
import { apply } from './intrinsics'
 
/**
 * An equivalence a collection can call, with results validated and errors naming the collection
 * method that called it.
 *
 * @internal
 */
export interface CheckedEquivalence {
  /** `methodsOfA`: `a`'s protocol methods already read by the caller (used by `valueEquivalence`). */
  equals(a: unknown, b: unknown, where: string, methodsOfA?: ProtocolMethods): boolean
  /** `methods`: the value's protocol methods already read by the caller (used by `valueEquivalence`). */
  hash(value: unknown, where: string, methods?: ProtocolMethods): number
}
 
const sameValueZeroChecked: CheckedEquivalence = {
  equals: (a, b) => sameValueZero(a, b),
  hash: (value) => hashPrimitiveOrIdentity(value),
}
 
/** A hash function for nested protocol values that reports errors with `where`. */
function nestedHash(where: string): HashFunction {
  const hash: HashFunction = (value) => hashAt(value, where, hash)
  return hash
}
 
const valueChecked: CheckedEquivalence = {
  equals: (a, b, where, methodsOfA) => equalsAt(a, b, where, methodsOfA),
  hash: (value, where, methods) => hashAt(value, where, nestedHash(where), methods),
}
 
/**
 * Checks a collection's `equivalence` option at construction.
 *
 * An omitted option, or this copy's `sameValueZeroEquivalence`, means SameValueZero; this copy's
 * `valueEquivalence` is routed to the internal entry points so protocol errors (including those of
 * nested values) name the collection method. Any other object must have `equals` and `hash`
 * functions; they are read once and called with `this` set to the object, and their results are
 * validated: `equals` must return a boolean, `hash` a finite number (reduced with ToInt32).
 *
 * Time: O(1).
 *
 * @param option - The `equivalence` option as given.
 * @param where - The constructor to name in errors (`HashMap.constructor`).
 * @throws TypeError when the option is `null`, not an object, or lacks either function.
 * @internal
 */
export function checkEquivalence(option: unknown, where: string): CheckedEquivalence {
  if (option === undefined || option === sameValueZeroEquivalence) return sameValueZeroChecked
  if (option === valueEquivalence) return valueChecked
  const fail = () =>
    new TypeError(`${where}: equivalence must be an object with equals and hash functions`)
  if (typeof option !== 'object' || option === null) throw fail()
  const record = option as Record<string, unknown>
  const equals = record.equals
  const hash = record.hash
  if (typeof equals !== 'function' || typeof hash !== 'function') throw fail()
  const callEquals = equals as (this: unknown, a: unknown, b: unknown) => unknown
  const callHash = hash as (this: unknown, value: unknown) => unknown
  return {
    equals(a, b, at) {
      const result = apply(callEquals, option, [a, b])
      if (typeof result !== 'boolean') {
        throw new TypeError(`${at}: equivalence.equals returned ${describeValue(result)}`)
      }
      return result
    },
    hash(value, at) {
      const result = apply(callHash, option, [value])
      if (typeof result !== 'number' || result - result !== 0) {
        throw new TypeError(`${at}: equivalence.hash returned ${describeValue(result)}`)
      }
      return result | 0
    },
  }
}