@slicr97/functional-conditions
v1.0.2
Published
Making conditional statements in JavaScript functional
Downloads
1
Readme
functional-conditions
Making conditional statements in JavaScript functional
Installation
Using npm:
npm install @slicr97/functional-conditions
Using yarn:
yarn add @slicr97/functional-conditions
Usage
Import the functions you want to use:
import { $if, $switch, $tryCatch } from '@slicr97/functional-conditions';
Or, make them available globally by importing at the top of your entrypoint:
import '@slicr97/functional-conditions/globalize';
The $if
function
This function provides a simple, pure wrapper around JavaScript's if
statement. It takes three parameters:
- The condition, which is checked for being truthy or falsy
- The 'then'-case. This can be either a constant value or a function resolving into any return type. This case is executed if
condition
evaluates totrue
. - The 'else'-case. This can also be a constant or a function. This case is executed if
condition
evaluates tofalse
.
The $switch
function
This function emulates a wrapper around JavaScript's switch
statement. It takes one option
parameter, consisting of the following properties:
with
is the value that is being evaluatedcases
is an array of tuples. Each case consists ofc
can be a constant value that will be compared towith
or a function that takeswith
and returns eithertrue
orfalse
then
can be a constant value that will be returned or a function that returns a valuedefault
is the default value that is returned if no cases match. This can be either a constant value or a function that returns a value.
The $tryCatch
function
This function wraps JavaScript's tryCatch
statement. It takes an fn
parameter which is a function that tries to execute.
It also takes a second parameter, err
, which can either be a constant value or a function that takes an error which can be used to format the error and return.
Examples
$if
import { $if } from '@slicr97/functional-conditions';
const a = 2;
const b = 3;
const message = $if (
a > b,
'a is larger than b',
'a is not larger than b'
);
console.log(message); // Will log "a is not larger than b"
$switch
import { $switch } from '@slicr97/functional-conditions';
const age = 43;
const message = $switch ({
with: age,
cases: [
[a => a > 80, 'You\'re very old'],
[a => a > 60, 'You\'re pretty old'],
[a => a > 40, 'You\'re not that old'],
[a => a > 20, 'You\'re pretty young'],
[a => a > 0, 'You\'re very young']
],
default: 'You\'re less than 0 years old???'
});
console.log(message); // Will log "You're not that old"
$tryCatch
import {$tryCatch} from '@slicr97/functional-conditions';
const value = $tryCatch(
() => 3 / 0,
'Cannot divide by zero!'
);
console.log(value); // Will log "Cannot divide by zero!"
Considerations
Switch cases
Switch cases are checked sequentially and execution stops once the first valid case is reached. This means that if you have
[a => a > 20]
and
[a => a > 40]
in that order, the code could never step into the second case, as the first one also satisfies the condition.
Side effects
While it is not strictly necessary to pass pure functions into $if
or $switch
, I strongly encourage doing so. These functions were not built with effectful functions in mind and I won't guarantee that the functions will not be executed out of order.