source: node_modules/es-toolkit/dist/compat/array/findLastIndex.mjs

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 
1import { identity } from '../../function/identity.mjs';
2import { toArray } from '../_internal/toArray.mjs';
3import { property } from '../object/property.mjs';
4import { matches } from '../predicate/matches.mjs';
5import { matchesProperty } from '../predicate/matchesProperty.mjs';
6
7function findLastIndex(arr, doesMatch = identity, fromIndex = arr ? arr.length - 1 : 0) {
8 if (!arr) {
9 return -1;
10 }
11 if (fromIndex < 0) {
12 fromIndex = Math.max(arr.length + fromIndex, 0);
13 }
14 else {
15 fromIndex = Math.min(fromIndex, arr.length - 1);
16 }
17 const subArray = toArray(arr).slice(0, fromIndex + 1);
18 switch (typeof doesMatch) {
19 case 'function': {
20 return subArray.findLastIndex(doesMatch);
21 }
22 case 'object': {
23 if (Array.isArray(doesMatch) && doesMatch.length === 2) {
24 const key = doesMatch[0];
25 const value = doesMatch[1];
26 return subArray.findLastIndex(matchesProperty(key, value));
27 }
28 else {
29 return subArray.findLastIndex(matches(doesMatch));
30 }
31 }
32 case 'number':
33 case 'symbol':
34 case 'string': {
35 return subArray.findLastIndex(property(doesMatch));
36 }
37 }
38}
39
40export { findLastIndex };
Note: See TracBrowser for help on using the repository browser.