cond-flow
Version:
Elixir style cond for easy javascript control flow
76 lines (52 loc) • 1.69 kB
Markdown
Inspired by [Elixir's `cond`](https://elixir-lang.org/getting-started/case-cond-and-if.html#cond) this is a simpler alternative to [lodash's `_.cond`](https://lodash.com/docs/4.17.15#cond)
[](https://codecov.io/gh/erikmueller/cond-flow)
Install with npm or yarn via
```sh
yarn add cond-flow
```
or
```sh
npm i cond-flow
```
```js
import cond from 'cond-flow'
const value = cond([
[],
[],
[],
])
// value === 'true'
```
Also works nicely with React components as you can have the values lazily evaluated by wrapping them in a function:
```jsx
import cond from 'cond-flow'
const Component = ({ isDisabled, isNew, isLoading }) => (
<>
{cond([
[],
[],
[],
])}
</>
)
```
You can provide a `default` fallback which will be returned if no provided conditions are met.
```js
import cond from 'cond-flow'
const value = cond(
[
[],
[],
],
{ default: () => 'fallback' },
)
// value === 'fallback'
```
As all predicates have to be evaluated before the right branch can be chosen, it can have a negative performance impact if you rely on heavy computations. It's best to have simple booleans and resort to [Ramda's](https://ramdajs.com/docs/#cond) `cond` for complex use cases.
If you find this useful or would like to add features, feel free to clone the repository and open a PR.