JavaScript "if" as an expression
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.editorconfig
.gitignore
.travis.yml
LICENSE
README.md
index.js
package.json
yarn.lock

README.md

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.