All Projects → erikmueller → cond-flow

erikmueller / cond-flow

Licence: other
Elixir style cond for easy javascript control flow

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to cond-flow

libra-code
quantum-dynamics-hub.github.io/libra/index.html
Stars: ✭ 33 (-10.81%)
Mutual labels:  utilities
FlexDict
Easily work with deeply nested dictionaries and write clean code using FlexDict; a small subclass of dict. FlexDict provides automatic and arbitrary levels of nesting along with additional utility functions.
Stars: ✭ 71 (+91.89%)
Mutual labels:  utilities
puppeteer-extensions
Convenience functions for the Puppeteer
Stars: ✭ 26 (-29.73%)
Mutual labels:  utilities
php-lodash
php-lodash is a PHP utility library, similar to Underscore/Lodash.
Stars: ✭ 35 (-5.41%)
Mutual labels:  utilities
alsa-utils
The Advanced Linux Sound Architecture (ALSA) - utilities
Stars: ✭ 122 (+229.73%)
Mutual labels:  utilities
FunUtils
Some codes i wrote to help me with me with my daily errands ;)
Stars: ✭ 43 (+16.22%)
Mutual labels:  utilities
lightflow
A tiny Promise-inspired control flow library for browser and Node.js.
Stars: ✭ 29 (-21.62%)
Mutual labels:  flow-control
assign-one-project-github-action
Automatically add an issue or pull request to specific GitHub Project(s) when you create and/or label them.
Stars: ✭ 140 (+278.38%)
Mutual labels:  utilities
gut
🍱 yet another collection of go utilities & tools
Stars: ✭ 24 (-35.14%)
Mutual labels:  utilities
timestampy
🕒 Bunch of utilities useful when working with UNIX timestamps
Stars: ✭ 21 (-43.24%)
Mutual labels:  utilities
ios
CoThings's iOS application. CoThings is a realtime counter for shared things.
Stars: ✭ 13 (-64.86%)
Mutual labels:  utilities
slackcat
Concatenate files(s), or stdin, directly to Slack. 🐈
Stars: ✭ 19 (-48.65%)
Mutual labels:  utilities
common
Utilities and base libraries for use across polkadot-js for Polkadot and Substrate. Includes base libraries, crypto helpers and cross-environment helpers. Full documentation & examples available.
Stars: ✭ 221 (+497.3%)
Mutual labels:  utilities
Jenkins-Pipeline-Utils
Global Jenkins Pipeline Library with common utilities.
Stars: ✭ 36 (-2.7%)
Mutual labels:  utilities
util.js
Useful JavaScript Functions in Single File
Stars: ✭ 25 (-32.43%)
Mutual labels:  utilities
framestack
Tools, Frameworks & Libraries to help you build your projects ✨
Stars: ✭ 27 (-27.03%)
Mutual labels:  utilities
MHW-Shop-Editor
Monster Hunter World Provisions Stockpile Shop Editor
Stars: ✭ 52 (+40.54%)
Mutual labels:  utilities
utils
🛠 A collection of light-weight methods and helpers for defensive programming
Stars: ✭ 15 (-59.46%)
Mutual labels:  utilities
youtube-unofficial
Access parts of your account unavailable through normal YouTube API access.
Stars: ✭ 33 (-10.81%)
Mutual labels:  utilities
diamonds
A pile of shiny typed JS helpers for everyday usage
Stars: ✭ 16 (-56.76%)
Mutual labels:  utilities

cond-flow

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

CI status code style: prettier JavaScript Style Guide tested with jest codecov

Install

Install with npm or yarn via

yarn add cond-flow

or

npm i cond-flow

API

type Cond = (
  pairs: Array<[boolean, unknown | (() => unknown)]>,
  options: { strict: boolean }
) => unknown

Usage

import cond from 'cond-flow'

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

// value === 'true'

You can disable strict checking by passing options as the second argument:

import cond from 'cond-flow'

const value = cond(
  [
    [false, 'false'],
    [1, 'truthy'],
    [true, 'true but also too late']
  ],
  { strict: false }
)

// value === 'truthy'

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

import cond from 'cond-flow'

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

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 here. It's best have simple booleans and resort to _.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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].