FCF
What is FCF?
FCF is a Monadic Functional Control Flow Micro-Library for Javascript written in Typescript.
It aims at providing a functional and semantic alternative to some native Javascript control flow statements like if
, switch
and while
.
Native Imperative Flow Statements
Keywords like if
or switch
are imperative statements that normally must be combined to functions to give them a semantic meaning for example:
// BAD imperative (non semantic)if documentvisibilityState === 'visible' // do stuff with visible document else ifdocumentvisibilityState === 'hidden' // do stuff with hidden document
// a bit better... with a semantic functionsconst isDocumentVisible = documentvisibilityState === 'visible'const isDocumentHidden = documentvisibilityState === 'hidden' // notice that here that we didn't store the value of the conditions// for that we need to introduce new variablesif // do stuff with visible document else if // do stuff with hidden document
Functional Control Flow
With FCF we can write your program flow in a more semantic way retaining the value of its conditional statement:
const checkIfApplicationIsActive = fcf // check the conditionconst value = checkIfApplicationIsActive // the value returned by the first `then` call matched ('active'|'standby')console
Notice that FCF is strictly typed so you can rewrite the example above in typescript in this way:
// with `[string]` you can define the type of arguments provided to the 'run' function // `string` is the value retained by the fcf.if object .ifvalue === 'visible' .then // `value` here is of type `string` .elseIfvalue === 'hidden' .then // check the condition // will be of type "string"console.logvalue
Another big advantage of FCF is that its API is composable:
const greetUserByRole = fcf const checkUser = fcf checkUser checkUser checkUser
IfFlow - fcf.if
fcf.if
provides an alternative to the native Javascript if
statement.
fcf
Any fcf.if
object has the following properties
else(fn: function)
- provide a fallback method if none of the conditions are matchedelseIf(fn: function|any)
- add a new condition that must be followed by athen
callthen(fn: function)
- add a callback to a previous condition and set thevalue
propertyvalue
- value returned by the first matchingthen
callrun(...args: any[])
- run the condition flow passing eventually arguments to it
Examples
simple
The simplest fcf.if
might look like this:
fcf
if-else
The else
method works like for normal if
statements
fcf
if-else-if
With the elseIf
method you can add new conditions
fcf
functional conditions
The fcf.if
and fcf.if.elseIf
methods accept also functions as argument.
fcf
functional conditions with arguments
The fcf.if.run
method allows you to pass arguments into your ifFlow chain
fcf
value property
The fcf.if
objects will retain the value returned by the first then
call matched
const value = fcf console // hello
SwitchFlow - fcf.switch
fcf.switch
provides an alternative to the native Javascript switch
statement.
It normalizes also the default switch
behavior avoiding the need of break
statements: the first case
matched will avoid the evaluation of the others
fcf
Any fcf.switch
object has the following properties
default(fn: function)
- provide a fallback method if none of thecase
is matchedcase(fn: function|any)
- add a new case that must be followed by athen
callthen(fn: function)
- add a callback to a previouscase
call and can set thevalue
propertyvalue
- value returned by the first matchingthen
callrun(...args: any[])
- run the condition flow passing eventually arguments to it
Examples
simple
The simplest fcf.switch
might look like this:
fcf
switch-default
The default
method works like for normal switch
statements: if no case
is matched the default
method will be called.
fcf
functional cases
The fcf.switch
and fcf.switch.case
methods accept also functions as argument.
fcf
functional cases with arguments
The fcf.switch.run
method allows you to pass arguments into your switchFlow chain
fcf
value property
The fcf.switch
objects will retain the value returned by the first then
call matched
const value = fcf console // hello
WhileFlow - fcf.while
fcf.while
provides an alternative to the native Javascript while
statement.
It normalizes its behavior in browsers and node using requestAnimationFrame
or setImmediate
to run loops
fcf
Any fcf.while
object has the following properties
do(fn: function)
- add a callback that will be called forever when the whileFlow is running. If ado
function will returnfalse
the while flow will be stoppedbreak(fn?: function)
- if called, it will stop the while flow. It accepts eventually a callback to call when the flow will be stoppedvalue
- value returned by the initial control functionrun(...args: any[])
- run the while flow passing eventually arguments to it
Examples
### simple
The simplest fcf.while
might look like this:
fcf
break
The break
allows to stop the while flow
const loggerFlow = fcf // stop the while loop after 1 second
functional control function
The fcf.while
method accept also functions as argument.
fcf // it will log until the document.visibilityState === 'visible' // otherwise it will be stopped
stop the while flow from a do function
The fcf.while.do
can stop the while flow returning false
fcf // it will log only once and then stop the while flow
functional with arguments
The fcf.while.run
method allows you to pass arguments into your whileFlow chain
const greetUser = fcf fcf
value property
The fcf.while
objects will retain the value returned by its initial control function
const value = fcf console // true
TODO
- Provide async functions support
- Add more control flow methods