source: imaps-frontend/node_modules/es-abstract/2023/FindViaPredicate.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.3 KB
Line 
1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var Call = require('./Call');
6var Get = require('./Get');
7var ToBoolean = require('./ToBoolean');
8var IsCallable = require('./IsCallable');
9var ToString = require('./ToString');
10var Type = require('./Type');
11
12var isInteger = require('../helpers/isInteger');
13
14module.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};
Note: See TracBrowser for help on using the repository browser.