UNPKG

2.59 kBSource Map (JSON)View Raw
1{"version":3,"sources":["asynciterable/single.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAG/C;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAC1B,MAAwB,EACxB,OAAgC;IAEhC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE,CAAC,WAAW,CAAC,EAAE,SAAS,GAAG,KAAK,IAAI,EAAE,CAAC,IAAI,EAAE,GAC7F,OAAO,IAAI,EAAE,CAAC;IAChB,cAAc,CAAC,MAAM,CAAC,CAAC;IACvB,IAAI,MAAqB,CAAC;IAC1B,IAAI,SAAS,GAAG,KAAK,CAAC;IACtB,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,IAAI,KAAK,EAAE,MAAM,IAAI,IAAI,aAAa,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE;QACtD,IAAI,SAAS,IAAI,CAAC,MAAM,SAAU,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,EAAE;YACpE,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;SACpD;QACD,IAAI,MAAM,SAAU,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,MAAM,CAAC,EAAE;YACrD,MAAM,GAAG,IAAI,CAAC;YACd,SAAS,GAAG,IAAI,CAAC;SAClB;KACF;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","file":"single.js","sourcesContent":["import { wrapWithAbort } from './operators/withabort';\nimport { throwIfAborted } from '../aborterror';\nimport { OptionalFindOptions } from './findoptions';\n\n/**\n * Returns the only element of an async-iterable sequence that matches the predicate if specified,\n * or undefined if no such element exists; this method reports an exception if there is more\n * than one element in the async-iterable sequence.\n *\n * @export\n * @template T The type of the elements in the source sequence.\n * @param {AsyncIterable<T>} source Source async-iterable sequence.\n * @param {OptionalFindOptions<T>} [options] The optional options which includes a predicate for filtering,\n * thisArg for predicate binding and an abort signal for cancellation.\n * @returns {(Promise<T | undefined>)} A promise with the single element in the async-iterable sequence that satisfies\n * the condition in the predicate, or undefined if no such element exists.\n */\nexport async function single<T>(\n source: AsyncIterable<T>,\n options?: OptionalFindOptions<T>\n): Promise<T | undefined> {\n const { ['signal']: signal, ['thisArg']: thisArg, ['predicate']: predicate = async () => true } =\n options || {};\n throwIfAborted(signal);\n let result: T | undefined;\n let hasResult = false;\n let i = 0;\n for await (const item of wrapWithAbort(source, signal)) {\n if (hasResult && (await predicate!.call(thisArg, item, i++, signal))) {\n throw new Error('More than one element was found');\n }\n if (await predicate!.call(thisArg, item, i++, signal)) {\n result = item;\n hasResult = true;\n }\n }\n\n return result;\n}\n"]}
\No newline at end of file