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