[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var $TypeError = require('es-errors/type');
|
---|
| 4 |
|
---|
| 5 | var Call = require('./Call');
|
---|
| 6 | var Get = require('./Get');
|
---|
| 7 | var ToBoolean = require('./ToBoolean');
|
---|
| 8 | var IsCallable = require('./IsCallable');
|
---|
| 9 | var ToString = require('./ToString');
|
---|
| 10 | var Type = require('./Type');
|
---|
| 11 |
|
---|
| 12 | var isInteger = require('../helpers/isInteger');
|
---|
| 13 |
|
---|
| 14 | module.exports = function FindViaPredicate(O, len, direction, predicate, thisArg) {
|
---|
| 15 | if (Type(O) !== 'Object') {
|
---|
| 16 | throw new $TypeError('Assertion failed: Type(O) is not Object');
|
---|
| 17 | }
|
---|
| 18 | if (!isInteger(len) || len < 0) {
|
---|
| 19 | throw new $TypeError('Assertion failed: len must be a non-negative integer');
|
---|
| 20 | }
|
---|
| 21 | if (direction !== 'ascending' && direction !== 'descending') {
|
---|
| 22 | throw new $TypeError('Assertion failed: direction must be "ascending" or "descending"');
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | if (!IsCallable(predicate)) {
|
---|
| 26 | throw new $TypeError('predicate must be callable'); // step 1
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | for ( // steps 2-4
|
---|
| 30 | var k = direction === 'ascending' ? 0 : len - 1;
|
---|
| 31 | direction === 'ascending' ? k < len : k >= 0;
|
---|
| 32 | k += 1
|
---|
| 33 | ) {
|
---|
| 34 | var Pk = ToString(k); // step 4.a
|
---|
| 35 | var kValue = Get(O, Pk); // step 4.c
|
---|
| 36 | var testResult = Call(predicate, thisArg, [kValue, k, O]); // step 4.d
|
---|
| 37 | if (ToBoolean(testResult)) {
|
---|
| 38 | return { '[[Index]]': k, '[[Value]]': kValue }; // step 4.e
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 | return { '[[Index]]': -1, '[[Value]]': void undefined }; // step 5
|
---|
| 42 | };
|
---|