Coder Social home page Coder Social logo

composable-statements's Introduction

Composable Statements for JavaScript

Usually statements like IfStatement is not composable within expression like function call. This package provides alternative way to express statements as composable function call expression. This may be useful when you are using stuff like JSX, which uses tons of nested expressions.

Currently IfStatement is only implementation so far, but more is to come! Feel free to suggest improvements, features, or report bugs. There is no NPM dependency.

Is this recommended way to express conditional logic?

I do not think so, for the most of case. But I don't like putting && or ternery operator everywhere, this is way better than doing such for the readability's sake, IMHO.

Installation

npm install composable-statements

Usage

start with IfStatement() (if_) and end with fi(). You can chain elif() and else() after if_.

JavaScript

import {
    IfStatement as if_
} from 'composable-statements';

if_(foo, () => 'foo')
    .elif_(bar, () => 'bar')
    .else_(() => 'baz')
.fi();

JSX:

// Conditional event handler
<div onClick={
    if_(foo, () => (e) => { $('#foo').append('<div className="this-is-if" />'); })
        .elif(bar, () => (e) => { $('#bar').append('<div className="this-is-elif" />'); })
        .else(() => (e) => { $('#baz').append('<div className="this-is-else" />'); })
    .fi()
    }
/>

// conditional element
<div>
    {
        if_(foo, <div className="foo" />)
            .elif(foo, <div className="bar-is-complicated"><ul></ul></div>)
        .fi()
    }
</div>

composable-statements's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.