esnext.iterator.map.js 990 B

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