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

fi

v1.0.16

Published

Javascript library for writing, chaining and using conditional statements in a functional way

Downloads

334

Readme

fi - Functional conditionals

With fi you can ignore the language constructs and write all your conditional logic in a functional way.

usage with Typescript

import { fi } from 'fi'

const myVar: string = fi(false, 'The dragon')
  .elseif(
    () => 1 == 2,
    () => 'Vampire',
  )
  .else('Wargulf')
  .ret()

usage with javascript / CommonJS

const { fi } = require('fi')

const myVar = fi(false, 'The dragon')
  .elseif(
    () => 1 == 2,
    () => 'Vampire',
  )
  .else('Wargulf')
  .ret()

Installation

The library is distributed as an npm module:

npm install fi

or

yarn add fi

Examples

Basic if statement

const getIfTrue = fi(true, 'flower puppy')

// getIfTrue() returns "flower puppy"

We can make it more interesting and add an elseif & else statements:

const getIfTrue = fi(false, 'flower puppy')
  .elseif(() => false, 'something else')
  .else('space pedals')

// getIfTrue() returns 'space pedals'

Even more interesting using an if-else statement as well:

With a half completed if statment, we can also start chaining more stuff to it later

const myif = fi(false, 'flower puppy').elseif(false, 'human skin')

// do some other stuff, and add to the chain:
const myVar = myif.else('crapware')

// myVar is "crapware"

Any value passed into any fi conditional or return value can be a function

Functions that don't meet a conditional are never executed, and conditionals that dont' meet a condition (like an else if conditional in an if statement evaluated as true) will also never get executed.

Ternary

The ternary variant is not chainable and does not return a function

import { ternary } from 'fi

const myVar = ternary(() => 1 > 4), () => 15, () => 42)

// myVar is 42

Switch statement using an object:

var sw = require('fi').sw

var myVar = sw('Rainbow', {
  red: 'Redish',
  green: 'Greenish',
  blue: 'Blueish',
  default: 'Some color',
})

// myVar is "Some color"

Switch statement using an array (so you can use functions as your keys):

var myVar = sw("Rainbow", [
    () => "red", "Redish",
    () => "green", () => "Random green",
    () => "blue", "Blueish",
    () => "Some color" // our default
})

// myVar is "Some color"

But.. WHY?!

  1. I don't know
  2. It could potentially lead to some interesting use cases

Author:

Arnor Heidar Sigurdsson @arnorhs on Twitter

License

MIT