1 | import { identity } from '../util/identity';
|
2 | import { bindCallback } from '../util/bindcallback';
|
3 | import { isIterable, isArrayLike, isIterator } from '../util/isiterable';
|
4 | import { toLength } from '../util/tolength';
|
5 |
|
6 | export let from;
|
7 |
|
8 | export let FromIterable;
|
9 | export function _initialize(Ctor) {
|
10 |
|
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 |
|
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 |
|