main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[d24f17c] | 1 | import _curry2 from "./internal/_curry2.js";
|
---|
| 2 | import _dispatchable from "./internal/_dispatchable.js";
|
---|
| 3 | import _xfindIndex from "./internal/_xfindIndex.js";
|
---|
| 4 | /**
|
---|
| 5 | * Returns the index of the first element of the list which matches the
|
---|
| 6 | * predicate, or `-1` if no element matches.
|
---|
| 7 | *
|
---|
| 8 | * Acts as a transducer if a transformer is given in list position.
|
---|
| 9 | *
|
---|
| 10 | * @func
|
---|
| 11 | * @memberOf R
|
---|
| 12 | * @since v0.1.1
|
---|
| 13 | * @category List
|
---|
| 14 | * @sig (a -> Boolean) -> [a] -> Number
|
---|
| 15 | * @param {Function} fn The predicate function used to determine if the element is the
|
---|
| 16 | * desired one.
|
---|
| 17 | * @param {Array} list The array to consider.
|
---|
| 18 | * @return {Number} The index of the element found, or `-1`.
|
---|
| 19 | * @see R.transduce, R.indexOf
|
---|
| 20 | * @example
|
---|
| 21 | *
|
---|
| 22 | * const xs = [{a: 1}, {a: 2}, {a: 3}];
|
---|
| 23 | * R.findIndex(R.propEq(2, 'a'))(xs); //=> 1
|
---|
| 24 | * R.findIndex(R.propEq(4, 'a'))(xs); //=> -1
|
---|
| 25 | */
|
---|
| 26 |
|
---|
| 27 | var findIndex =
|
---|
| 28 | /*#__PURE__*/
|
---|
| 29 | _curry2(
|
---|
| 30 | /*#__PURE__*/
|
---|
| 31 | _dispatchable([], _xfindIndex, function findIndex(fn, list) {
|
---|
| 32 | var idx = 0;
|
---|
| 33 | var len = list.length;
|
---|
| 34 |
|
---|
| 35 | while (idx < len) {
|
---|
| 36 | if (fn(list[idx])) {
|
---|
| 37 | return idx;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | idx += 1;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | return -1;
|
---|
| 44 | }));
|
---|
| 45 |
|
---|
| 46 | export default findIndex; |
---|
Note:
See
TracBrowser
for help on using the repository browser.