With fi you can ignore the language constructs and write all your conditional logic in a functional way.
var fi = require'fi';var myVar = fifalse "The dragon"elsfi return 1==2; return "Vampire";els"Wargulf"ret;// myVar is "Wargulf"
The library is distributed as an npm module:
npm install fi
If you want to use this in the browser you can use Browserify for all your npm needs.
I also plan to make a client side ready version, hit me up with a github issue if you're interested in seeing that happen sooner.
Basic if statement
var myVar = fitrue "flower puppy"ret; // we need to add .ret() to get the value of a statement// myVar is "flower puppy"
We can make it more interesting and add an else statement:
var myVar = fifalse "flower puppy"els"space pedals"ret;// myVar is "space pedals"
Even more interesting using an if-else statement as well:
var myVar = fifalse "flower puppy"elsfitrue "human skin"els"space pedals"ret;// myVar is "human skin"
With a half completed if statment, we can also start chaining more stuff to it later
var myif = fifalse "flower puppy"elsfifalse "human skin";// do some other stuff, and add to the chain:var myVar = myifels"crapware"ret;// myVar is "crapware"
Wait, this is not functional enough!
Ok, how about: any value passed into any fi conditional can be either a function, or a value
var myVar = fi return 1 < 0;return 300 / 0;elsreturn 1337/7; ret;// myVar is 191
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.
var ternary = require'fi'ternary;var myVar = ternary return 1 > 4; return 15; return 42; ret;// 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"ret;// myVar is "Some color"
Switch statement using an array (so you can use functions as your keys):
var myVar = sw"Rainbow"return "red"; "Redish"return "green"; return "Random green";return "blue"; "Blueish"return "Some color"; // our defaultret;// myVar is "Some color"
- Becuase somebody had to
- It could potentially lead to some interesting use cases
This library is pretty fresh and any pull requests or feature requests are welcome.
Arnor Heidar Sigurdsson @arnorhs on Twitter