UNPKG

1.41 kBSource Map (JSON)View Raw
1{"version":3,"sources":["iterable/first.ts"],"names":[],"mappings":"AAiBA,MAAM,UAAU,KAAK,CACnB,MAAmB,EACnB,YAAkD,GAAG,EAAE,CAAC,IAAI;IAE5D,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,KAAK,MAAM,IAAI,IAAI,MAAM,EAAE;QACzB,IAAI,SAAS,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE;YACxB,OAAO,IAAI,CAAC;SACb;KACF;IAED,OAAO,SAAS,CAAC;AACnB,CAAC","file":"first.js","sourcesContent":["/**\n * Returns the first element in a sequence that satisfies a specified condition if provided, else\n * the first element in the sequence.\n * @param {Iterable<T>} source Source collection\n * @param {function:(value: T): boolean} [selector] An optional function to test each element for a condition.\n * @returns {T | undefined} The first element in the sequence that passes the test in the\n * specified predicate function if provided, else the first element. If there are no elements,\n * undefined is returned.\n */\nexport function first<T, S extends T>(\n source: Iterable<T>,\n predicate: (value: T, index: number) => value is S\n): S | undefined;\nexport function first<T>(\n source: Iterable<T>,\n predicate?: (value: T, index: number) => boolean\n): T | undefined;\nexport function first<T>(\n source: Iterable<T>,\n predicate: (value: T, index: number) => boolean = () => true\n): T | undefined {\n let i = 0;\n for (const item of source) {\n if (predicate(item, i++)) {\n return item;\n }\n }\n\n return undefined;\n}\n"]}
\No newline at end of file