UNPKG

2.92 kBSource Map (JSON)View Raw
1{"version":3,"sources":["iterable/reduce.ts"],"names":[],"mappings":"AAoBA,MAAM,UAAU,MAAM,CACpB,MAAmB,EACnB,oBAA8F,EAC9F,IAAQ;IAER,MAAM,OAAO;IACX,6CAA6C;IAC7C,OAAO,oBAAoB,KAAK,UAAU;QACxC,CAAC,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC;YACpB,CAAC,CAAC,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,EAAE,IAAI,EAAE;YACpD,CAAC,CAAC,EAAE,UAAU,EAAE,oBAAoB,EAAE;QACxC,CAAC,CAAC,oBAAoB,CAAC;IAC3B,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,UAAU,CAAC,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;IAC5D,MAAM,OAAO,GAAG,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IAC/C,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,IAAI,QAAQ,GAAG,KAAK,CAAC;IACrB,IAAI,GAAG,GAAG,KAAc,CAAC;IACzB,KAAK,MAAM,IAAI,IAAI,MAAM,EAAE;QACzB,IAAI,QAAQ,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,EAAE;YACpC,GAAG,GAAG,QAAQ,CAAI,GAAG,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC;SACnC;aAAM;YACL,GAAG,GAAG,IAAI,CAAC;YACX,QAAQ,GAAG,IAAI,CAAC;YAChB,CAAC,EAAE,CAAC;SACL;KACF;IAED,IAAI,CAAC,CAAC,OAAO,IAAI,QAAQ,CAAC,EAAE;QAC1B,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;KAClD;IAED,OAAO,GAAQ,CAAC;AAClB,CAAC","file":"reduce.js","sourcesContent":["import { ReduceOptions } from './reduceoptions';\n\n/**\n * Applies an accumulator function over an iterable sequence, returning the result of the aggregation as a\n * single element in the result sequence. The seed value, if specified, is used as the initial accumulator value.\n * For aggregation behavior with incremental intermediate results, scan.\n *\n * @export\n * @template T The type of the elements in the source sequence.\n * @template R The type of the result of the aggregation.\n * @param {Iterable<T>} source An iterable sequence to aggregate over.\n * @param {ReduceOptions<T, R>} options The options which contains a callback and optional seed.\n * @returns {R} The final accumulator value.\n */\nexport function reduce<T, R = T>(source: Iterable<T>, options: ReduceOptions<T, R>): R;\nexport function reduce<T, R = T>(\n source: Iterable<T>,\n accumulator: (accumulator: R, current: T, index: number) => R,\n seed?: R\n): R;\nexport function reduce<T, R = T>(\n source: Iterable<T>,\n optionsOrAccumulator: ReduceOptions<T, R> | ((accumulator: R, current: T, index: number) => R),\n seed?: R\n): R {\n const options =\n // eslint-disable-next-line no-nested-ternary\n typeof optionsOrAccumulator === 'function'\n ? arguments.length > 2\n ? { 'callback': optionsOrAccumulator, 'seed': seed }\n : { 'callback': optionsOrAccumulator }\n : optionsOrAccumulator;\n const { ['seed']: _seed, ['callback']: callback } = options;\n const hasSeed = options.hasOwnProperty('seed');\n let i = 0;\n let hasValue = false;\n let acc = _seed as T | R;\n for (const item of source) {\n if (hasValue || (hasValue = hasSeed)) {\n acc = callback(<R>acc, item, i++);\n } else {\n acc = item;\n hasValue = true;\n i++;\n }\n }\n\n if (!(hasSeed || hasValue)) {\n throw new Error('Sequence contains no elements');\n }\n\n return acc as R;\n}\n"]}
\No newline at end of file