CorentinTh / kombi

Simple object and array cartesian combination generator (for node, typescript and the browser)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

logo

Weekly Downloads GitHub Workflow Status codecov npm bundle size GitHub package.json version Dependencies Licence Badge

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.

Installation

JS / Typescript / Node JS

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'},
]
*/

Browser

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)

Usage

Combinate object with array properties

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'}
]
*/

Combinate array of arrays

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']
]
*/

Roadmap

  • 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

Contribute

Pull requests are welcome ! Feel free to contribute.

Credits

Coded with ❤️ by Corentin Thomasset.

License

This project is under the MIT license.

About

Simple object and array cartesian combination generator (for node, typescript and the browser)

License:MIT License


Languages

Language:TypeScript 84.5%Language:JavaScript 15.5%