@metreeca/flow - v0.10.0
    Preparing search index...

    Function toMap

    Creates a sink collecting items or extracted values into a map under extracted keys.

    • Creates a sink collecting the items of the feed into a map under extracted keys.

      Keys and values are made immutable as they are collected, in source order. Keys must be unique: an item whose key was already collected fails the sink rather than silently overwriting the entry standing under it. Keys are compared with SameValueZero semantics, so NaN matches itself and -0 matches 0.

      The returned map is frozen, with set, delete and clear shadowed by own properties that throw: entries cannot be altered through the map, although mutating methods invoked directly on Map.prototype still reach the internal slots backing them.

      Warning

      • Exhaustive: every item is collected before the sink resolves, so an infinite feed never completes.
      • Materialising: the whole feed is held in memory, so a large feed may exhaust it.
      • Stateful: the map covers the items drawn, so a sink closing a nested or truncated feed sees those alone, and a key repeated across two of them fails neither.
      Warning

      Freezing clones structured keys, giving them a fresh identity: entries are not reachable through the original key reference, and the same mutable key extracted twice yields two distinct entries rather than a duplicate key error. Extract structured keys as immutable values to keep their identity stable.

      Type Parameters

      • V

        The type of items in the feed

      • K

        The type of map keys

      Parameters

      • key: (item: NoInfer<V>) => Awaitable<K>

        The function extracting the key from each item

      Returns Sink<V, ReadonlyMap<K, V>>

      A sink resolving to the deeply immutable read-only map pairing each extracted key with the item it was extracted from

      Error If two items yield the same key

      await pipe(
      (items([{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }]))
      (toMap(x => x.id))
      ); // Map(2) { 1 => { id: 1, name: "Alice" }, 2 => { id: 2, name: "Bob" } }
    • Creates a sink collecting extracted values into a map under extracted keys.

      Collects like toMap without a value argument, subject to the same key, freezing and axis rules, pairing each key with an extracted value rather than with the item itself.

      Type Parameters

      • V

        The type of items in the feed

      • K

        The type of map keys

      • T

        The type of map values

      Parameters

      • key: (item: NoInfer<V>) => Awaitable<K>

        The function extracting the key from each item

      • value: (item: NoInfer<V>) => Awaitable<T>

        The function transforming each item into a map value

      Returns Sink<V, ReadonlyMap<K, T>>

      A sink resolving to the deeply immutable read-only map pairing each extracted key with the value extracted alongside it

      Error If two items yield the same key

      await pipe(
      (items([{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }]))
      (toMap(x => x.id, x => x.name))
      ); // Map(2) { 1 => "Alice", 2 => "Bob" }