Skip to content

Commit

Permalink
feat(sum): ensures that typing will work with things other than numbe…
Browse files Browse the repository at this point in the history
…rs, objects
  • Loading branch information
MarlonPassos-git authored and aleclarson committed Aug 6, 2024
1 parent 49263c8 commit a5be0c4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/number/sum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
* // => 2
* ```
*/
export function sum<T extends number>(array: readonly T[]): number
export function sum<T extends object | boolean>(
export function sum(array: readonly number[]): number
export function sum<T>(
array: readonly T[],
fn: (item: T) => number,
): number
export function sum<T extends object | number>(
export function sum<T>(
array: readonly any[],
fn?: (item: T) => number,
): number {
Expand Down
10 changes: 10 additions & 0 deletions tests/number/sum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ describe('sum', () => {
const result = _.sum(list, x => (x ? 1 : 0))
expect(result).toBe(3)
})

test('gracefully handles matrix input', () => {
const list = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
const result = _.sum(list, x => _.sum(x))
expect(result).toBe(45)
})
})

0 comments on commit a5be0c4

Please sign in to comment.