Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add product function #377

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions docs/array/product.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: product
group: 'Array'
description: Get Cartesian product of the arguments
---

## Basic usage

```ts
import { product } from 'radash'

const array1 = [0, 1]
const array2 = ['a', 'b']
const array3 = ['A', 'B', 'C']

product(array1, array2, array3) /* => [
[0, 'a', 'A'],
[0, 'a', 'B'],
[0, 'a', 'C'],
[0, 'b', 'A'],
[0, 'b', 'B'],
[0, 'b', 'C'],
[1, 'a', 'A'],
[1, 'a', 'B'],
[1, 'a', 'C'],
[1, 'b', 'A'],
[1, 'b', 'B'],
[1, 'b', 'C'],
]
*/
```
19 changes: 19 additions & 0 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,22 @@ export function shift<T>(arr: Array<T>, n: number) {

return [...arr.slice(-shiftNumber, arr.length), ...arr.slice(0, -shiftNumber)]
}

export type ProductItem<Arrs> = Arrs extends [Array<infer T>, ...infer Rest] ? Rest extends [] ? [T] : [T, ...ProductItem<Rest>] : never;
export function product<T extends Array<any>[]>(...args: T): Array<ProductItem<T>> {
if (args.some(arg => arg.length < 1)) return [];
const indices = new Array(args.length).fill(0);
let currentIndex = args.length - 1;
const result = [] as Array<T[number][number]>;
while (indices[0] < args[0]?.length) {
result.push(indices.map((j, i) => args[i][j]));
indices[currentIndex] += 1;
while (currentIndex > 0 && indices[currentIndex] === args[currentIndex].length) {
indices[currentIndex] = 0;
indices[currentIndex - 1] += 1;
currentIndex -= 1;
}
currentIndex = args.length - 1;
}
return result;
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export {
merge,
min,
objectify,
product,
ProductItem,
range,
replace,
replaceOrAppend,
Expand Down
56 changes: 56 additions & 0 deletions src/tests/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,4 +777,60 @@ describe('array module', () => {
assert.deepEqual(result, ['b', 'a'])
})
})

describe('product function', () => {
const arrA = [{}, {}, {}]
const arrB = [{}, {}]
const arrC = [{}]
test('returns cartesian product of two arguments', () => {
const result = _.product(arrA, arrB)
assert.deepEqual(result, [
[arrA[0], arrB[0]],
[arrA[0], arrB[1]],
[arrA[1], arrB[0]],
[arrA[1], arrB[1]],
[arrA[2], arrB[0]],
[arrA[2], arrB[1]],
])
})
test('returns cartesian product of more than two arguments', () => {
const result = _.product(arrB, arrB, arrB)
assert.deepEqual(result, [
[arrB[0], arrB[0], arrB[0]],
[arrB[0], arrB[0], arrB[1]],
[arrB[0], arrB[1], arrB[0]],
[arrB[0], arrB[1], arrB[1]],
[arrB[1], arrB[0], arrB[0]],
[arrB[1], arrB[0], arrB[1]],
[arrB[1], arrB[1], arrB[0]],
[arrB[1], arrB[1], arrB[1]],
])
})
test('works when one of the arguments has 1 item', () => {
const result = _.product(arrB, arrC, arrB)
assert.deepEqual(result, [
[arrB[0], arrC[0], arrB[0]],
[arrB[0], arrC[0], arrB[1]],
[arrB[1], arrC[0], arrB[0]],
[arrB[1], arrC[0], arrB[1]],
])
})
test('works when called with a single argument', () => {
const result = _.product(arrA)
assert.deepEqual(result, [
[arrA[0]],
[arrA[1]],
[arrA[2]],
])
})
test('returns empty array when one of the arguments is an empty array', () => {
const result = _.product(arrA, [], arrB)
assert.deepEqual(result, [])
})
test('returns empty array when called with no arguments', () => {
const result = _.product()
assert.deepEqual(result, [])
})
})

})
Loading