es.iterator.for-each.js 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var call = require('../internals/function-call');
  4. var iterate = require('../internals/iterate');
  5. var aCallable = require('../internals/a-callable');
  6. var anObject = require('../internals/an-object');
  7. var getIteratorDirect = require('../internals/get-iterator-direct');
  8. var iteratorClose = require('../internals/iterator-close');
  9. var iteratorHelperWithoutClosingOnEarlyError = require('../internals/iterator-helper-without-closing-on-early-error');
  10. var forEachWithoutClosingOnEarlyError = iteratorHelperWithoutClosingOnEarlyError('forEach', TypeError);
  11. // `Iterator.prototype.forEach` method
  12. // https://tc39.es/ecma262/#sec-iterator.prototype.foreach
  13. $({ target: 'Iterator', proto: true, real: true, forced: forEachWithoutClosingOnEarlyError }, {
  14. forEach: function forEach(fn) {
  15. anObject(this);
  16. try {
  17. aCallable(fn);
  18. } catch (error) {
  19. iteratorClose(this, 'throw', error);
  20. }
  21. if (forEachWithoutClosingOnEarlyError) return call(forEachWithoutClosingOnEarlyError, this, fn);
  22. var record = getIteratorDirect(this);
  23. var counter = 0;
  24. iterate(record, function (value) {
  25. fn(value, counter++);
  26. }, { IS_RECORD: true });
  27. }
  28. });