UNPKG

2.02 kBSource Map (JSON)View Raw
1{"version":3,"sources":["iterable/single.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,MAAM,CAAI,MAAmB,EAAE,OAAgC;IAC7E,MAAM,EAAE,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE,CAAC,WAAW,CAAC,EAAE,SAAS,GAAG,GAAG,EAAE,CAAC,IAAI,EAAE,GAAG,OAAO,IAAI,EAAE,CAAC;IACtF,IAAI,MAAqB,CAAC;IAC1B,IAAI,SAAS,GAAG,KAAK,CAAC;IACtB,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,KAAK,MAAM,IAAI,IAAI,MAAM,EAAE;QACzB,IAAI,SAAS,IAAI,SAAU,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE;YACpD,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;SACpD;QACD,IAAI,SAAU,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE;YACvC,MAAM,GAAG,IAAI,CAAC;YACd,SAAS,GAAG,IAAI,CAAC;SAClB;KACF;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","file":"single.js","sourcesContent":["import { OptionalFindOptions } from './findoptions';\n\n/**\n * Returns the only element of an 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 iterable sequence.\n *\n * @export\n * @template T The type of the elements in the source sequence.\n * @param {AsyncIterable<T>} source Source iterable sequence.\n * @param {OptionalFindOptions<T>} [options] The optional options which includes a predicate for filtering,\n * and thisArg for predicate binding.\n * @returns {(T | undefined)} The single element in the iterable sequence that satisfies\n * the condition in the predicate, or undefined if no such element exists.\n */\nexport function single<T>(source: Iterable<T>, options?: OptionalFindOptions<T>): T | undefined {\n const { ['thisArg']: thisArg, ['predicate']: predicate = () => true } = options || {};\n let result: T | undefined;\n let hasResult = false;\n let i = 0;\n for (const item of source) {\n if (hasResult && predicate!.call(thisArg, item, i++)) {\n throw new Error('More than one element was found');\n }\n if (predicate!.call(thisArg, item, i++)) {\n result = item;\n hasResult = true;\n }\n }\n\n return result;\n}\n"]}
\No newline at end of file