Created
June 22, 2018 20:27
-
-
Save davidchase/b0a7e0a64501762ff47ae7df712943b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const obj = { 1: {}, 2: {}, 3: {} } | |
const calculateLimitedPermutation = (pFn , aFn, o) => { | |
const recurse = (index = 0, xs = []) => { | |
const arr = Object.keys(o) | |
if ( arr.length === 0 ) return o; | |
const types = arr.reduce( | |
(acc, key, idx) => | |
idx === index | |
? { ...acc, ...{ [key]: pFn(key) } } | |
: { ...acc, ...{ [key]: aFn(key) } }, | |
{} | |
) | |
const xy = xs.concat(types) | |
return xy.length === arr.length ? xy : recurse(++index, xy) | |
} | |
return recurse() | |
} | |
calculateLimitedPermutation(key => `${key}-a`, key => `${key}-b`, obj) // [{"1": "1-a", "2": "2-b", "3": "3-b"}, {"1": "1-b", "2": "2-a", "3": "3-b"}, {"1": "1-b", "2": "2-b", "3": "3-a"}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment