Maybe better conditionals feat. tagged template literals

Overview

npm npm bundle size GitHub license


Logo

condicional

Maybe better conditionals
Report Bug · Request Feature

About The Project

⚠️ `condicional` is based on a PEG grammar and is not battle-tested.

Often times there's some mapping logic that needs to be applied to components to return values. Simple mappings can be achieved with a plain object. Adding a default value commonly adds an edge case. Things get more complicated once logic needs to be applied to an input e.g. the input is odd. The uncommon but existing case is nesting logic for checks. The tool that most resembles what comfortably covers these cases is Ramda's cond and its derivatives. However, it lacks feature richness and suffers from array overload.

(back to top)

Package Details

(back to top)

Getting Started

condicional uses tagged template literals to combine runtime data and static pieces in a single, concise mapper. The syntax in a nutshell is

cond`
  static_value_1 result_1
  static_value_2 result_2
  ${isEven} {
    static_value_3 result_3
    fallback_1
  }
  fallback
`

Installation

pnpm add condicional

(back to top)

Basic Usage

Just import cond from condicional and build your mapper.

const mapper = cond`
  ${isEven} 0
  ${isOdd} 1
  ${'not an integer'}
`

mapper(10) === '0' // true
mapper(5) === '1' // true
mapper(0.5) === 'not an integer' // true

condicional stores your functions, so you can create stable conditional components

const isError = ({ type }) => type === 'error'
const isWarning = ({ type }) => type === 'warning'

const mapper = cond<JSXElementConstructor>`
  ${isError} ${ErrorIcon} 
  ${isWarning} ${WarningIcon}
  ${OfflineIcon}
`

const StatusIcon = (props) => {
  const Component = mapper(props)
  return <Component {...props} />
}

// <StatusIcon type="error" size="small" /> renders <ErrorIcon type="error" size="small" />
You might also like...

Better looking pdf for lovely rustacean 3

The Rust Programming Language (TRPL) PDF Better looking pdf in your finger tips. Please find the latest version of pdf in release tab. Contributing I

Dec 29, 2022

Get better insight on why your Remix app crashed during development 💥

Get better insight on why your Remix app crashed during development 💥

Remix Crash A root development ErrorBoundary / for your Remix apps. Overview Remix Crash is a development overlay to simplify debugging during your

Jul 19, 2022

Yet another eslint blame (might) with better adaptability

yet-another-eslint-blame Yet another eslint blame (might) with better adaptability. The input is eslint's output with json format (You can see it here

Mar 7, 2022

The Next.js reference implementation on how to design better APIs

The Next.js reference implementation on how to design better APIs

This is the Next.js reference implementation of my blog bost on How to design better APIs. Getting Started Clone the repository git clone https://gith

Nov 22, 2022

📦 Custom eslint rules for a better life.

Eslint Plugin Rimac Architecture When creating a new rule put it in the src/rules folder Name the file the same as the rule itself For example rimac/i

Nov 22, 2022

The Better Version Of Classic Game, Wordle

The Better Version Of Classic Game, Wordle

Wordable The Better Wordle 🟩 🟨 ⬛ The better version of the classic game wordle with web3 support. Made for Hashnode Netlify Hackathon. Check the blo

Aug 6, 2022

🔗 A better npm link

npx link A safer version of npm link. Why is npm link unsafe? Read the blog post. Features 🔗 Link dependencies without removing previous links 🛡 Onl

Dec 24, 2022

A better redistribution of node-fetch

node-fetch-native A redistribution of node-fetch v3 for better backward and forward compatibility. Why this package? We can no longer require('node-fe

Dec 29, 2022

DNS-Blocklists: For a better internet - keep the internet clean!

DNS-Blocklists: For a better internet - keep the internet clean!

DNS Blocklists - For a better internet! Multi - Cleans the Internet and protects your privacy! An all in one blocklist based on the OISD blocklist, ba

Jan 2, 2023
Releases(v0.8.0)
Owner
Why did Microsoft need to go and turn Github into a social media platform? The next thing you know, Github Chat.
null
A small library aims to improve better tagged-unions/discriminated-unions supporting for TypeScript

coproduct A small library aims to improve better tagged-unions/discriminated-unions supporting for TypeScript Benefits Small bundled size(just 1kb) Ea

工业聚 29 Aug 15, 2022
A simple, template literals-based preprocessor for WGSL shaders

WGSL Preprocessor This library provides a dirt-simple way of adding simple preprocessor macros to your WGSL shaders via tagged templates. It supports:

Brandon Jones 19 Nov 17, 2022
A JavaScript object getter and setter with string literals ⚡

objectio (object + io) A JavaScript object getter and setter with string literals ⚡ Usage import { get, set } from 'objectio'; const obj = { a: 1,

Shiono Yoshihide 2 Sep 14, 2022
Generate tagged PDFs from your Eleventy site

eleventy-plugin-prince-pdf This plugin makes it easy to generate tagged PDFs from webpages in your Eleventy site, using Prince. What this does This pl

null 4 Feb 9, 2022
The world's greatest open source 3D CAM software. (Maybe one day.)

Meshmill is open source 3D CAM software for Linux. It turns STL models into G-code for CNC machines. This is beta-quality software. Use it at your own

James Stanley 20 Dec 21, 2022
A social-media mock app for the ones who love to read - and maybe show it off

?? ?? Cachalote ?? ?? Share what you are reading and find people who also likes it - or not! What does it do? This app focuses on three main questions

Thaís França 3 May 22, 2022
A maybe slightly safer-ish wrapper around eval Function constructors

evalish A maybe slightly safer-ish wrapper around eval Function constructors Please maybe try something else first.. Please. evalish is a small helper

Phil Pluckthun 24 Sep 6, 2022
A maybe slightly safer-ish wrapper around eval Function constructors

evalish A maybe slightly safer-ish wrapper around eval Function constructors Please maybe try something else first.. Please. evalish is a small helper

0no.co 22 Aug 21, 2022
Extended version of create-t3-app to make it even faster to start (or maybe slower)

create-T3-app with extra tools/config out of the box create-t3-app is one of the fastest and easiest way to scaffold fullstack app. create-t3-extended

A Riolly C 7 Jan 4, 2023
Placebo, a beautiful new language agnostic diagnostics printer! It won't solve your problems, but you will feel better about them.

Placebo A beautiful new language agnostic diagnostics printer! ┌─[./README.md] │ > 1 │ What is Placebo? · ───┬──── ·

Robin Malfait 78 Dec 16, 2022