remeda
Version:
A utility library for JavaScript and Typescript.
1 lines • 3.44 kB
Source Map (JSON)
{"version":3,"file":"reduce.cjs","names":["purry"],"sources":["../src/reduce.ts"],"sourcesContent":["import { purry } from \"./purry\";\n\n/**\n * Executes a user-supplied \"reducer\" callback function on each element of the\n * array, in order, passing in the return value from the calculation on the\n * preceding element. The final result of running the reducer across all\n * elements of the array is a single value. Equivalent to\n * `Array.prototype.reduce`.\n *\n * @param data - The items to reduce.\n * @param callbackfn - A function to execute for each element in the array. Its\n * return value becomes the value of the accumulator parameter on the next\n * invocation of callbackFn. For the last invocation, the return value becomes\n * the return value of reduce().\n * @param initialValue - A value to which accumulator is initialized the first\n * time the callback is called. CallbackFn starts executing with the first value\n * in the array as currentValue.\n * @returns The value that results from running the \"reducer\" callback function\n * to completion over the entire array.\n * @signature\n * R.reduce(data, callbackfn, initialValue)\n * @example\n * R.reduce([1, 2, 3, 4, 5], (acc, x) => acc + x, 100) // => 115\n * @dataFirst\n * @category Array\n */\nexport function reduce<T, U>(\n data: ReadonlyArray<T>,\n callbackfn: (\n previousValue: U,\n currentValue: T,\n currentIndex: number,\n data: ReadonlyArray<T>,\n ) => U,\n initialValue: U,\n): U;\n\n/**\n * Executes a user-supplied \"reducer\" callback function on each element of the\n * array, in order, passing in the return value from the calculation on the\n * preceding element. The final result of running the reducer across all\n * elements of the array is a single value. Equivalent to\n * `Array.prototype.reduce`.\n *\n * @param callbackfn - A function to execute for each element in the array. Its\n * return value becomes the value of the accumulator parameter on the next\n * invocation of callbackFn. For the last invocation, the return value becomes\n * the return value of reduce().\n * @param initialValue - A value to which accumulator is initialized the first\n * time the callback is called. CallbackFn starts executing with the first value\n * in the array as currentValue.\n * @returns The value that results from running the \"reducer\" callback function\n * to completion over the entire array.\n * @signature\n * R.reduce(fn, initialValue)(array)\n * @example\n * R.pipe([1, 2, 3, 4, 5], R.reduce((acc, x) => acc + x, 100)) // => 115\n * @dataLast\n * @category Array\n */\nexport function reduce<T, U>(\n callbackfn: (\n previousValue: U,\n currentValue: T,\n currentIndex: number,\n data: ReadonlyArray<T>,\n ) => U,\n initialValue: U,\n): (data: ReadonlyArray<T>) => U;\n\nexport function reduce(...args: ReadonlyArray<unknown>): unknown {\n return purry(reduceImplementation, args);\n}\n\nconst reduceImplementation = <T, U>(\n data: ReadonlyArray<T>,\n callbackfn: (\n previousValue: U,\n currentValue: T,\n currentIndex: number,\n data: ReadonlyArray<T>,\n ) => U,\n initialValue: U,\n): U =>\n // eslint-disable-next-line unicorn/no-array-reduce -- Our function wraps the built-in reduce.\n data.reduce(callbackfn, initialValue);\n"],"mappings":"wCAsEA,SAAgB,EAAO,GAAG,EAAuC,CAC/D,OAAOA,EAAAA,EAAM,EAAsB,EAAK,CAG1C,MAAM,GACJ,EACA,EAMA,IAGA,EAAK,OAAO,EAAY,EAAa"}