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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ifx

v1.1.0

Published

Ifx is a JavaScript module that provides if-expression to return a value

Readme

Ifx?

ifx is a JavaScript module that provides "if-expression" to return a value. It can be alternative to ternary operator.

Motivation

Use of ternary operators to express stateless values and functions tends to result in significantly diminished readability when applied to complicated nesting conditions or multiple conditions.
I wanted to use conditional branches built on JavaScript which return values in the same manner as the if-expression in Scala to ensure readability in such situation.

Usage

  • install
npm install ifx
  • import
const If = require('ifx');
  • ES6 syntax (recommend)
// stateless and readable to use ES6 arrow function
const x = If(false)(() => 1).ElseIf(true)(() => 2).Else(() => 3);
console.log(x);
  • formatted
// can be return value
const fn = value =>
  If(value instanceof Array)(() => value.map(x =>
    If(typeof x === 'number')(() => x * 5).Get()))
  .ElseIf(value instanceof Object)(() => Object.keys(value))
  .Else(() => [])
  .filter(x => x !== null);

console.log(fn([1,2,null,3,4,5]));    // [ 5, 10, 15, 20, 25 ]
console.log(fn({a: 1, b: 2, c: 3}));  // [ 'a', 'b', 'c' ]
console.log(fn(2));                   // []
  • ES5 syntax
var If = require('ifx');

var x = If(false)(function () { return 1 })
    .ElseIf(true)(function () { return 2 })
    .Else(function () { return 3 });

console.log(x);  // 2

API

If = condition -> expected function -> object

  • If is a curried function.
  • returned-object has methods Else/ElseIF/Get
// every type can be returning value
console.log(If(true)(() => true).Get());  // true
console.log(If(true)(() => 'a').Get());   // a
console.log(If(true)(() => null).Get());  // null
console.log(If(true)(() => {}).Get());      // undefined
console.log(If(true)(() => [1]).Get());   // [ 1 ]
console.log(If(true)(() => ({})).Get());    // {}

// returned function apply to function only
try {If(true)(1).Get()} catch(e) {console.log(e)}   // [Error: If() con be applied to a function only]

.Get = () -> value

  • if you don't use Else, Get gets rerutning a value.
// If(true)
console.log(If(true)(() => 1).Get());   // 1

// If(false)
console.log(If(false)(() => 1).Get());  // null

.Else = function -> value`

  • When you use Else, Get is unnecessary.
// If + Else
console.log(If(false)(() => 1).Else(() => 2));  // 2

.ElseIf = condition -> expected function -> object

  • Chain ElseIf, if you need conditions more than once.
// If + ElseIf + Get
console.log(If(false)(() => 1).ElseIf(true)(() => 2).Get());                        // 2
console.log(If(false)(() => 1).ElseIf(false)(() => 2).ElseIf(true)(() => 3).Get()); // 3

// If + ElseIf + Else
console.log(If(false)(() => 1).ElseIf(false)(() => 2).ElseIf(false)(() => 3).Else(() => 4));  // 4

// need condition
try {If(true)(() => 1).ElseIf()(() => 2).Get()} catch(e) {console.log(e)}  // [Error: ElseIf connot be applied to an empty value]

TypeScript Import

import * as If from 'ifx';

about specs

  • It returns first matched value when conditions match more than once.
console.log(If(true)(() => 1).ElseIf(false)(() => 2).ElseIf(false)(() => 3).Else(() => 4)); // 1
console.log(If(false)(() => 1).ElseIf(false)(() => 2).ElseIf(true)(() => 3).Get());         // 3
  • It returns null when all conditions don't match.
console.log(If(false)(() => 1).ElseIf(false)(() => 2).Get());                        // null
console.log(If(false)(() => 1).ElseIf(false)(() => 2).ElseIf(false)(() => 3).Get()); // null

Author

jshosomichi

License

under the MIT license.