From 85eb28a5dc0ae3399e4a4c148a3f79060c31c217 Mon Sep 17 00:00:00 2001 From: robonen Date: Thu, 24 Oct 2024 07:28:24 +0700 Subject: [PATCH] feat(packages/stdlib): add sum arrays util --- packages/stdlib/src/arrays/sum/index.test.ts | 39 ++++++++++++++++++++ packages/stdlib/src/arrays/sum/index.ts | 26 +++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 packages/stdlib/src/arrays/sum/index.test.ts create mode 100644 packages/stdlib/src/arrays/sum/index.ts diff --git a/packages/stdlib/src/arrays/sum/index.test.ts b/packages/stdlib/src/arrays/sum/index.test.ts new file mode 100644 index 0000000..a007487 --- /dev/null +++ b/packages/stdlib/src/arrays/sum/index.test.ts @@ -0,0 +1,39 @@ +import { describe, it, expect } from 'vitest'; +import { sum } from '.'; + +describe('sum', () => { + it('return the sum of all elements in a number array', () => { + const result = sum([1, 2, 3, 4, 5]); + expect(result).toBe(15); + }); + + it('return 0 for an empty array', () => { + const result = sum([]); + expect(result).toBe(0); + }); + + it('return the sum of all elements using a getValue function', () => { + const result = sum([{ value: 1 }, { value: 2 }, { value: 3 }], (item) => item.value); + expect(result).toBe(6); + }); + + it('handle arrays with negative numbers', () => { + const result = sum([-1, -2, -3, -4, -5]); + expect(result).toBe(-15); + }); + + it('handle arrays with mixed positive and negative numbers', () => { + const result = sum([1, -2, 3, -4, 5]); + expect(result).toBe(3); + }); + + it('handle arrays with floating point numbers', () => { + const result = sum([1.5, 2.5, 3.5]); + expect(result).toBe(7.5); + }); + + it('handle arrays with a getValue function returning floating point numbers', () => { + const result = sum([{ value: 1.5 }, { value: 2.5 }, { value: 3.5 }], (item) => item.value); + expect(result).toBe(7.5); + }); +}); \ No newline at end of file diff --git a/packages/stdlib/src/arrays/sum/index.ts b/packages/stdlib/src/arrays/sum/index.ts new file mode 100644 index 0000000..4a82850 --- /dev/null +++ b/packages/stdlib/src/arrays/sum/index.ts @@ -0,0 +1,26 @@ +/** + * @name sum + * @category Arrays + * @description Returns the sum of all the elements in an array + * + * @param {Value[]} array - The array to sum + * @param {(item: Value) => number} [getValue] - A function that returns the value to sum from each element in the array + * @returns {number} The sum of all the elements in the array + * + * @example + * sum([1, 2, 3, 4, 5]) // => 15 + * + * sum([{ value: 1 }, { value: 2 }, { value: 3 }], (item) => item.value) // => 6 + * + * @since 0.0.3 + */ +export function sum(array: Value[]): number; +export function sum(array: Value[], getValue: (item: Value) => number): number; +export function sum(array: Value[], getValue?: (item: Value) => number): number { + // This check is necessary because the overload without the getValue argument + // makes tree-shaking based on argument types possible + if (!getValue) + return array.reduce((acc, item) => acc + (item as number), 0); + + return array.reduce((acc, item) => acc + getValue(item), 0); +}