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

    Function group

    • Creates a task collecting items sharing the same key.

      Groups are emitted in first-appearance order of their key, with items inside each group in source order. Keys are limited to Primitive values and compared with SameValueZero semantics, so NaN matches itself and -0 matches 0.

      Warning

      • Exhaustive: the whole feed is drained before the first group is emitted, so an infinite feed never completes.
      • Materialising: the whole feed is held in memory before grouping, so a large feed may exhaust it.
      • Stateful: groups cover the items drawn, so a task invoked per nested feed or per run groups each independently, yielding the same key once per invocation rather than once for the feed as a whole.

      Type Parameters

      • V

        The type of items in the feed

      • K extends Primitive

        The type of the grouping key

      Parameters

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

        The function extracting the grouping key from each item

      Returns Task<V, readonly [K, readonly V[]]>

      A task yielding each distinct key paired with the read-only list of the items sharing it

      await pipe(
      (items([1, 2, 3, 4]))
      (group(n => n%2))
      (toArray())
      ); // [[1, [1, 3]], [0, [2, 4]]]

      await pipe(
      (items([{ id: 1 }, { id: 2 }, { id: 1 }]))
      (group(x => x.id))
      (toArray())
      ); // [[1, [{ id: 1 }, { id: 1 }]], [2, [{ id: 2 }]]]