cond-flow
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

cond-flow

Inspired by Elixir's cond this is a simpler alternative to lodash's _.cond

codecov

Install

Install with npm or yarn via

yarn add cond-flow

or

npm i cond-flow

Usage

import cond from 'cond-flow'

const value = cond([
  [false, 'false'],
  [true, 'true'],
  [true, 'true but too late'],
])

// value === 'true'

Also works nicely with React components as you can have the values lazily evaluated by wrapping them in a function:

import cond from 'cond-flow'

const Component = ({ isDisabled, isNew, isLoading }) => (
  <>
    {cond([
      [isLoading, () => <Loading />],
      [isNew, () => <Create />],
      [isDisabled, null],
    ])}
  </>
)

Default return value

You can provide a default fallback which will be returned if no provided conditions are met.

import cond from 'cond-flow'

const value = cond(
  [
    [false, () => 'false'],
    [false, () => 'also false'],
  ],
  { default: () => 'fallback' },
)

// value === 'fallback'

Note

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 cond for complex use cases.

Development

If you find this useful or would like to add features, feel free to clone the repository and open a PR.

Readme

Keywords

Package Sidebar

Install

npm i cond-flow

Weekly Downloads

20

Version

1.0.0

License

MIT

Unpacked Size

4.67 kB

Total Files

5

Last publish

Collaborators

  • airrick