remeda
Version:
A utility library for JavaScript and Typescript.
1 lines • 1.65 kB
Source Map (JSON)
{"version":3,"file":"last.cjs","names":["purry"],"sources":["../src/last.ts"],"sourcesContent":["import type { LastArrayElement } from \"type-fest\";\nimport type { IterableContainer } from \"./internal/types/IterableContainer\";\nimport { purry } from \"./purry\";\n\ntype Last<T extends IterableContainer> = LastArrayElement<\n T,\n // Type-fest's LastArrayElement assumes a looser typescript configuration\n // where `noUncheckedIndexedAccess` is disabled. To support the stricter\n // configuration we assume we need to assign the \"LastArrayElement\" param to\n // `undefined`, but only if the array isn't empty.\n T extends readonly [] ? never : undefined\n>;\n\n/**\n * Gets the last element of `array`.\n *\n * @param data - The array.\n * @signature\n * R.last(array)\n * @example\n * R.last([1, 2, 3]) // => 3\n * R.last([]) // => undefined\n * @dataFirst\n * @category Array\n */\nexport function last<T extends IterableContainer>(data: T): Last<T>;\n\n/**\n * Gets the last element of `array`.\n *\n * @signature\n * R.last()(array)\n * @example\n * R.pipe(\n * [1, 2, 4, 8, 16],\n * R.filter(x => x > 3),\n * R.last(),\n * x => x + 1\n * ); // => 17\n * @dataLast\n * @category Array\n */\nexport function last(): <T extends IterableContainer>(data: T) => Last<T>;\n\nexport function last(...args: ReadonlyArray<unknown>): unknown {\n return purry(lastImplementation, args);\n}\n\nconst lastImplementation = <T>(array: ReadonlyArray<T>): T | undefined =>\n array.at(-1);\n"],"mappings":"wCA4CA,SAAgB,EAAK,GAAG,EAAuC,CAC7D,OAAOA,EAAAA,EAAM,EAAoB,EAAK,CAGxC,MAAM,EAAyB,GAC7B,EAAM,GAAG,GAAG"}