remeda
Version:
A utility library for JavaScript and Typescript.
1 lines • 4.24 kB
Source Map (JSON)
{"version":3,"file":"findLast.cjs","names":["purry"],"sources":["../src/findLast.ts"],"sourcesContent":["import { purry } from \"./purry\";\n\n/**\n * Iterates the array in reverse order and returns the value of the first\n * element that satisfies the provided testing function. If no elements satisfy\n * the testing function, undefined is returned.\n *\n * Similar functions:\n * * `find` - If you need the first element that satisfies the provided testing function.\n * * `findLastIndex` - If you need the index of the found element in the array.\n * * `lastIndexOf` - If you need to find the index of a value.\n * * `includes` - If you need to find if a value exists in an array.\n * * `some` - If you need to find if any element satisfies the provided testing function.\n * * `filter` - If you need to find all elements that satisfy the provided testing function.\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. A type-predicate can also be used to narrow the result.\n * @returns The last (highest-index) element in the array that satisfies the\n * provided testing function; undefined if no matching element is found.\n * @signature\n * R.findLast(data, predicate)\n * @example\n * R.findLast([1, 3, 4, 6], n => n % 2 === 1) // => 3\n * @dataFirst\n * @category Array\n */\nexport function findLast<T, S extends T>(\n data: ReadonlyArray<T>,\n predicate: (value: T, index: number, data: ReadonlyArray<T>) => value is S,\n): S | undefined;\nexport function findLast<T>(\n data: ReadonlyArray<T>,\n predicate: (value: T, index: number, data: ReadonlyArray<T>) => boolean,\n): T | undefined;\n\n/**\n * Iterates the array in reverse order and returns the value of the first\n * element that satisfies the provided testing function. If no elements satisfy\n * the testing function, undefined is returned.\n *\n * Similar functions:\n * * `find` - If you need the first element that satisfies the provided testing function.\n * * `findLastIndex` - If you need the index of the found element in the array.\n * * `lastIndexOf` - If you need to find the index of a value.\n * * `includes` - If you need to find if a value exists in an array.\n * * `some` - If you need to find if any element satisfies the provided testing function.\n * * `filter` - If you need to find all elements that satisfy the provided testing function.\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. A type-predicate can also be used to narrow the result.\n * @returns The last (highest-index) element in the array that satisfies the\n * provided testing function; undefined if no matching element is found.\n * @signature\n * R.findLast(predicate)(data)\n * @example\n * R.pipe(\n * [1, 3, 4, 6],\n * R.findLast(n => n % 2 === 1)\n * ) // => 3\n * @dataLast\n * @category Array\n */\nexport function findLast<T, S extends T>(\n predicate: (value: T, index: number, data: ReadonlyArray<T>) => value is S,\n): (data: ReadonlyArray<T>) => S | undefined;\nexport function findLast<T = never>(\n predicate: (value: T, index: number, data: ReadonlyArray<T>) => boolean,\n): (data: ReadonlyArray<T>) => T | undefined;\n\nexport function findLast(...args: ReadonlyArray<unknown>): unknown {\n return purry(findLastImplementation, args);\n}\n\nconst findLastImplementation = <T, S extends T>(\n data: ReadonlyArray<T>,\n predicate: (value: T, index: number, data: ReadonlyArray<T>) => value is S,\n): S | undefined => {\n // TODO [>2]: When node 18 reaches end-of-life bump target lib to ES2023+ and use `Array.prototype.findLast` here.\n\n for (let i = data.length - 1; i >= 0; i--) {\n const item = data[i]!;\n if (predicate(item, i, data)) {\n return item;\n }\n }\n\n return undefined;\n};\n"],"mappings":"wCAwEA,SAAgB,EAAS,GAAG,EAAuC,CACjE,OAAOA,EAAAA,EAAM,EAAwB,EAAK,CAG5C,MAAM,GACJ,EACA,IACkB,CAGlB,IAAK,IAAI,EAAI,EAAK,OAAS,EAAG,GAAK,EAAG,IAAK,CACzC,IAAM,EAAO,EAAK,GAClB,GAAI,EAAU,EAAM,EAAG,EAAK,CAC1B,OAAO"}