@imekachi/combinations
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

Combinations

Generate all possible combinations of variant array which contains options array.

NPM Version Codecov Status

Installation

Using npm:

$ npm install --save @imekachi/combinations

Using yarn:

$ yarn add @imekachi/combinations

Quick Start

import combinations from '@imekachi/combinations'

const variants = combinations([
  ['red', 'green', 'blue'],
  ['S', 'M', 'L'],
])

console.log(`variants: `, variants)
// variants:
// [
//   ['red', 'S'],
//   ['red', 'M'],
//   ['red', 'L'],
//   ['green', 'S'],
//   ['green', 'M'],
//   ['green', 'L'],
//   ['blue', 'S'],
//   ['blue', 'M'],
//   ['blue', 'L'],
// ]

If you store options in an object, you can do it like this

import combinations from '@imekachi/combinations'

const shirtOptionObj = {
  color: ['red', 'green', 'blue'],
  size: ['S', 'M', 'L'],
}

const variants = combinations(Object.values(shirtOptionObj))

Or option object within an array

import combinations from '@imekachi/combinations'

const shirtOptions = [
  {
    name: 'Color',
    options: ['red', 'green', 'blue'],
  },
  {
    name: 'Size',
    options: ['S', 'M', 'L'],
  },
]

const variants = combinations(
  shirtVariantOptions.map((variant) => variant.options)
)

Contributing

Feel free to submit any issues or pull requests.

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i @imekachi/combinations

Weekly Downloads

0

Version

1.1.1

License

MIT

Unpacked Size

10 kB

Total Files

11

Last publish

Collaborators

  • imekachi