UNPKG

979 BJavaScriptView Raw
1/**
2 * Returns the last element of an iterable sequence that satisfies the condition in the predicate if given
3 * otherwise the last item in the sequence, or a default value if no such element exists.
4 *
5 * @export
6 * @template T The type of elements in the source sequence.
7 * @param {Iterable<T>} source The source iterable sequence.
8 * @param {OptionalFindSubclassedOptions<T, S>} [options] The options which include an optional predicate for filtering,
9 * thirArg for binding, and abort signal for cancellation
10 * @returns {(S | undefined)} The last value that matches the optional predicate or last item, otherwise undefined.
11 */
12export function last(source, options) {
13 const { ['thisArg']: thisArg, ['predicate']: predicate = () => true } = options || {};
14 let i = 0;
15 let result;
16 for (const item of source) {
17 if (predicate.call(thisArg, item, i++)) {
18 result = item;
19 }
20 }
21 return result;
22}
23
24//# sourceMappingURL=last.mjs.map