UNPKG

@newdash/newdash

Version:

javascript/typescript utility library

59 lines (58 loc) 1.9 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.findIndex = void 0; // @ts-nocheck const baseFindIndex_1 = __importDefault(require("./.internal/baseFindIndex")); const getIteratee_1 = __importDefault(require("./.internal/getIteratee")); const toInteger_1 = __importDefault(require("./toInteger")); /** * This method is like `find` except that it returns the index of the first * element `predicate` returns truthy for instead of the element itself. * * @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': false }, * { 'user': 'fred', 'active': false }, * { 'user': 'pebbles', 'active': true } * ]; * * findIndex(users, function(o) { return o.user == 'barney'; }); * // => 0 * * // The `matches` iteratee shorthand. * findIndex(users, { 'user': 'fred', 'active': false }); * // => 1 * * // The `matchesProperty` iteratee shorthand. * findIndex(users, ['active', false]); * // => 0 * * // The `property` iteratee shorthand. * findIndex(users, 'active'); * // => 2 * ``` */ function findIndex(array, predicate, fromIndex) { const length = array == null ? 0 : array.length; if (!length) { return -1; } let index = fromIndex == null ? 0 : (0, toInteger_1.default)(fromIndex); if (index < 0) { index = Math.max(length + index, 0); } return (0, baseFindIndex_1.default)(array, (0, getIteratee_1.default)(predicate, 3), index); } exports.findIndex = findIndex; exports.default = findIndex;