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

sweet-pipeline

v1.0.1

Published

Sweet-Pipeline is a node package for defining and executing a simple sequential flow of process to process using functional programming principle

Downloads

6

Readme

Sweet-Pipeline Package

npm version GitHub license Build Status Scrutinizer Code Quality GitHub issues

Description

Sweet-Pipeline is a node package for defining and executing a simple sequential flow from process to process using functional programming principle. This process is initiated by a payload that is passed to the pipeline from one pipe (process) to another and finally completes the entire process and returns the final result.

Installation

Node 13 + is required.

To get the latest version of Sweet-Pipeline, simply install it

npm install sweet-pipeline

Usage

Processes which are referred to as pipes in the pipeline are callable functions, closures or anything that can be invoked.

To begin require it at the top.

const Pipeline = require("sweet-pipeline");

Synchronous (Sync) Usage

This is an example where pipes returns a concrete value.

const pipes = [callablePipe1, callablePipe2, callablePipe3];
const pipeData = { data: "data" };

const pipeline = new Pipeline().send(pipeData).through(pipes).return();

Example without the break method

The example below demonstrate a function nairaToUSD() that converts a Nigerian (NGN) naira amount to USD, another function usdToBTC() that converts the result to Bitcoin (BTC) and lastly applyTax() that applies tax to the conversion's final result. Without pipeline this can be implemented like this: applyTax(usdToBTC(nairaToUSD(amount)))

Note all data used here are dummy data, they are not real data

const usdToNairaRate = 200; // 1USD = N200
const btcToUSDRate = 10; // 1BTC = 10USD
const tax = 0.3; // 3%

const nairaToUSD = (amount) => {
  return amount / usdToNairaRate;
};

const usdToBTC = (amount) => {
  return amount / btcToUSDRate;
};

const applyTax = (amount) => {
  return amount - amount * tax;
};

const pipes = [nairaToUSD, usdToBTC, applyTax];
const pipeData = 10000;

const result = new Pipeline().send(pipeData).through(pipes).return();

console.log(result);

/*
Process Calculation

First Process : 10000/200 = 50USD
Second Process : 50/10 = 5BTC
Third Process: 5 - (5 * 0.3)  = 3.5BTC

This will return (int) 3.5BTC
*/

With the example above the amount is passed to every pipe on the pipeline sequentially which is processed by the pipes individually with there separate logics passed to the current result and the final result is returned.

Example with break method

You can also chain the pipeline class with a break method and pass a boolean of true, this will return the result of the first pipe on the pipeline that returns a true response and others will be ignored. This is applicable when you are implementing a pipeline of services with the same logic or implementation or external service.

Note by default the break method is false

class FirstService {
  handle() {
    return {
      handler: "First Service",
      error: "The first service is not avalibale",
    };
  }
}
class SecondService {
  handle() {
    return {
      handler: "Second Service",
      success: "The second service was executed",
    };
  }
}
class ThirdService {
  handle() {
    return {
      handler: "Third Service",
      error: "The third service has an error",
    };
  }
}

module.exports = {
  FirstService,
  SecondService,
  ThirdService,
};

Now using pipeline to return the service that first return a true response.

const firstService = new FirstService();
const secondService = new SecondService();
const thirdService = new ThirdService();

const pipes = [
  firstService.handle(),
  secondService.handle(),
  thirdService.handle(),
];

const result = new Pipeline()
  .send(pipeData)
  .through(pipes)
  .break(true)
  .return();

console.log(result);

/*
  This will return because that pipe is the first pipe that return a true response

    { 
      "handler" : "Second Service",
     "success" : "The second service was executed"
    }

  */

Asynchronous (async) Usage

This is an example where at least one pipe or all the pipes returns a promise. The difference with the sync usage is the way the result is returned. If there is at least one pipe that returns a promise you will have to use the then() method to get the result.

var q = require("q");

const repeatTextAsync = (text) => {
  var deferred = q.defer();

  deferred.resolve(text + ", " + text);

  return deferred.promise;
};

const capitalizeFirstLetterAsync = (text) => {
  var deferred = q.defer();

  deferred.resolve(text[0].toUpperCase() + text.substring(1));

  return deferred.promise;
};

module.exports = {
  repeatTextAsync,
  capitalizeFirstLetterAsync,
};

const pipes = [repeatTextAsync, capitalizeFirstLetterAsync];

const text = "Hello";

const result = new Pipeline()
  .send(text)
  .through(asyncPipes)
  .return()
  .then((value) => {
    console.log(value);
  })
  .catch((error) => {
    console.log(error);
  });

/*
  This will return
  
  Hello, hello

*/

The example above is an asynchronous implementation that returns a promise. The function repeatTextAsync repeats a text passed to it and capitalizeFirstLetterAsync capitalizes the first letter of the first repeated text. When this is passed to the pipeline it will execute the first function and apply the second function's logic to the result and returns the final result.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email abrahamudele@gmail instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.