npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ntw-processor

v1.0.7

Published

A simple module that makes it easy to group functions and process inputs conditionaly

Downloads

14

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>