Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 

if-expression

semantic-release Build Status

JavaScript "if" as an expression.

Install

npm install --save if-expression

Use

Run some code if the predicate is true:

import iff from "if-expression";

const message = iff(
  true,
  () => "Cool!"
); // => Cool!

Run some other code if the predicate's false:

import iff from "if-expression";

const message = iff(
  false,
  () => "Cool!",
  () => "Not so cool."
); // => Not so cool.

Check many things!

import iff from "if-expression";

const message = iff(
  false,
  () => "Cool!",
  true,
  () => "Pretty cool!",
  () => "Not so cool."
; // => Pretty cool!

Just return a value if everything is falsey:

import iff from "if-expression";

const message = iff(
  false,
  () => "Cool!",
  false,
  () => "Pretty cool!",
  "Not too shabby!"
); // => Not too shabby!

You can even return a value from the truthy branch, if you really want:

import iff from "if-expression";

const message = iff(
  true,
  "Cool!",
  "Not too shabby!"
); // => Cool!

What to do if this makes you nervous...

It can act like a statement too!

import iff from "if-expression";

let message;
iff(true, () => {
  message = "Cool!";
}, () => {
  message = "Not so cool.";
});

Development

$ npm install
$ npm test

Commit messages should follow the Angular commit message guidelines.

Release

This repository uses semantic-release. Changes will automatically be released to npm.

About

JavaScript "if" as an expression

Resources

License

Packages

No packages published