Skip to content

Instantly share code, notes, and snippets.

@davidchase
Created June 22, 2018 20:27
Show Gist options
  • Save davidchase/b0a7e0a64501762ff47ae7df712943b5 to your computer and use it in GitHub Desktop.
Save davidchase/b0a7e0a64501762ff47ae7df712943b5 to your computer and use it in GitHub Desktop.
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