fluid-func
A way to write your code with functional programming in mind
Getting started
- Installation
npm install --save fluid-func
- Javascript (ES6)
;
- Javascsript
var FluidFunc = ;
Note: This package is basically a fork of fluid-chains with lighter packaging, less dependencies, a more secure aproach to chaining functions and focused on functional programming.
Two ways to create a Func
'_1stFunc' { // do some work here };
FluidFunc ;
Two ways to start a Func
FluidFuncstart'_1stFunc' paramName:'paramVAlue' ;
FluidFunc ;
Creating a Func sequence
'_1stFunc' { // do the first func }; '_2ndFunc'{ //do the 2nd func }; FluidFuncstart'_1stFunc''_2ndFunc' paramName:'paramValue' ;
FluidFunc ;
Accessing a parameter
Parameters are immutable and can be accessed by calling the field as function.
'_1stFunc' { // do the first func const paramValue = param; }; FluidFuncstart'_1stFunc' paramName:'paramValue' ;
Maintaining a context
The first parameter is accessible across the Func sequence.
'_1stFunc' { // do the first func const paramValue = param; }; '_2ndFunc'{ //do the 2nd func const paramValue = param; }; FluidFuncstart'_1stFunc''_2ndFunc' paramName:'paramValue' ;
In Func sequence you can set the next parameter by returning a value or an object in the action method.
'_1stFunc' { // do the first func const paramValue = param; return from1st:'_1st says hi' person: name: 'John Doe' age: 22 ; }; '_2ndFunc'{ //do the 2nd func const paramValue = param; const _1stSaysHi = param; // got from the previous func const name = param; }; FluidFuncstart'_1stFunc''_2ndFunc' paramName:'paramValue' ;
Returning a literal value will be accessible by paramater.value on the next Func.
'_1stFunc' { // do the first func const paramValue = param; return '_1st says hi'; }; '_2ndFunc'{ //do the 2nd func const paramValue = param; const _1stSaysHi = paramvalue; // got from the previous func }; FluidFuncstart'_1stFunc''_2ndFunc' paramName:'paramValue' ;
Returning a promise in Func
'_1stFunc' { // do the first func const paramValue = param; return { ; }; }; '_2ndFunc'{ //do the 2nd func const paramValue = param; const _1stSaysHi = paramvalue; // got from the previous func }; FluidFuncstart'_1stFunc''_2ndFunc' paramName:'paramValue' ;
Restricting parameters with func.strict
If you want to be strict with parameters and get only what you need you can enable strict mode per func.
'_2ndFunc' { const paramValue = parameters; const iNeedThisAlso = parameters; // parameters.fillerParam will be undefined } ; FluidFuncstart'_1stFunc' paramName:'paramValue' fillerParam:'not needed value' moreParam:'I need this also' ;
func.onBefore
If you want to have something to do before runing the Func use the func.onBefore.
'_1stFunc' { } ;
Using promises.
'_1stFunc' { // do some func thingy here } ;
Note: FluidFunc will just skip and continue to the next Func when you return false value.
func.onFail
A way to handle error or breakage in Func process. In this you have options to retry the Func or break the sequence.
'_1stFunc' { // do some func thingy here } ;
onFail: Function(error: Error, retry: Function, reject: Function)
Param | Description |
---|---|
error | Error instance that contains stack trace and error message |
retry | Function that triggers the reprocess of Func |
reject | Breaks the Func sequence |
Specifications and validations with func.spec
spec.require
- To require value from parameters add require:true in spec
'_1stFunc' { const mandatoryField = parameter; //this field will always have value } ; FluidFuncstart'_1stFunc' mandatoryField: 'hello' ;
- Adding custom message
'_1stFunc' { const mandatoryField = parameter; //this field will always have value } ; FluidFuncstart'_1stFunc' mandatoryField: 'hello' ;
- Transforming the parameter value
transform: Function(current: String): Promise
Param | Description |
---|---|
currentValue | Has the current value of the parameter |
Note: Transform function should always return Promise
'_1stFunc' { const userData = parameter; // this is now a user object } ; FluidFuncstart'_1stFunc' user: '#432userID' ;
- Translating the parameters
Tranlates the parameter value into a new sets of parameters
transform: Function(current: String): Promise
Param | Description |
---|---|
value | Has the current value of the parameter |
context | Func current context |
Note: Tranlate function should always return Promise
'_1stFunc' { const username = parameter; const fullname = parameter; } ; FluidFuncstart'_1stFunc' user: username: 'rickzx98' fullname: 'Jerico de Guzman' ;
- Customer validator
validate: Function(current: String): Promise
Param | Description |
---|---|
currentValue | Has the current value of the parameter |
'_1stFunc' { const userData = parameter; // this is now a user object } ; FluidFuncstart'_1stFunc' email: '[email protected]' ;
Reducer
To make the Func act as reducer use func.reduce(${fieldToReduce})
'_1stFunc' { return current + parametervalue ? parametervalue : 0; }; FluidFuncstart'_1stFunc' sampleArray: 1 2 3 4 5 ;
Cache
func.cache(cachedFor:number)
Param | Description | Default |
---|---|---|
cachedFor | Sets the number (in milliseconds) for how long it will cache the Func | 1500 |
var counter = 0; '_1stFunc' { counter++; return '_1st'; } ; '_2ndFunc' { counter++; return '_2nd'; } ; FluidFuncstart'_1stFunc''_2ndFunc''_1stFunc''_2ndFunc' hi:'hello' ;
Note: The cache is based on the parameter set to the Func action so enabling .strict() mode will make cache more effective.
Monitoring
To monitor a single Func action you must do the following:
FluidFunc;
- Monitor contains the detail of every Func process including it parameters and resolved context.
Plugins
Field | Type | Description | Required |
---|---|---|---|
name | String | Name of the plugin | true |
action | Function | Action that will run | true |
before/after | array | list of Func names the action will invoke to before/after | atleast one of either |
- Plugin.action: Function(context: Context) - Will handle the plugin execution. All return types will be merged to the Context parameter.
- Plugin.before - Will run the plugin before executing the Func
- Plugin.after - Will run the plugin after the Func completes
FluidFunc; FluidFunc; FluidFunc ;
Authors
- Jerico de Guzman - LinkedIn
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE.md file for details