Skip to content

Commit

Permalink
feat: add product function
Browse files Browse the repository at this point in the history
  • Loading branch information
kantord committed Oct 20, 2018
1 parent 9bfa159 commit 8da5fa4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/__tests__/builtins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const {
keys,
split,
values,
combinations
combinations,
product
} = builtIns

describe('built ins', () => {
Expand Down Expand Up @@ -190,4 +191,16 @@ describe('built ins', () => {
])
})
})

describe('product', () => {
it('returns correct value', () => {
expect(product([['a', 'b'], []])).toEqual([])
expect(product([['a', 'b'], ['1', '2']])).toEqual([
['a', '1'],
['a', '2'],
['b', '1'],
['b', '2']
])
})
})
})
6 changes: 5 additions & 1 deletion src/builtins.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

import { combinations } from '@aureooms/js-itertools/lib/map/combinations.js'
import { product } from '@aureooms/js-itertools/lib/map/product.js'

type ProjectableType = Array<mixed> & {[string]: mixed};
type ProjectionRuleType = number & string;
Expand Down Expand Up @@ -93,5 +94,8 @@ export default {

combinations: (r: number): Array<mixed> | (string => Array<Array<mixed>>) => (
input: Array<mixed> | string
): Array<Array<mixed>> => Array.from(combinations(input, r))
): Array<Array<mixed>> => Array.from(combinations(input, r)),

product: (input: Array<mixed> | string): Array<Array<mixed>> =>
Array.from(product(input))
}

0 comments on commit 8da5fa4

Please sign in to comment.