1 |
- {"version":3,"file":"reduce.js","sourceRoot":"","sources":["../../src/operators/reduce.ts"],"names":[],"mappings":";AACA,qBAAqB,QAAQ,CAAC,CAAA;AAC9B,yBAAyB,YAAY,CAAC,CAAA;AACtC,+BAA+B,kBAAkB,CAAC,CAAA;AAElD,qBAAqB,cAAc,CAAC,CAAA;AAMpC,mCAAmC;AAEnC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,gBAA6B,WAAoD,EAAE,IAAQ;IACzF,gEAAgE;IAChE,qDAAqD;IACrD,sEAAsE;IACtE,oEAAoE;IACpE,+EAA+E;IAC/E,EAAE,CAAC,CAAC,SAAS,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC,CAAC;QAC1B,MAAM,CAAC,wCAAwC,MAAqB;YAClE,MAAM,CAAC,WAAI,CAAC,WAAI,CAAC,WAAW,EAAE,IAAI,CAAC,EAAE,mBAAQ,CAAC,CAAC,CAAC,EAAE,+BAAc,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;QAClF,CAAC,CAAC;IACJ,CAAC;IACD,MAAM,CAAC,gCAAgC,MAAqB;QAC1D,MAAM,CAAC,WAAI,CAAC,WAAI,CAAW,UAAC,GAAG,EAAE,KAAK,EAAE,KAAK;YAC3C,MAAM,CAAC,WAAW,CAAI,GAAG,EAAE,KAAK,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;QAC/C,CAAC,CAAC,EAAE,mBAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAkB,CAAC;IAC5C,CAAC,CAAC;AACJ,CAAC;AAhBe,cAAM,SAgBrB,CAAA","sourcesContent":["import { Observable } from '../Observable';\nimport { scan } from './scan';\nimport { takeLast } from './takeLast';\nimport { defaultIfEmpty } from './defaultIfEmpty';\nimport { OperatorFunction, MonoTypeOperatorFunction } from '../interfaces';\nimport { pipe } from '../util/pipe';\n\n/* tslint:disable:max-line-length */\nexport function reduce<T>(accumulator: (acc: T, value: T, index: number) => T, seed?: T): MonoTypeOperatorFunction<T>;\nexport function reduce<T>(accumulator: (acc: T[], value: T, index: number) => T[], seed: T[]): OperatorFunction<T, T[]>;\nexport function reduce<T, R>(accumulator: (acc: R, value: T, index: number) => R, seed?: R): OperatorFunction<T, R>;\n/* tslint:enable:max-line-length */\n\n/**\n * Applies an accumulator function over the source Observable, and returns the\n * accumulated result when the source completes, given an optional seed value.\n *\n * <span class=\"informal\">Combines together all values emitted on the source,\n * using an accumulator function that knows how to join a new source value into\n * the accumulation from the past.</span>\n *\n * <img src=\"./img/reduce.png\" width=\"100%\">\n *\n * Like\n * [Array.prototype.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce),\n * `reduce` applies an `accumulator` function against an accumulation and each\n * value of the source Observable (from the past) to reduce it to a single\n * value, emitted on the output Observable. Note that `reduce` will only emit\n * one value, only when the source Observable completes. It is equivalent to\n * applying operator {@link scan} followed by operator {@link last}.\n *\n * Returns an Observable that applies a specified `accumulator` function to each\n * item emitted by the source Observable. If a `seed` value is specified, then\n * that value will be used as the initial value for the accumulator. If no seed\n * value is specified, the first item of the source is used as the seed.\n *\n * @example <caption>Count the number of click events that happened in 5 seconds</caption>\n * var clicksInFiveSeconds = Rx.Observable.fromEvent(document, 'click')\n * .takeUntil(Rx.Observable.interval(5000));\n * var ones = clicksInFiveSeconds.mapTo(1);\n * var seed = 0;\n * var count = ones.reduce((acc, one) => acc + one, seed);\n * count.subscribe(x => console.log(x));\n *\n * @see {@link count}\n * @see {@link expand}\n * @see {@link mergeScan}\n * @see {@link scan}\n *\n * @param {function(acc: R, value: T, index: number): R} accumulator The accumulator function\n * called on each source value.\n * @param {R} [seed] The initial accumulation value.\n * @return {Observable<R>} An Observable that emits a single value that is the\n * result of accumulating the values emitted by the source Observable.\n * @method reduce\n * @owner Observable\n */\nexport function reduce<T, R>(accumulator: (acc: R, value: T, index?: number) => R, seed?: R): OperatorFunction<T, R> {\n // providing a seed of `undefined` *should* be valid and trigger\n // hasSeed! so don't use `seed !== undefined` checks!\n // For this reason, we have to check it here at the original call site\n // otherwise inside Operator/Subscriber we won't know if `undefined`\n // means they didn't provide anything or if they literally provided `undefined`\n if (arguments.length >= 2) {\n return function reduceOperatorFunctionWithSeed(source: Observable<T>): Observable<R> {\n return pipe(scan(accumulator, seed), takeLast(1), defaultIfEmpty(seed))(source);\n };\n }\n return function reduceOperatorFunction(source: Observable<T>): Observable<R> {\n return pipe(scan<T, T | R>((acc, value, index) => {\n return accumulator(<R>acc, value, index + 1);\n }), takeLast(1))(source) as Observable<R>;\n };\n}\n"]}
|