Web streams utils - v0.3.0
    Preparing search index...

    Function reduce

    • Create a TransformStream that reduces all values to a single value

      Type Parameters

      • T
      • R

      Parameters

      • reducer: (accumulator: R, chunk: T) => SyncOrAsync<R>

        The reducer function to apply to each chunk

      • initialValue: R

        The initial value

      Returns TransformStream<T, R>

      A TransformStream that emits the final reduced value when the stream closes

      // Sum all numbers in a stream
      const stream = readable.pipeThrough(reduce((a, b) => a + b));
      const [sum] = await toArray(stream); // Gets the single reduced value