npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

if-exp

v2.0.2

Published

Conditional (if) expressions for JavaScript

Downloads

274

Readme

if-exp

Build Status Coverage Status Deps NPM version Downloads extra Twitter Follow

Conditional (if) expressions for JavaScript

NPM

npm install if-exp

Usage

const iff = require('if-exp');

const result = iff(condition)
            .then('result 1')
        .elseIff(secondCondition)
            .then('result 2')
        .elseIff(thirdCondition)
            .then('result 3')
        .otherwise('result 4');

Motivations

Some languages implement conditionals (and other well known statements) as expressions, so we can write:

val res = if(condition){
    'result 1'
} else if(secondCondition) {
    'result 2'
} else if(thirdCondition) {
    'result 3'
} else {
    'result 4'
}

While in JavaScript, conditional expressions can be achieve using ternary operators. Multiple conditions are sometimes hard to read using ternary:

var res = condition ?  'result 1' :
    (secondCondition ? 'result 2' :
        (thirdCondition ? 'result 3' : 'result 4'));

Here is how to write the same thing with iff:

const res = iff(condition)
            .then('result 1')
        .elseIff(secondCondition)
            .then('result 2')
        .elseIff(thirdCondition)
            .then('result 3')
        .otherwise('result 4');

How to do lazy-statements evaluation in JavaScript

const res = iff(condition1)
  .then(statement1())
elseIff(condition2)
  .then(statement2())
.otherwise(statement3());

The issue with the above code, is that statement1, statement2 and statement3 functions will be executed no matter the value of condition and condition2. To only execute one or the other we can use functions:

const res = iff(condition1)
  .then(statement)
elseIff(condition2)
  .then(statement)
.otherwise(statement)
();

Now, if-exp will yield a function depending on the conditions and we directly execute it resulting in a lazy statement evaluation.

How if-exp supports lazy condition evaluation

const res = iff(conditionalFn1())
  .then(1)
elseIff(conditionalFn2())
  .then(2)
.otherwise(3);

The issue with the above code is that conditionalFn1 and conditionalFn2 will be both executed. Since if-exp v2+, you can specify a predicate function as a condition and it will be evaluated or not depending on the previous conditions:

const res = iff(conditionalFn1)
  .then(1)
elseIff(conditionalFn2)
  .then(2)
.otherwise(3);

Changelog

Donate

I maintain this project in my free time, if it helped you please support my work via paypal or Bitcoins, thanks a lot!