UNPKG

2.61 kBSource Map (JSON)View Raw
1{"version":3,"sources":["asynciterable/reduce.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAC1B,MAAwB,EACxB,OAA4B;IAE5B,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC,UAAU,CAAC,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;IAC/E,MAAM,OAAO,GAAG,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IAC/C,cAAc,CAAC,MAAM,CAAC,CAAC;IACvB,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,IAAI,QAAQ,GAAG,KAAK,CAAC;IACrB,IAAI,GAAG,GAAG,IAAa,CAAC;IACxB,IAAI,KAAK,EAAE,MAAM,IAAI,IAAI,aAAa,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE;QACtD,IAAI,QAAQ,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,EAAE;YACpC,GAAG,GAAG,MAAM,QAAQ,CAAI,GAAG,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;SACjD;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';\nimport { wrapWithAbort } from './operators/withabort';\nimport { throwIfAborted } from '../aborterror';\n\n/**\n * Applies an accumulator function over an async-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 {AsyncIterable<T>} source An async-iterable sequence to aggregate over.\n * @param {ReduceOptions<T, R>} options The options which contains a callback, with optional seedn and an optional abort signal for cancellation.\n * @returns {Promise<R>} A promise with the final accumulator value.\n */\nexport async function reduce<T, R = T>(\n source: AsyncIterable<T>,\n options: ReduceOptions<T, R>\n): Promise<R> {\n const { ['seed']: seed, ['signal']: signal, ['callback']: callback } = options;\n const hasSeed = options.hasOwnProperty('seed');\n throwIfAborted(signal);\n let i = 0;\n let hasValue = false;\n let acc = seed as T | R;\n for await (const item of wrapWithAbort(source, signal)) {\n if (hasValue || (hasValue = hasSeed)) {\n acc = await callback(<R>acc, item, i++, signal);\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