Skip to content

Commit

Permalink
feat(ifUtils): add configurable ifXXX and ifNotXXX methods (#8)
Browse files Browse the repository at this point in the history
closes #7
  • Loading branch information
rrag authored and Kent C. Dodds committed Aug 30, 2016
1 parent e1dbec9 commit 67dacf8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/get-if-utils.js
Expand Up @@ -31,16 +31,16 @@ export {getIfUtils}
* This returns an object with methods to help you conditionally set values to your webpack configuration object.
* @param {Object|string} env This would be either the `env` object from webpack (function config API) or the value of
* `process.env.NODE_ENV`.
* @param {Array} vars A list of valid environments if utils are generated for
* @return {IfUtils} the IfUtils object for the given environment
*/
function getIfUtils(env) {
function getIfUtils(env, vars = ['production', 'prod', 'test', 'development', 'dev']) {
env = typeof env === 'string' ? {[env]: true} : env
if (typeof env !== 'object') {
throw new Error(
`webpack-config-utils:getIfUtils: env passed should be a string/Object. Was ${JSON.stringify(env)}`
)
}
const vars = ['production', 'prod', 'test', 'development', 'dev']
return vars.reduce((utils, variable) => {
const envValue = !!env[variable]
const capitalVariable = capitalizeWord(variable)
Expand Down
18 changes: 18 additions & 0 deletions src/get-if-utils.test.js
Expand Up @@ -15,6 +15,16 @@ test('has if and ifNot methods for the expected environment values', t => {
const diff = difference(methods, expectedMethods)
t.deepEqual(diff, [])
})
test('has ifXXX and ifNotXXX methods for the expected environment values', t => {
const expectedMethods = [
'ifFoo', 'ifNotFoo',
'ifBar', 'ifNotBar',
]
const utils = getIfUtils({}, ['foo', 'bar'])
const methods = Object.keys(utils)
const diff = difference(methods, expectedMethods)
t.deepEqual(diff, [])
})

test('works with string values', t => {
const {ifProduction} = getIfUtils('production')
Expand All @@ -36,3 +46,11 @@ test('returns true/false when given no arguments', t => {
t.is(ifProd(), false)
t.is(ifNotDev(), true)
})

test('returns true/false for custom ifXXX when given no arguments', t => {
const {ifWatch, ifProd, ifNotDev, ifTest} = getIfUtils('watch', ['prod', 'dev', 'watch'])
t.is(ifWatch(), true)
t.is(ifProd(), false)
t.is(ifNotDev(), true)
t.is(ifTest, undefined)
})

0 comments on commit 67dacf8

Please sign in to comment.