iterator.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. export var Iterable;
  6. (function (Iterable) {
  7. function is(thing) {
  8. return thing && typeof thing === 'object' && typeof thing[Symbol.iterator] === 'function';
  9. }
  10. Iterable.is = is;
  11. const _empty = Object.freeze([]);
  12. function empty() {
  13. return _empty;
  14. }
  15. Iterable.empty = empty;
  16. function* single(element) {
  17. yield element;
  18. }
  19. Iterable.single = single;
  20. function from(iterable) {
  21. return iterable || _empty;
  22. }
  23. Iterable.from = from;
  24. function isEmpty(iterable) {
  25. return !iterable || iterable[Symbol.iterator]().next().done === true;
  26. }
  27. Iterable.isEmpty = isEmpty;
  28. function first(iterable) {
  29. return iterable[Symbol.iterator]().next().value;
  30. }
  31. Iterable.first = first;
  32. function some(iterable, predicate) {
  33. for (const element of iterable) {
  34. if (predicate(element)) {
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40. Iterable.some = some;
  41. function find(iterable, predicate) {
  42. for (const element of iterable) {
  43. if (predicate(element)) {
  44. return element;
  45. }
  46. }
  47. return undefined;
  48. }
  49. Iterable.find = find;
  50. function* filter(iterable, predicate) {
  51. for (const element of iterable) {
  52. if (predicate(element)) {
  53. yield element;
  54. }
  55. }
  56. }
  57. Iterable.filter = filter;
  58. function* map(iterable, fn) {
  59. let index = 0;
  60. for (const element of iterable) {
  61. yield fn(element, index++);
  62. }
  63. }
  64. Iterable.map = map;
  65. function* concat(...iterables) {
  66. for (const iterable of iterables) {
  67. for (const element of iterable) {
  68. yield element;
  69. }
  70. }
  71. }
  72. Iterable.concat = concat;
  73. function* concatNested(iterables) {
  74. for (const iterable of iterables) {
  75. for (const element of iterable) {
  76. yield element;
  77. }
  78. }
  79. }
  80. Iterable.concatNested = concatNested;
  81. function reduce(iterable, reducer, initialValue) {
  82. let value = initialValue;
  83. for (const element of iterable) {
  84. value = reducer(value, element);
  85. }
  86. return value;
  87. }
  88. Iterable.reduce = reduce;
  89. /**
  90. * Returns an iterable slice of the array, with the same semantics as `array.slice()`.
  91. */
  92. function* slice(arr, from, to = arr.length) {
  93. if (from < 0) {
  94. from += arr.length;
  95. }
  96. if (to < 0) {
  97. to += arr.length;
  98. }
  99. else if (to > arr.length) {
  100. to = arr.length;
  101. }
  102. for (; from < to; from++) {
  103. yield arr[from];
  104. }
  105. }
  106. Iterable.slice = slice;
  107. /**
  108. * Consumes `atMost` elements from iterable and returns the consumed elements,
  109. * and an iterable for the rest of the elements.
  110. */
  111. function consume(iterable, atMost = Number.POSITIVE_INFINITY) {
  112. const consumed = [];
  113. if (atMost === 0) {
  114. return [consumed, iterable];
  115. }
  116. const iterator = iterable[Symbol.iterator]();
  117. for (let i = 0; i < atMost; i++) {
  118. const next = iterator.next();
  119. if (next.done) {
  120. return [consumed, Iterable.empty()];
  121. }
  122. consumed.push(next.value);
  123. }
  124. return [consumed, { [Symbol.iterator]() { return iterator; } }];
  125. }
  126. Iterable.consume = consume;
  127. /**
  128. * Returns whether the iterables are the same length and all items are
  129. * equal using the comparator function.
  130. */
  131. function equals(a, b, comparator = (at, bt) => at === bt) {
  132. const ai = a[Symbol.iterator]();
  133. const bi = b[Symbol.iterator]();
  134. while (true) {
  135. const an = ai.next();
  136. const bn = bi.next();
  137. if (an.done !== bn.done) {
  138. return false;
  139. }
  140. else if (an.done) {
  141. return true;
  142. }
  143. else if (!comparator(an.value, bn.value)) {
  144. return false;
  145. }
  146. }
  147. }
  148. Iterable.equals = equals;
  149. })(Iterable || (Iterable = {}));