esnext.iterator.map.js 847 B

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