UNPKG

962 BJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8/**
9 * @template T
10 * @param {Iterable<T>} set a set
11 * @returns {T | undefined} last item
12 */
13const last = set => {
14 let last;
15 for (const item of set) last = item;
16 return last;
17};
18
19/**
20 * @template T
21 * @param {Iterable<T>} iterable iterable
22 * @param {function(T): boolean} filter predicate
23 * @returns {boolean} true, if some items match the filter predicate
24 */
25const someInIterable = (iterable, filter) => {
26 for (const item of iterable) {
27 if (filter(item)) return true;
28 }
29 return false;
30};
31
32/**
33 * @template T
34 * @param {Iterable<T>} iterable an iterable
35 * @returns {number} count of items
36 */
37const countIterable = iterable => {
38 let i = 0;
39 // eslint-disable-next-line no-unused-vars
40 for (const _ of iterable) i++;
41 return i;
42};
43
44exports.last = last;
45exports.someInIterable = someInIterable;
46exports.countIterable = countIterable;