UNPKG

945 BJavaScriptView Raw
1'use strict';
2var call = require('../internals/function-call');
3var aCallable = require('../internals/a-callable');
4var anObject = require('../internals/an-object');
5var getIteratorDirect = require('../internals/get-iterator-direct');
6var createIteratorProxy = require('../internals/iterator-create-proxy');
7var callWithSafeIterationClosing = require('../internals/call-with-safe-iteration-closing');
8
9var IteratorProxy = createIteratorProxy(function () {
10 var iterator = this.iterator;
11 var result = anObject(call(this.next, iterator));
12 var done = this.done = !!result.done;
13 if (!done) return callWithSafeIterationClosing(iterator, this.mapper, [result.value, this.counter++], true);
14});
15
16// `Iterator.prototype.map` method
17// https://github.com/tc39/proposal-iterator-helpers
18module.exports = function map(mapper) {
19 anObject(this);
20 aCallable(mapper);
21 return new IteratorProxy(getIteratorDirect(this), {
22 mapper: mapper
23 });
24};