source: node_modules/es-toolkit/dist/compat/array/findIndex.js

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 1.3 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
5const property = require('../object/property.js');
6const matches = require('../predicate/matches.js');
7const matchesProperty = require('../predicate/matchesProperty.js');
8
9function findIndex(arr, doesMatch, fromIndex = 0) {
10 if (!arr) {
11 return -1;
12 }
13 if (fromIndex < 0) {
14 fromIndex = Math.max(arr.length + fromIndex, 0);
15 }
16 const subArray = Array.from(arr).slice(fromIndex);
17 let index = -1;
18 switch (typeof doesMatch) {
19 case 'function': {
20 index = subArray.findIndex(doesMatch);
21 break;
22 }
23 case 'object': {
24 if (Array.isArray(doesMatch) && doesMatch.length === 2) {
25 const key = doesMatch[0];
26 const value = doesMatch[1];
27 index = subArray.findIndex(matchesProperty.matchesProperty(key, value));
28 }
29 else {
30 index = subArray.findIndex(matches.matches(doesMatch));
31 }
32 break;
33 }
34 case 'number':
35 case 'symbol':
36 case 'string': {
37 index = subArray.findIndex(property.property(doesMatch));
38 }
39 }
40 return index === -1 ? -1 : index + fromIndex;
41}
42
43exports.findIndex = findIndex;
Note: See TracBrowser for help on using the repository browser.