All files / src/internal/views/read-only read-only-entry.ts

100% Statements 17/17
100% Branches 6/6
100% Functions 12/12
100% Lines 15/15

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                                          1026x       961x       628x         246x       95x       219x       17x 16x 15x 15x       1x       11x       19x       2x               3x      
// The entry a read-only map hands out: it wraps the source map's entry and reads it each time, so
// it shows the value the source entry shows. `setValue` is refused.
 
import type { MapEntry } from '../../../interfaces/map'
import { isBranded, MAP_ENTRY_BRAND } from '../../brand'
import { equals, equalsSymbol, type HashFunction, hashCodeSymbol } from '../../equality'
import { NativeLiveIterator } from '../../iteration'
import { INSPECT, type InspectOptions, inspectMapping, mappingToString } from '../../output'
import { mappingHash } from '../../skeletons/abstract-map'
import { refuse } from './refuse'
import { hideSource, SOURCE } from './source-key'
 
/**
 * A read-only entry over a source entry.
 *
 * @internal
 */
export class ReadOnlyEntry<K, V> implements MapEntry<K, V> {
  declare readonly [SOURCE]: MapEntry<K, V>
 
  constructor(source: MapEntry<K, V>) {
    hideSource(this, source)
  }
 
  get key(): K {
    return this[SOURCE].key
  }
 
  get value(): V {
    return this[SOURCE].value
  }
 
  /** Marks every map entry, across loaded copies. */
  get [MAP_ENTRY_BRAND](): true {
    return true
  }
 
  setValue(_value: V): V {
    throw refuse('MapEntry.setValue')
  }
 
  [Symbol.iterator](): IterableIterator<K | V> {
    return new NativeLiveIterator<K | V>([this.key, this.value][Symbol.iterator]())
  }
 
  equals(other: unknown): boolean {
    if (other === this) return true
    if (!isBranded(other, MAP_ENTRY_BRAND)) return false
    const that = other as MapEntry<K, V>
    return equals(this.key, that.key) && equals(this.value, that.value)
  }
 
  [equalsSymbol](other: unknown): boolean {
    return this.equals(other)
  }
 
  [hashCodeSymbol](hash: HashFunction): number {
    return mappingHash(hash(this.key), hash(this.value))
  }
 
  toString(): string {
    return mappingToString(this, this.key, this.value)
  }
 
  toJSON(): [K, V] {
    return [this.key, this.value]
  }
 
  [INSPECT](
    _depth: number | null,
    options: InspectOptions,
    inspect: (value: unknown, options: InspectOptions) => string,
  ): string {
    return inspectMapping(this.key, this.value, options, inspect)
  }
}