224 lines
6.3 KiB
JavaScript
224 lines
6.3 KiB
JavaScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
export var Iterable;
|
|
(function (Iterable) {
|
|
function is(thing) {
|
|
return thing && typeof thing === 'object' && typeof thing[Symbol.iterator] === 'function';
|
|
}
|
|
Iterable.is = is;
|
|
const _empty = Object.freeze([]);
|
|
function empty() {
|
|
return _empty;
|
|
}
|
|
Iterable.empty = empty;
|
|
function* single(element) {
|
|
yield element;
|
|
}
|
|
Iterable.single = single;
|
|
function wrap(iterableOrElement) {
|
|
if (is(iterableOrElement)) {
|
|
return iterableOrElement;
|
|
}
|
|
else {
|
|
return single(iterableOrElement);
|
|
}
|
|
}
|
|
Iterable.wrap = wrap;
|
|
function from(iterable) {
|
|
return iterable || _empty;
|
|
}
|
|
Iterable.from = from;
|
|
function* reverse(array) {
|
|
for (let i = array.length - 1; i >= 0; i--) {
|
|
yield array[i];
|
|
}
|
|
}
|
|
Iterable.reverse = reverse;
|
|
function isEmpty(iterable) {
|
|
return !iterable || iterable[Symbol.iterator]().next().done === true;
|
|
}
|
|
Iterable.isEmpty = isEmpty;
|
|
function first(iterable) {
|
|
return iterable[Symbol.iterator]().next().value;
|
|
}
|
|
Iterable.first = first;
|
|
function some(iterable, predicate) {
|
|
let i = 0;
|
|
for (const element of iterable) {
|
|
if (predicate(element, i++)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
Iterable.some = some;
|
|
function find(iterable, predicate) {
|
|
for (const element of iterable) {
|
|
if (predicate(element)) {
|
|
return element;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
Iterable.find = find;
|
|
function* filter(iterable, predicate) {
|
|
for (const element of iterable) {
|
|
if (predicate(element)) {
|
|
yield element;
|
|
}
|
|
}
|
|
}
|
|
Iterable.filter = filter;
|
|
function* map(iterable, fn) {
|
|
let index = 0;
|
|
for (const element of iterable) {
|
|
yield fn(element, index++);
|
|
}
|
|
}
|
|
Iterable.map = map;
|
|
function* flatMap(iterable, fn) {
|
|
let index = 0;
|
|
for (const element of iterable) {
|
|
yield* fn(element, index++);
|
|
}
|
|
}
|
|
Iterable.flatMap = flatMap;
|
|
function* concat(...iterables) {
|
|
for (const iterable of iterables) {
|
|
yield* iterable;
|
|
}
|
|
}
|
|
Iterable.concat = concat;
|
|
function reduce(iterable, reducer, initialValue) {
|
|
let value = initialValue;
|
|
for (const element of iterable) {
|
|
value = reducer(value, element);
|
|
}
|
|
return value;
|
|
}
|
|
Iterable.reduce = reduce;
|
|
/**
|
|
* Returns an iterable slice of the array, with the same semantics as `array.slice()`.
|
|
*/
|
|
function* slice(arr, from, to = arr.length) {
|
|
if (from < -arr.length) {
|
|
from = 0;
|
|
}
|
|
if (from < 0) {
|
|
from += arr.length;
|
|
}
|
|
if (to < 0) {
|
|
to += arr.length;
|
|
}
|
|
else if (to > arr.length) {
|
|
to = arr.length;
|
|
}
|
|
for (; from < to; from++) {
|
|
yield arr[from];
|
|
}
|
|
}
|
|
Iterable.slice = slice;
|
|
/**
|
|
* Consumes `atMost` elements from iterable and returns the consumed elements,
|
|
* and an iterable for the rest of the elements.
|
|
*/
|
|
function consume(iterable, atMost = Number.POSITIVE_INFINITY) {
|
|
const consumed = [];
|
|
if (atMost === 0) {
|
|
return [consumed, iterable];
|
|
}
|
|
const iterator = iterable[Symbol.iterator]();
|
|
for (let i = 0; i < atMost; i++) {
|
|
const next = iterator.next();
|
|
if (next.done) {
|
|
return [consumed, Iterable.empty()];
|
|
}
|
|
consumed.push(next.value);
|
|
}
|
|
return [consumed, { [Symbol.iterator]() { return iterator; } }];
|
|
}
|
|
Iterable.consume = consume;
|
|
async function asyncToArray(iterable) {
|
|
const result = [];
|
|
for await (const item of iterable) {
|
|
result.push(item);
|
|
}
|
|
return Promise.resolve(result);
|
|
}
|
|
Iterable.asyncToArray = asyncToArray;
|
|
})(Iterable || (Iterable = {}));
|
|
export class ArrayIterator {
|
|
items;
|
|
start;
|
|
end;
|
|
index;
|
|
constructor(items, start = 0, end = items.length) {
|
|
this.items = items;
|
|
this.start = start;
|
|
this.end = end;
|
|
this.index = start - 1;
|
|
}
|
|
first() {
|
|
this.index = this.start;
|
|
return this.current();
|
|
}
|
|
next() {
|
|
this.index = Math.min(this.index + 1, this.end);
|
|
return this.current();
|
|
}
|
|
current() {
|
|
if (this.index === this.start - 1 || this.index === this.end) {
|
|
return null;
|
|
}
|
|
return this.items[this.index];
|
|
}
|
|
}
|
|
export class ArrayNavigator extends ArrayIterator {
|
|
constructor(items, start = 0, end = items.length) {
|
|
super(items, start, end);
|
|
}
|
|
current() {
|
|
return super.current();
|
|
}
|
|
previous() {
|
|
this.index = Math.max(this.index - 1, this.start - 1);
|
|
return this.current();
|
|
}
|
|
first() {
|
|
this.index = this.start;
|
|
return this.current();
|
|
}
|
|
last() {
|
|
this.index = this.end - 1;
|
|
return this.current();
|
|
}
|
|
parent() {
|
|
return null;
|
|
}
|
|
}
|
|
export class MappedIterator {
|
|
iterator;
|
|
fn;
|
|
constructor(iterator, fn) {
|
|
this.iterator = iterator;
|
|
this.fn = fn;
|
|
// noop
|
|
}
|
|
next() { return this.fn(this.iterator.next()); }
|
|
}
|
|
export class MappedNavigator extends MappedIterator {
|
|
navigator;
|
|
constructor(navigator, fn) {
|
|
super(navigator, fn);
|
|
this.navigator = navigator;
|
|
}
|
|
current() { return this.fn(this.navigator.current()); }
|
|
previous() { return this.fn(this.navigator.previous()); }
|
|
parent() { return this.fn(this.navigator.parent()); }
|
|
first() { return this.fn(this.navigator.first()); }
|
|
last() { return this.fn(this.navigator.last()); }
|
|
next() { return this.fn(this.navigator.next()); }
|
|
}
|
|
//# sourceMappingURL=iterator.js.map
|