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

    Function toObject

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

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

      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 limited to PropertyKey values and compared after property key coercion, so the number 1 and the string "1" denote the same entry.

      Entries are collected as own properties, so a __proto__ key is stored as data rather than altering the prototype chain of the returned object; they are enumerated in standard property order, that is integer-like keys in ascending numeric order, followed by other string keys and symbol keys in collection order. The object is made immutable once the feed is drained, freezing it together with the values collected into it.

      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 object 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 items, giving them a fresh identity: entries are not reachable through the original item reference. Supply structured items as immutable values to keep their identity stable.

      Type Parameters

      • V

        The type of items in the feed

      • K extends PropertyKey

        The type of object keys

      Parameters

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

        The function extracting the key from each item

      Returns Sink<V, Readonly<Record<K, V>>>

      A sink resolving to the deeply immutable object 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" }]))
      (toObject(x => x.id))
      ); // { 1: { id: 1, name: "Alice" }, 2: { id: 2, name: "Bob" } }
    • Creates a sink collecting extracted values into an object under extracted keys.

      Collects like toObject 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 extends PropertyKey

        The type of object keys

      • T

        The type of object 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 an object value

      Returns Sink<V, Readonly<Record<K, T>>>

      A sink resolving to the deeply immutable object 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" }]))
      (toObject(x => x.id, x => x.name))
      ); // { 1: "Alice", 2: "Bob" }