UNPKG

@newdash/newdash

Version:

javascript/typescript utility library

63 lines (62 loc) 1.99 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.findLastIndex = void 0; const baseFindIndex_1 = __importDefault(require("./.internal/baseFindIndex")); const getIteratee_1 = __importDefault(require("./.internal/getIteratee")); const toInteger_1 = __importDefault(require("./toInteger")); /** * This method is like `findIndex` except that it iterates over elements * of `collection` from right to left. * * @since 5.2.0 * @category Array * @param array The array to inspect. * @param predicate The function invoked per iteration. * @param fromIndex The index to search from. * @returns {number} Returns the index of the found element, else `-1`. * @example * * ```js * * var users = [ * { 'user': 'barney', 'active': true }, * { 'user': 'fred', 'active': false }, * { 'user': 'pebbles', 'active': false } * ]; * * findLastIndex(users, function(o) { return o.user == 'pebbles'; }); * // => 2 * * // The `matches` iteratee shorthand. * findLastIndex(users, { 'user': 'barney', 'active': true }); * // => 0 * * // The `matchesProperty` iteratee shorthand. * findLastIndex(users, ['active', false]); * // => 2 * * // The `property` iteratee shorthand. * findLastIndex(users, 'active'); * // => 0 * * ``` */ function findLastIndex(array, predicate, fromIndex) { const length = array == null ? 0 : array.length; if (!length) { return -1; } let index = length - 1; if (fromIndex !== undefined) { index = (0, toInteger_1.default)(fromIndex); index = fromIndex < 0 ? Math.max(length + index, 0) : Math.min(index, length - 1); } return (0, baseFindIndex_1.default)(array, (0, getIteratee_1.default)(predicate, 3), index, true); } exports.findLastIndex = findLastIndex; exports.default = findLastIndex;