source: frontend/node_modules/es-abstract/2024/FindViaPredicate.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.5 KB
Line 
1'use strict';
2
3var $TypeError = require('es-errors/type');
4var isInteger = require('math-intrinsics/isInteger');
5var isObject = require('es-object-atoms/isObject');
6
7var Call = require('./Call');
8var Get = require('./Get');
9var ToBoolean = require('./ToBoolean');
10var IsCallable = require('./IsCallable');
11var ToString = require('./ToString');
12
13// https://262.ecma-international.org/15.0/#sec-findviapredicate
14
15module.exports = function FindViaPredicate(O, len, direction, predicate, thisArg) {
16 if (!isObject(O)) {
17 throw new $TypeError('Assertion failed: Type(O) is not Object');
18 }
19 if (!isInteger(len) || len < 0) {
20 throw new $TypeError('Assertion failed: len must be a non-negative integer');
21 }
22 if (direction !== 'ascending' && direction !== 'descending' && direction !== 'DESCENDING' && direction !== 'ASCENDING') {
23 throw new $TypeError('Assertion failed: direction must be ~ASCENDING~ or ~DESCENDING~');
24 }
25
26 if (!IsCallable(predicate)) {
27 throw new $TypeError('predicate must be callable'); // step 1
28 }
29
30 for ( // steps 2-4
31 var k = direction === 'ascending' || direction === 'ASCENDING' ? 0 : len - 1;
32 direction === 'ascending' || direction === 'ASCENDING' ? k < len : k >= 0;
33 k += 1
34 ) {
35 var Pk = ToString(k); // step 4.a
36 var kValue = Get(O, Pk); // step 4.c
37 var testResult = Call(predicate, thisArg, [kValue, k, O]); // step 4.d
38 if (ToBoolean(testResult)) {
39 return { '[[Index]]': k, '[[Value]]': kValue }; // step 4.e
40 }
41 }
42 return { '[[Index]]': -1, '[[Value]]': void undefined }; // step 5
43};
Note: See TracBrowser for help on using the repository browser.