├── .npmignore ├── .gitignore ├── .travis.yml ├── demo ├── README.md ├── demo-screenshot.png ├── data.js ├── package.json ├── Button.js ├── Root.js └── index.html ├── src ├── index.js ├── get-permutations.js └── get-options.js ├── package.json ├── README.md └── test └── index.js /.npmignore: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | dist 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 5.6 4 | -------------------------------------------------------------------------------- /demo/README.md: -------------------------------------------------------------------------------- 1 | # Demo 2 | 3 | ```sh 4 | npm i && npm start 5 | ``` 6 | 7 | -------------------------------------------------------------------------------- /demo/demo-screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnblk/react-component-permutations/HEAD/demo/demo-screenshot.png -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | 2 | import { parse } from 'react-docgen' 3 | import getPermutations from './get-permutations' 4 | 5 | export default function (src, opts) { 6 | opts = opts || {} 7 | 8 | try { 9 | const info = parse(src) 10 | const result = getPermutations(info.props, opts) 11 | return result 12 | } catch (e) { 13 | console.log('Could not read component') 14 | return false 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /demo/data.js: -------------------------------------------------------------------------------- 1 | 2 | import fs from 'fs' 3 | import path from 'path' 4 | import colors from 'colors.css' 5 | import permutations from '../src' 6 | 7 | const ButtonSrc = fs.readFileSync('Button.js', 'utf8') 8 | 9 | const colorKeys = Object.keys(colors) 10 | .filter(key => key !== 'white') 11 | 12 | const data = { 13 | ButtonSrc, 14 | permutations: permutations(ButtonSrc), 15 | permutationsColors: permutations(ButtonSrc, { 16 | color: colorKeys 17 | }) 18 | } 19 | 20 | export default data 21 | 22 | -------------------------------------------------------------------------------- /demo/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-component-permutations-demo", 3 | "version": "1.0.0", 4 | "scripts": { 5 | "test": "static-react Root.js", 6 | "start": "static-react Root.js > index.html" 7 | }, 8 | "devDependencies": { 9 | "babel-preset-es2015": "^6.5.0", 10 | "babel-preset-react": "^6.5.0", 11 | "babel-preset-stage-0": "^6.5.0", 12 | "colors.css": "^3.0.0", 13 | "react": "^0.14.7", 14 | "static-react": "^3.1.0" 15 | }, 16 | "babel": { 17 | "presets": [ 18 | "es2015", 19 | "stage-0", 20 | "react" 21 | ] 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/get-permutations.js: -------------------------------------------------------------------------------- 1 | 2 | import getOptions from './get-options' 3 | 4 | function getPermutations (props, opts) { 5 | const result = [] 6 | const obj = getOptions(props, opts) 7 | const keys = Object.keys(obj) 8 | const max = keys.length - 1 9 | 10 | function combine (a, i) { 11 | const key = keys[i] 12 | obj[key].forEach((prop, j) => { 13 | const b = Object.assign({}, a) 14 | b[key] = prop 15 | 16 | if (i === max) { 17 | result.push(b) 18 | } else { 19 | combine(b, i + 1) 20 | } 21 | }) 22 | } 23 | combine({}, 0) 24 | 25 | return result 26 | } 27 | 28 | export default getPermutations 29 | 30 | -------------------------------------------------------------------------------- /demo/Button.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import colors from 'colors.css' 3 | 4 | const colorKeys = Object.keys(colors) 5 | 6 | const Button = ({ big, color, pill, outline, ...props }) => ( 7 |

With Options

By configuring options for the colors prop, there are now 128 permutations.

permutations(buttonSourceString, { colors: colorKeys })

--------------------------------------------------------------------------------