es.iterator.map.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var call = require('../internals/function-call');
  4. var aCallable = require('../internals/a-callable');
  5. var anObject = require('../internals/an-object');
  6. var getIteratorDirect = require('../internals/get-iterator-direct');
  7. var createIteratorProxy = require('../internals/iterator-create-proxy');
  8. var callWithSafeIterationClosing = require('../internals/call-with-safe-iteration-closing');
  9. var iteratorClose = require('../internals/iterator-close');
  10. var iteratorHelperWithoutClosingOnEarlyError = require('../internals/iterator-helper-without-closing-on-early-error');
  11. var IS_PURE = require('../internals/is-pure');
  12. var mapWithoutClosingOnEarlyError = !IS_PURE && iteratorHelperWithoutClosingOnEarlyError('map', TypeError);
  13. var IteratorProxy = createIteratorProxy(function () {
  14. var iterator = this.iterator;
  15. var result = anObject(call(this.next, iterator));
  16. var done = this.done = !!result.done;
  17. if (!done) return callWithSafeIterationClosing(iterator, this.mapper, [result.value, this.counter++], true);
  18. });
  19. // `Iterator.prototype.map` method
  20. // https://tc39.es/ecma262/#sec-iterator.prototype.map
  21. $({ target: 'Iterator', proto: true, real: true, forced: IS_PURE || mapWithoutClosingOnEarlyError }, {
  22. map: function map(mapper) {
  23. anObject(this);
  24. try {
  25. aCallable(mapper);
  26. } catch (error) {
  27. iteratorClose(this, 'throw', error);
  28. }
  29. if (mapWithoutClosingOnEarlyError) return call(mapWithoutClosingOnEarlyError, this, mapper);
  30. return new IteratorProxy(getIteratorDirect(this), {
  31. mapper: mapper
  32. });
  33. }
  34. });