Skip to content

Commit

Permalink
✨ Add product function
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Apr 19, 2021
1 parent 891e0e0 commit f1ccc1d
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
| [lte](./lte/) | Returns <code>true</code> if the first argument is less than or equal to the second; otherwise <code>false</code> |
| [multiply](./multiply/) | Multiplies first argument and second argument. |
| [or](./or/) | Returns true if one or both of its arguments are true; otherwise false. |
| [product](./product/) | <b><i>(BETA)</i></b> Multiplies together all the elements of a list. |
| [reverse](./reverse/) | Returns a new list or string with the elements or characters in reverse order. |
| [startsWith](./startswith/) | Checks if a string starts with the provided substring. |
| [subtract](./subtract/) | Subtracts its second argument from its first argument. |
Expand Down
30 changes: 30 additions & 0 deletions docs/api/product/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->



## product variable

> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
>
Multiplies together all the elements of a list.

<b>Signature:</b>

```typescript
product: {
(val: number[]): number;
(val: bigint[]): bigint;
}
```
## Example
```ts
product([1, 2, 3, 4, 5]) // 120
product([1n, 2n, 3n, 4n, 5n]) //120n
product([]) // 0

```
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export { lt } from '@/lt'
export { lte } from '@/lte'
export { multiply } from '@/multiply'
export { or } from '@/or'
export { product } from '@/product'
export { reverse } from '@/reverse'
export { startsWith } from '@/startsWith'
export { subtract } from '@/subtract'
Expand Down
33 changes: 33 additions & 0 deletions src/product.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { first } from '@/first'
import { isNumber } from '@/isNumber'
import { isUndefined } from '@/isUndefined'
import { multiply } from '@/multiply'

/**
* Multiplies together all the elements of a list.
*
* @param val - list An array of numbers
* @returns The product of all the numbers in the list
*
* @example
* ```ts
* product([1, 2, 3, 4, 5]) // 120
* product([1n, 2n, 3n, 4n, 5n]) //120n
* product([]) // 0
* ```
*
* @beta
*/
const product: {
(val: number[]): number
(val: bigint[]): bigint
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} = (val: any) => {
const head = first(val) as number | bigint
if (isUndefined(head)) return 0
const init = isNumber(head) ? 1 : 1n

return val.reduce(multiply, init)
}

export { product }
25 changes: 25 additions & 0 deletions test/product.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { product } from '@/product'

describe('product', () => {
const tableNumber: [number[], number][] = [
[[], 0],
[[0, 0], 0],
[[1], 1],
[[1, 2, 3, 4, 5], 120],
[[1, -2, 3, -4, 5], 120]
]

it.each(tableNumber)('product(%s) -> %s', (val, expected) => {
expect(product(val)).toBe(expected)
})
const tableBigint: [bigint[], bigint][] = [
[[0n], 0n],
[[1n], 1n],
[[1n, 2n, 3n, 4n, 5n], 120n],
[[1n, -2n, 3n, -4n, 5n], 120n]
]

it.each(tableBigint)('product(%s) -> %s', (val, expected) => {
expect(product(val)).toBe(expected)
})
})

0 comments on commit f1ccc1d

Please sign in to comment.