Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Elixir's cond construct implementation for Javascript and Typescript

NotificationsYou must be signed in to change notification settings

mudssrali/cond-construct

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Inspired byElixir'scond this is a simpler alternative tolodash's_.cond

CI statusJavaScript Style Guidetested with jest

Install

Install with npm or yarn via

yarn add cond-construct

or

npm i cond-construct

API

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

Usage

importcondfrom'cond-construct'constvalue=cond([[false,'false'],[true,'true'],[true,'true but too late']])// value === 'true'

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

importcondfrom'cond-construct'constvalue=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:

importcondfrom'cond-construct'constComponent=({ hasErrors, isNew, isLoading})=>(<>{cond([[isLoading,()=><Loading/>],[isNew,()=><Create/>],[hasErrors,()=><ShowErrors/>]])}</>)

Next

  • [] Handle multiple method executions
  • [] Add more option for falsy value

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.

About

Elixir's cond construct implementation for Javascript and Typescript

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp