UNPKG

903 BJavaScriptView Raw
1/**
2 * Returns a promise that represents how many elements in the specified iterable sequence satisfy a condition
3 * otherwise, the number of items in the sequence.
4 *
5 * @export
6 * @template T The type of elements in the source collection.
7 * @param {Iterable<T>} source An iterable sequence that contains elements to be counted.
8 * @param {OptionalFindOptions<T>} [options] The options for a predicate for filtering and thisArg for binding.
9 * @returns {number} The number of matching elements for the given condition if provided, otherwise
10 * the number of elements in the sequence.
11 */
12export function count(source, options) {
13 const { ['thisArg']: thisArg, ['predicate']: predicate = () => true } = options || {};
14 let i = 0;
15 for (const item of source) {
16 if (predicate.call(thisArg, item, i)) {
17 i++;
18 }
19 }
20 return i;
21}
22
23//# sourceMappingURL=count.mjs.map