es.iterator.find.js 1.3 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 findWithoutClosingOnEarlyError = iteratorHelperWithoutClosingOnEarlyError('find', TypeError);
  11. // `Iterator.prototype.find` method
  12. // https://tc39.es/ecma262/#sec-iterator.prototype.find
  13. $({ target: 'Iterator', proto: true, real: true, forced: findWithoutClosingOnEarlyError }, {
  14. find: function find(predicate) {
  15. anObject(this);
  16. try {
  17. aCallable(predicate);
  18. } catch (error) {
  19. iteratorClose(this, 'throw', error);
  20. }
  21. if (findWithoutClosingOnEarlyError) return call(findWithoutClosingOnEarlyError, this, predicate);
  22. var record = getIteratorDirect(this);
  23. var counter = 0;
  24. return iterate(record, function (value, stop) {
  25. if (predicate(value, counter++)) return stop(value);
  26. }, { IS_RECORD: true, INTERRUPTED: true }).result;
  27. }
  28. });