UNPKG

1.63 kBJavaScriptView Raw
1import { identity } from '../util/identity';
2import { bindCallback } from '../util/bindcallback';
3import { isIterable, isArrayLike, isIterator } from '../util/isiterable';
4import { toLength } from '../util/tolength';
5/** @nocollapse */
6export let from;
7/** @nocollapse */
8export let FromIterable;
9export function _initialize(Ctor) {
10 /** @nocollapse */
11 from = function (source, selector = identity, thisArg) {
12 const fn = bindCallback(selector, thisArg, 2);
13 if (isIterable(source)) {
14 return new FromIterable(source, fn);
15 }
16 if (isArrayLike(source)) {
17 return new FromIterable(source, fn);
18 }
19 if (isIterator(source)) {
20 return new FromIterable({ [Symbol.iterator]: () => source }, fn);
21 }
22 throw new TypeError('Input type not supported');
23 };
24 // eslint-disable-next-line no-shadow
25 FromIterable = class FromIterable extends Ctor {
26 constructor(source, fn) {
27 super();
28 this._source = source;
29 this._fn = fn;
30 }
31 *[Symbol.iterator]() {
32 const iterable = isIterable(this._source);
33 let i = 0;
34 if (iterable) {
35 for (const item of this._source) {
36 yield this._fn(item, i++);
37 }
38 }
39 else {
40 const length = toLength(this._source.length);
41 while (i < length) {
42 const val = this._source[i];
43 yield this._fn(val, i++);
44 }
45 }
46 }
47 };
48}
49
50//# sourceMappingURL=from.mjs.map