ignore-if
v1.3.0
Published
Ignore a higher-order function if a condition is met, such as NODE_ENV === 'test'
Downloads
108
Readme
ignore-if
A higher-order function that allows you to ignore higher-order function when
a condition is met, such as NODE_ENV === 'test'
.
Installation
$ npm install --save-dev ignore-if
Usage
One use case for this is in React when you want to wrap your component in a higher-order component, but you'd like to ignore the wrapper in testing. This is useful for things like react-css-modules.
import cssModules from 'react-css-modules'
import ignoreIf from 'ignore-if'
import styles from './styles.css'
class MyComponent {
render() {
return <div></div>
}
}
const isTest = process.env.NODE_ENV === 'test'
export default ignoreIf(isTest, cssModules(MyComponent, styles)))
Automatically Curried
If you omit the second argument, the wrapper, ignoreIf will automatically return a curried function that allows you to call it again with the second argument:
import cssModules from 'react-css-modules'
import ignoreIf from 'ignore-if'
import styles from './styles.css'
const ignoreIfTest = ignoreIf(process.env.NODE_ENV === 'test')
class MyComponent {
render() {
return <div></div>
}
}
export default ignoreIfTest(cssModules(MyComponent, styles)))
Compose and Pipe
You can use this tool easily with compose() or pipe() from functional libraries like Ramda:
import {pipe} from 'ramda'
import connect from 'react-redux'
import cssModules from 'react-css-modules'
import ignoreIf from 'ignore-if'
import styles from './styles.css'
const ignoreIfTest = ignoreIf(process.env.NODE_ENV === 'test')
class MyComponent {
render() {
return <div></div>
}
}
export default pipe(
ignoreIfTest(cssModules(styles)),
connect()
)(MyComponent)
License
The MIT License (MIT)
Copyright (c) 2015 Brainspace Corporation