Commit 394d192f authored by Evan Hahn's avatar Evan Hahn

Add allPossibleObjects function (which we'll use in `minify`)

parent 55e218c6
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -2,13 +2,23 @@
"private": true,
"scripts": {
"start": "node src/dev-server",
"test": "standard --fix"
"pretest": "standard --fix",
"test": "jest",
"test:watch": "jest --watch --notify"
},
"devDependencies": {
"babel-minify": "^0.3.0",
"ejs": "^2.5.7",
"express": "^4.16.2",
"jest": "^22.2.2",
"regpack": "^5.0.1",
"standard": "^10.0.3"
},
"standard": {
"globals": [
"describe",
"it",
"expect"
]
}
}
module.exports = function allPossibleObjects (options) {
const keys = Object.keys(options)
if (keys.length === 0) { return {} }
const numberOfResults = Object.values(options).reduce((count, choices) => count * choices.length, 1)
const results = []
for (let i = 0; i < numberOfResults; i++) {
results.push(keys.reduce((result, key) => {
const choices = options[key]
return {
...result,
[key]: choices[i % choices.length]
}
}, {}))
}
return results
}
const allPossibleObjects = require('../src/all-possible-objects')
describe('allPossibleObjects', () => {
it('returns an array of objects with all possible options', () => {
const result = allPossibleObjects({
one: ['foo'],
bool: [true, false],
nums: [1, 2, 3]
})
expect(result).toHaveLength(6)
;[true, false].forEach((bool) => {
expect(result).toContainEqual({
one: 'foo',
bool,
nums: 1
})
expect(result).toContainEqual({
one: 'foo',
bool,
nums: 2
})
expect(result).toContainEqual({
one: 'foo',
bool,
nums: 3
})
})
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment