-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
156 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import {expect} from 'chai'; | ||
import {cartesian} from '../../../../src/array'; | ||
import {checkThrow} from '../../../support/checkThrow'; | ||
|
||
describe('cartesian', () => { | ||
|
||
|
||
it('should cartesian []', () => { | ||
|
||
const fn = cartesian; | ||
expect(fn([])).to.eql([]); | ||
}); | ||
|
||
it('should cartesian [2]', () => { | ||
|
||
const fn = cartesian; | ||
expect(fn([1, 2])).to.eql([ | ||
[1], | ||
[2], | ||
]); | ||
}); | ||
it('should cartesian [2,3]', () => { | ||
|
||
const fn = cartesian; | ||
expect(fn([1, 2], ['a', 'b', 'c'])).to.eql([ | ||
[ | ||
[1, 'a'], | ||
[1, 'b'], | ||
[1, 'c'], | ||
], | ||
[ | ||
[2, 'a'], | ||
[2, 'b'], | ||
[2, 'c'], | ||
], | ||
]); | ||
}); | ||
|
||
it('should cartesian [3,4,2]', () => { | ||
|
||
const fn = cartesian; | ||
type Cover = 'blue cover' | 'red cover' | ||
type Color = 'hearts' | 'spades' | 'diamonds' | 'clubs' | ||
type Value = 'ace' | 'king' | 'queen' | ||
const covers: Cover[] = ['blue cover', 'red cover']; | ||
const expansion = fn(['ace', 'king', 'queen'] as Value[], ['hearts', 'spades', 'diamonds', 'clubs'] as Color[], covers); | ||
const expected: typeof expansion = [ | ||
[ | ||
[ | ||
['ace', 'hearts', 'blue cover'], | ||
['ace', 'hearts', 'red cover'],], | ||
[ | ||
['ace', 'spades', 'blue cover'], | ||
['ace', 'spades', 'red cover'], | ||
], | ||
[ | ||
['ace', 'diamonds', 'blue cover'], | ||
['ace', 'diamonds', 'red cover'], | ||
], | ||
[ | ||
['ace', 'clubs', 'blue cover'], | ||
['ace', 'clubs', 'red cover'], | ||
], | ||
], | ||
[ | ||
[ | ||
['king', 'hearts', 'blue cover'], | ||
['king', 'hearts', 'red cover'], | ||
], | ||
[ | ||
['king', 'spades', 'blue cover'], | ||
['king', 'spades', 'red cover'], | ||
], | ||
[ | ||
['king', 'diamonds', 'blue cover'], | ||
['king', 'diamonds', 'red cover'], | ||
], | ||
[ | ||
['king', 'clubs', 'blue cover'], | ||
['king', 'clubs', 'red cover'], | ||
], | ||
], | ||
[ | ||
[ | ||
['queen', 'hearts', 'blue cover'], | ||
['queen', 'hearts', 'red cover'], | ||
], | ||
[ | ||
['queen', 'spades', 'blue cover'], | ||
['queen', 'spades', 'red cover'], | ||
], | ||
[ | ||
['queen', 'diamonds', 'blue cover'], | ||
['queen', 'diamonds', 'red cover'], | ||
], | ||
[ | ||
['queen', 'clubs', 'blue cover'], | ||
['queen', 'clubs', 'red cover'], | ||
], | ||
] | ||
]; | ||
expect(expansion).to.eql(expected); | ||
}); | ||
|
||
|
||
it('should throw if null or undefined', () => { | ||
const fn = (e: number[][]) => cartesian(e); | ||
checkThrow(fn); | ||
}); | ||
|
||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {chunkBySize} from './chunkBySize'; | ||
import {allCombinations} from './allCombinations'; | ||
|
||
export function cartesian(): []; | ||
export function cartesian<A>(a: A[]): [A][]; | ||
export function cartesian<A, B>(a: A[], b: B[]): [A, B][][]; | ||
export function cartesian<A, B, C>(a: A[], b: B[], c: C[]): [A, B, C][][][]; | ||
export function cartesian<A, B, C, D>(a: A[], b: B[], c: C[], d: D[]): [A, B, C, D][][][][]; | ||
export function cartesian<A, B, C, D, E>(a: A[], b: B[], c: C[], d: D[], e: E[]): [A, B, C, D, E][][][][][]; | ||
export function cartesian<A, B, C, D, E, F>(a: A[], b: B[], c: C[], d: D[], e: E[], f: F[]): [A, B, C, D, E, F][][][][][]; | ||
export function cartesian<A, B, C, D, E, F, G>(a: A[], b: B[], c: C[], d: D[], e: E[], f: F[], g: G[]): [A, B, C, D, E, F, G][][][][][][]; | ||
export function cartesian<A, B, C, D, E, F, G, H>(a: A[], b: B[], c: C[], d: D[], e: E[], f: F[], g: G[], h: H[]): [A, B, C, D, E, F, G, H][][][][][][][]; | ||
export function cartesian<A, B, C, D, E, F, G, H>(a: A[], b: B[], c: C[], d: D[], e: E[], f: F[], g: G[], h: H[]): [A, B, C, D, E, F, G, H][][][][][][][]; | ||
|
||
export function cartesian(...arrays: unknown[][]): unknown[][] { | ||
|
||
// @ts-ignore | ||
let res: any[] = allCombinations(...arrays); | ||
|
||
for (let i = arrays.length - 1; i > 0; i--) { | ||
let size = arrays[i].length; | ||
res = chunkBySize(size)(res); | ||
} | ||
return res; | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters