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

2.1.0 • Public • Published

elegir

A simple function to do switch-like expressions that look good, so you don't have to nest your ternaries.

import { elegir } from 'elegir'

export const isHuge = (planetName) => elegir(
  planetName === 'jupiter',
  true,

  planetName === 'saturn',
  true,

  true, // default case
  false
)

The way it works is that each odd argument (1st, 3rd, 5th, …) will be treated as a condition, and if it is true, the next even argument will be returned. The first condition to evaluate to true will end the chain.

So:

isHuge('jupiter') // => true
isHuge('saturn') // => true
isHuge('mars') // => false
isHuge('earth') // => false
isHuge('anything else') // => false

elegir simply means "to choose" in Spanish

Dependencies (1)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i elegir

    Weekly Downloads

    3

    Version

    2.1.0

    License

    MIT

    Unpacked Size

    72 kB

    Total Files

    9

    Last publish

    Collaborators

    • julia.spehrer
    • ijigg
    • deepsweet
    • xaviervia
    • psxcode
    • irminakacz
    • fosimus
    • atilaz
    • moria.am
    • gideon.gur