overview
ifelif is a very simple javascript package that lets you create conditional statements as chainable methods. basically, every chain returns the result of the first true condition, and if nothing works, it just returns null. it’s pretty much like a ternary operator but more readable, and that’s why i built it.
as a vue developer, i really didn’t like how react handles conditional rendering. react relies on ternaries everywhere, while vue has v-if, v-else-if, and v-else directives. i wanted something more readable for my react projects, so i made ifelif.
lightweight
ifelif is only 736 bytes (minified). it’s very fast, and performs nearly as good as normal if-else statements. you can see the performance comparison with native if-else statements, ternaries, and other libraries below.
normal ~ 709.8368759155273 ms
ternary ~ 631.1364431381226 ms
ifelif ~ 839.0895328521729 ms
iffy ~ 1183.1396884918213 ms
ifx ~ 1099.2623691558838 ms
jif ~ 1163.1959772109985 ms
usage
const name = 'John';
_if(name === 'John')
.then(() => console.log('Hello John!'))
.else(() => console.log('Hello stranger!'));
// Hello John!