remeda
Version:
A utility library for JavaScript and Typescript.
1 lines • 2.96 kB
Source Map (JSON)
{"version":3,"file":"findLastIndex.cjs","names":["purry"],"sources":["../src/findLastIndex.ts"],"sourcesContent":["import { purry } from \"./purry\";\n\n/**\n * Iterates the array in reverse order and returns the index of the first\n * element that satisfies the provided testing function. If no elements satisfy\n * the testing function, -1 is returned.\n *\n * See also `findLast` which returns the value of last element that satisfies\n * the testing function (rather than its index).\n *\n * @param data - The items to search in.\n * @param predicate - A function to execute for each element in the array. It\n * should return `true` to indicate a matching element has been found, and\n * `false` otherwise.\n * @returns The index of the last (highest-index) element in the array that\n * passes the test. Otherwise -1 if no matching element is found.\n * @signature\n * R.findLastIndex(data, predicate)\n * @example\n * R.findLastIndex([1, 3, 4, 6], n => n % 2 === 1) // => 1\n * @dataFirst\n * @category Array\n */\nexport function findLastIndex<T>(\n data: ReadonlyArray<T>,\n predicate: (value: T, index: number, data: ReadonlyArray<T>) => boolean,\n): number;\n\n/**\n * Iterates the array in reverse order and returns the index of the first\n * element that satisfies the provided testing function. If no elements satisfy\n * the testing function, -1 is returned.\n *\n * See also `findLast` which returns the value of last element that satisfies\n * the testing function (rather than its index).\n *\n * @param predicate - A function to execute for each element in the array. It\n * should return `true` to indicate a matching element has been found, and\n * `false` otherwise.\n * @returns The index of the last (highest-index) element in the array that\n * passes the test. Otherwise -1 if no matching element is found.\n * @signature\n * R.findLastIndex(fn)(items)\n * @example\n * R.pipe(\n * [1, 3, 4, 6],\n * R.findLastIndex(n => n % 2 === 1)\n * ) // => 1\n * @dataLast\n * @category Array\n */\nexport function findLastIndex<T>(\n predicate: (value: T, index: number, data: ReadonlyArray<T>) => boolean,\n): (array: ReadonlyArray<T>) => number;\n\nexport function findLastIndex(...args: ReadonlyArray<unknown>): unknown {\n return purry(findLastIndexImplementation, args);\n}\n\nconst findLastIndexImplementation = <T>(\n data: ReadonlyArray<T>,\n predicate: (value: T, index: number, data: ReadonlyArray<T>) => boolean,\n): number => {\n // TODO [>2]: When node 18 reaches end-of-life bump target lib to ES2023+ and use `Array.prototype.findLastIndex` here.\n\n for (let i = data.length - 1; i >= 0; i--) {\n if (predicate(data[i]!, i, data)) {\n return i;\n }\n }\n\n return -1;\n};\n"],"mappings":"wCAuDA,SAAgB,EAAc,GAAG,EAAuC,CACtE,OAAOA,EAAAA,EAAM,EAA6B,EAAK,CAGjD,MAAM,GACJ,EACA,IACW,CAGX,IAAK,IAAI,EAAI,EAAK,OAAS,EAAG,GAAK,EAAG,IACpC,GAAI,EAAU,EAAK,GAAK,EAAG,EAAK,CAC9B,OAAO,EAIX,MAAO"}