ntw-processor
v1.0.7
Published
A simple module that makes it easy to group functions and process inputs conditionaly
Downloads
4
Maintainers
Readme
ntw-processor
Group your functions into a network and process values
Installation
Using npm:
$ npm i --save ntw-processor
In Node.js:
const NtwProcessor = require('ntw-processor');
Usage
Linear network:
// Create some functions
const a = elem => ++elem;
const b = elem => elem * 3;
const c = elem => elem / 2;
// Instanciate the processor with an array of functions
const processor = new NtwProcessor([a, b, c]);
// Start processing
const input = 1;
processor.init(input).subscribe(output => {
// Expected output: 3
console.log(output);
});
Non linear network:
// Create some functions
const a = elem => ++elem;
const b = elem => elem * 3;
const c = elem => elem / 2;
// Instanciate the processor with an array of config objects
const processor = new NtwProcessor([
{ id: 0, fn: a, next: [1, 2] },
{ id: 1, fn: b },
{ id: 2, fn: c }
]);
// Start processing
const input = 1;
processor.init(input).subscribe(output => {
// Expected output: [6, 1]
console.log(output);
});
Complex network:
const a = elem => ++elem;
const b = elem => --elem;
const c = elem => elem * 3;
const d = elem => elem * 5;
const e = elem => elem / 2;
const f = elem => elem * 2;
const dispatchFn = elem => {
if (elem > 20) {
return [6, 7];
} else {
return 'end';
}
};
const endFn = elem => 'Result : ' + elem;
const processor = new NtwProcessor([
{ id: 0, fn: a, next: ['first', 'second'] },
{ id: 'first', fn: b, next: 'third' },
{ id: 'second', fn: c, next: ['fourth', 5] },
{ id: 'third', fn: d, next: 'fifth' },
{ id: 'fifth', fn: e, next: ['sixth', 'seventh'] },
{ id: 'sixth', fn: e },
{ id: 'seventh', fn: [a, b, c, d] },
{ id: 5, fn: f, next: dispatchFn },
{ id: 'end', fn: endFn },
{ id: 6, fn: b },
{ id: 7, fn: a }
]);
const input = 2;
processor.init(input).subscribe(output => {
// Expected output: [2.5, 75, undefined, 'Result : 18']
console.log(output);
});
API
Constructor
The NtwProcessor constructor takes an array of functions (linear network) or an array of NtwNode objects. The latter has three properties :
{
id: number | string, // Node id
fn: functions[] | NtwNodeInterface[], // Function(s) to perform
next: number | string | number[] | string[] // Next node(s)' id where output should be sent, or a function returning such id
}
Note that the first node should have an ID equal to 0.
init(input, startNode)
The init method starts processing any given input (string, number, date, custom object, etc.). You can specify another starting node id with the second parameter.
init(input: any, startNode: number | string) => any[]
init returns one result per branch in the network.
functions provided
fn: (element: any, ctx: ContextInterface) => any
The first node (with ID 0) receives the input as its first argument and must return an output element, which will be the next function's input, etc.
ctx
parameter lets you access to all resolved values from the network. It has a deps
property which is a map where you can find the input value at one point of the network : Map<number | string, Observable<any>