iterator-map.js 945 B

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