We're using the previous getRandomColorSetIndex
to compose over getRandomIndexFromItemCount
which now acts as a unit-testable base function. This way, we can change the functionality of getRandomColorSetIndex
without changing the API.
const pluckRandomNumberFromRange = require('./pluckRandomNumberFromRange')
const getRandomColorSetIndex = () => (
pluckRandomNumberFromRange(
colorSets.length,
)
)
const flashRandomLight = (
observable,
) => (
observable
.do(console.log.bind(console, 'isHalloween:'))
.filter(Boolean)
.map(getRandomColorSetIndex)
.map(getColorSetAtIndex)
.map(doScaryLightFlash)
.switchMap(getDataFromPromise)
)
module.exports = flashRandomLight
const pluckRandomNumberFromRange = (
upperBound,
dependencies = {
randomizer: Math.random,
},
) => (
Math
.floor(
dependencies
.randomizer() * upperBound
)
)
module.exports = pluckRandomNumberFromRange