iterator.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. function forEach(iterable, fn) {
  90. let index = 0;
  91. for (const element of iterable) {
  92. fn(element, index++);
  93. }
  94. }
  95. Iterable.forEach = forEach;
  96. /**
  97. * Returns an iterable slice of the array, with the same semantics as `array.slice()`.
  98. */
  99. function* slice(arr, from, to = arr.length) {
  100. if (from < 0) {
  101. from += arr.length;
  102. }
  103. if (to < 0) {
  104. to += arr.length;
  105. }
  106. else if (to > arr.length) {
  107. to = arr.length;
  108. }
  109. for (; from < to; from++) {
  110. yield arr[from];
  111. }
  112. }
  113. Iterable.slice = slice;
  114. /**
  115. * Consumes `atMost` elements from iterable and returns the consumed elements,
  116. * and an iterable for the rest of the elements.
  117. */
  118. function consume(iterable, atMost = Number.POSITIVE_INFINITY) {
  119. const consumed = [];
  120. if (atMost === 0) {
  121. return [consumed, iterable];
  122. }
  123. const iterator = iterable[Symbol.iterator]();
  124. for (let i = 0; i < atMost; i++) {
  125. const next = iterator.next();
  126. if (next.done) {
  127. return [consumed, Iterable.empty()];
  128. }
  129. consumed.push(next.value);
  130. }
  131. return [consumed, { [Symbol.iterator]() { return iterator; } }];
  132. }
  133. Iterable.consume = consume;
  134. /**
  135. * Consumes `atMost` elements from iterable and returns the consumed elements,
  136. * and an iterable for the rest of the elements.
  137. */
  138. function collect(iterable) {
  139. return consume(iterable)[0];
  140. }
  141. Iterable.collect = collect;
  142. /**
  143. * Returns whether the iterables are the same length and all items are
  144. * equal using the comparator function.
  145. */
  146. function equals(a, b, comparator = (at, bt) => at === bt) {
  147. const ai = a[Symbol.iterator]();
  148. const bi = b[Symbol.iterator]();
  149. while (true) {
  150. const an = ai.next();
  151. const bn = bi.next();
  152. if (an.done !== bn.done) {
  153. return false;
  154. }
  155. else if (an.done) {
  156. return true;
  157. }
  158. else if (!comparator(an.value, bn.value)) {
  159. return false;
  160. }
  161. }
  162. }
  163. Iterable.equals = equals;
  164. })(Iterable || (Iterable = {}));