source: node_modules/es-toolkit/dist/compat/array/findLast.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.0 KB
Line 
1import { identity } from '../../function/identity.mjs';
2import { iteratee } from '../util/iteratee.mjs';
3import { toInteger } from '../util/toInteger.mjs';
4
5function findLast(source, _doesMatch = identity, fromIndex) {
6 if (!source) {
7 return undefined;
8 }
9 const length = Array.isArray(source) ? source.length : Object.keys(source).length;
10 fromIndex = toInteger(fromIndex ?? length - 1);
11 if (fromIndex < 0) {
12 fromIndex = Math.max(length + fromIndex, 0);
13 }
14 else {
15 fromIndex = Math.min(fromIndex, length - 1);
16 }
17 const doesMatch = iteratee(_doesMatch);
18 if (!Array.isArray(source)) {
19 const keys = Object.keys(source);
20 for (let i = fromIndex; i >= 0; i--) {
21 const key = keys[i];
22 const value = source[key];
23 if (doesMatch(value, key, source)) {
24 return value;
25 }
26 }
27 return undefined;
28 }
29 return source.slice(0, fromIndex + 1).findLast(doesMatch);
30}
31
32export { findLast };
Note: See TracBrowser for help on using the repository browser.