IterableHelpers.js 962 B

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