esnext.iterator.map.js 917 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. // https://github.com/tc39/proposal-iterator-helpers
  3. var $ = require('../internals/export');
  4. var apply = require('../internals/function-apply');
  5. var aCallable = require('../internals/a-callable');
  6. var anObject = require('../internals/an-object');
  7. var createIteratorProxy = require('../internals/iterator-create-proxy');
  8. var callWithSafeIterationClosing = require('../internals/call-with-safe-iteration-closing');
  9. var IteratorProxy = createIteratorProxy(function (args) {
  10. var iterator = this.iterator;
  11. var result = anObject(apply(this.next, iterator, args));
  12. var done = this.done = !!result.done;
  13. if (!done) return callWithSafeIterationClosing(iterator, this.mapper, result.value);
  14. });
  15. $({ target: 'Iterator', proto: true, real: true, forced: true }, {
  16. map: function map(mapper) {
  17. return new IteratorProxy({
  18. iterator: anObject(this),
  19. mapper: aCallable(mapper)
  20. });
  21. }
  22. });