|
Last change
on this file since ba17441 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1.2 KB
|
| Line | |
|---|
| 1 | import { property } from '../object/property.mjs';
|
|---|
| 2 | import { matches } from '../predicate/matches.mjs';
|
|---|
| 3 | import { matchesProperty } from '../predicate/matchesProperty.mjs';
|
|---|
| 4 |
|
|---|
| 5 | function findIndex(arr, doesMatch, fromIndex = 0) {
|
|---|
| 6 | if (!arr) {
|
|---|
| 7 | return -1;
|
|---|
| 8 | }
|
|---|
| 9 | if (fromIndex < 0) {
|
|---|
| 10 | fromIndex = Math.max(arr.length + fromIndex, 0);
|
|---|
| 11 | }
|
|---|
| 12 | const subArray = Array.from(arr).slice(fromIndex);
|
|---|
| 13 | let index = -1;
|
|---|
| 14 | switch (typeof doesMatch) {
|
|---|
| 15 | case 'function': {
|
|---|
| 16 | index = subArray.findIndex(doesMatch);
|
|---|
| 17 | break;
|
|---|
| 18 | }
|
|---|
| 19 | case 'object': {
|
|---|
| 20 | if (Array.isArray(doesMatch) && doesMatch.length === 2) {
|
|---|
| 21 | const key = doesMatch[0];
|
|---|
| 22 | const value = doesMatch[1];
|
|---|
| 23 | index = subArray.findIndex(matchesProperty(key, value));
|
|---|
| 24 | }
|
|---|
| 25 | else {
|
|---|
| 26 | index = subArray.findIndex(matches(doesMatch));
|
|---|
| 27 | }
|
|---|
| 28 | break;
|
|---|
| 29 | }
|
|---|
| 30 | case 'number':
|
|---|
| 31 | case 'symbol':
|
|---|
| 32 | case 'string': {
|
|---|
| 33 | index = subArray.findIndex(property(doesMatch));
|
|---|
| 34 | }
|
|---|
| 35 | }
|
|---|
| 36 | return index === -1 ? -1 : index + fromIndex;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | export { findIndex };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.