1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var call = require('../internals/function-call');
|
---|
4 | var aCallable = require('../internals/a-callable');
|
---|
5 | var anObject = require('../internals/an-object');
|
---|
6 | var getIteratorDirect = require('../internals/get-iterator-direct');
|
---|
7 | var getIteratorFlattenable = require('../internals/get-iterator-flattenable');
|
---|
8 | var createIteratorProxy = require('../internals/iterator-create-proxy');
|
---|
9 | var iteratorClose = require('../internals/iterator-close');
|
---|
10 | var IS_PURE = require('../internals/is-pure');
|
---|
11 |
|
---|
12 | var IteratorProxy = createIteratorProxy(function () {
|
---|
13 | var iterator = this.iterator;
|
---|
14 | var mapper = this.mapper;
|
---|
15 | var result, inner;
|
---|
16 |
|
---|
17 | while (true) {
|
---|
18 | if (inner = this.inner) try {
|
---|
19 | result = anObject(call(inner.next, inner.iterator));
|
---|
20 | if (!result.done) return result.value;
|
---|
21 | this.inner = null;
|
---|
22 | } catch (error) { iteratorClose(iterator, 'throw', error); }
|
---|
23 |
|
---|
24 | result = anObject(call(this.next, iterator));
|
---|
25 |
|
---|
26 | if (this.done = !!result.done) return;
|
---|
27 |
|
---|
28 | try {
|
---|
29 | this.inner = getIteratorFlattenable(mapper(result.value, this.counter++), false);
|
---|
30 | } catch (error) { iteratorClose(iterator, 'throw', error); }
|
---|
31 | }
|
---|
32 | });
|
---|
33 |
|
---|
34 | // `Iterator.prototype.flatMap` method
|
---|
35 | // https://tc39.es/ecma262/#sec-iterator.prototype.flatmap
|
---|
36 | $({ target: 'Iterator', proto: true, real: true, forced: IS_PURE }, {
|
---|
37 | flatMap: function flatMap(mapper) {
|
---|
38 | anObject(this);
|
---|
39 | aCallable(mapper);
|
---|
40 | return new IteratorProxy(getIteratorDirect(this), {
|
---|
41 | mapper: mapper,
|
---|
42 | inner: null
|
---|
43 | });
|
---|
44 | }
|
---|
45 | });
|
---|