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

    Function group

    • Creates a task collecting items sharing the same key.

      The whole feed is drained before the first group is emitted, then 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

      Accumulates the whole feed in memory before grouping. For large or infinite feeds, this may exhaust memory or never complete.

      Type Parameters

      • V

        The type of items in the feed

      • K extends Primitive

        The type of the grouping key

      Parameters

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

        The function extracting the grouping key from each item

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

      A task pairing each distinct key 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 }]]]