Simple object and array cartesian combination generator (for node, typescript and the browser). Kombi will generate every combination between the sets of value given as parameters.
Kombi can be installed using yarn or npm.
npm install kombi
# or
yarn add kombi
And import :
// EMAScript import
import {kombi} from 'kombi';
// Or Common JS:
const {kombi} = require('kombi');
// and used...
const obj = {
a: [1, 2, 3],
b: ['a', 'b']
}
const mergedObj = kombi(obj)
console.log(mergedObj)
/*
[
{a: 1, b: 'a'},
{a: 1, b: 'b'},
{a: 2, b: 'a'},
{a: 2, b: 'b'},
{a: 3, b: 'a'},
{a: 3, b: 'b'},
]
*/
You can use the CDN:
<script src="https://unpkg.com/kombi"></script>
And everything is globally accessible and prefixed with Kombi
:
const kombi = new Kombi.kombi(obj)
const result = kombi({
a: [1, 2, 3, 4],
b: ['a', 'b', 'c', 'd'],
})
/*
result = [
{a: 1, b: 'a'}, {a: 1, b: 'b'}, {a: 1, b: 'c'}, {a: 1, b: 'd'},
{a: 2, b: 'a'}, {a: 2, b: 'b'}, {a: 2, b: 'c'}, {a: 2, b: 'd'},
{a: 3, b: 'a'}, {a: 3, b: 'b'}, {a: 3, b: 'c'}, {a: 3, b: 'd'},
{a: 4, b: 'a'}, {a: 4, b: 'b'}, {a: 4, b: 'c'}, {a: 4, b: 'd'}
]
*/
const result = kombi([
[1, 2, 3, 4],
['a', 'b', 'c'],
])
/*
result = [
[1, 'a'], [1, 'b'], [1, 'c'],
[2, 'a'], [2, 'b'], [2, 'c'],
[3, 'a'], [3, 'b'], [3, 'c'],
[4, 'a'], [4, 'b'], [4, 'c']
]
*/
- Improve documentation
- Add more unit test
- Allow single parameters as input, like :
const result = kombi({
a: [1, 2, 3, 4],
b: ['a', 'b', 'c', 'd'],
c: 'foo'
})
- Create benchmark
Pull requests are welcome ! Feel free to contribute.
Coded with ❤️ by Corentin Thomasset.
This project is under the MIT license.