aboutsummaryrefslogtreecommitdiffstats
path: root/node_modules/rxjs/src/internal/operators/reduce.ts
diff options
context:
space:
mode:
authorPinapelz <yukais@pinapelz.com>2025-06-28 17:26:46 -0700
committerPinapelz <yukais@pinapelz.com>2025-06-28 17:43:56 -0700
commite4fa1e69e7ebfb627c7198fd1a9881e9327ec4d4 (patch)
tree06284a538a6008eca75051399e47db4e5d50301c /node_modules/rxjs/src/internal/operators/reduce.ts
initial commit: scaffolding
Diffstat (limited to 'node_modules/rxjs/src/internal/operators/reduce.ts')
-rw-r--r--node_modules/rxjs/src/internal/operators/reduce.ts61
1 files changed, 61 insertions, 0 deletions
diff --git a/node_modules/rxjs/src/internal/operators/reduce.ts b/node_modules/rxjs/src/internal/operators/reduce.ts
new file mode 100644
index 0000000..5ee565e
--- /dev/null
+++ b/node_modules/rxjs/src/internal/operators/reduce.ts
@@ -0,0 +1,61 @@
+import { scanInternals } from './scanInternals';
+import { OperatorFunction } from '../types';
+import { operate } from '../util/lift';
+
+export function reduce<V, A = V>(accumulator: (acc: A | V, value: V, index: number) => A): OperatorFunction<V, V | A>;
+export function reduce<V, A>(accumulator: (acc: A, value: V, index: number) => A, seed: A): OperatorFunction<V, A>;
+export function reduce<V, A, S = A>(accumulator: (acc: A | S, value: V, index: number) => A, seed: S): OperatorFunction<V, A>;
+
+/**
+ * Applies an accumulator function over the source Observable, and returns the
+ * accumulated result when the source completes, given an optional seed value.
+ *
+ * <span class="informal">Combines together all values emitted on the source,
+ * using an accumulator function that knows how to join a new source value into
+ * the accumulation from the past.</span>
+ *
+ * ![](reduce.png)
+ *
+ * Like
+ * [Array.prototype.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce),
+ * `reduce` applies an `accumulator` function against an accumulation and each
+ * value of the source Observable (from the past) to reduce it to a single
+ * value, emitted on the output Observable. Note that `reduce` will only emit
+ * one value, only when the source Observable completes. It is equivalent to
+ * applying operator {@link scan} followed by operator {@link last}.
+ *
+ * Returns an Observable that applies a specified `accumulator` function to each
+ * item emitted by the source Observable. If a `seed` value is specified, then
+ * that value will be used as the initial value for the accumulator. If no seed
+ * value is specified, the first item of the source is used as the seed.
+ *
+ * ## Example
+ *
+ * Count the number of click events that happened in 5 seconds
+ *
+ * ```ts
+ * import { fromEvent, takeUntil, interval, map, reduce } from 'rxjs';
+ *
+ * const clicksInFiveSeconds = fromEvent(document, 'click')
+ * .pipe(takeUntil(interval(5000)));
+ *
+ * const ones = clicksInFiveSeconds.pipe(map(() => 1));
+ * const seed = 0;
+ * const count = ones.pipe(reduce((acc, one) => acc + one, seed));
+ *
+ * count.subscribe(x => console.log(x));
+ * ```
+ *
+ * @see {@link count}
+ * @see {@link expand}
+ * @see {@link mergeScan}
+ * @see {@link scan}
+ *
+ * @param accumulator The accumulator function called on each source value.
+ * @param seed The initial accumulation value.
+ * @return A function that returns an Observable that emits a single value that
+ * is the result of accumulating the values emitted by the source Observable.
+ */
+export function reduce<V, A>(accumulator: (acc: V | A, value: V, index: number) => A, seed?: any): OperatorFunction<V, V | A> {
+ return operate(scanInternals(accumulator, seed, arguments.length >= 2, false, true));
+}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage