Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@priestine/pipeline

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@priestine/pipeline

Simple and reusable pipelines with abstracted asynchrony

  • 4.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@priestine/pipeline

@priestine/pipeline is a set of inverted monoids which are built from a composition of handlers called middleware and a data entry point called process.

The main goal of this package is a proof of concept that simplified representation of functional composition can lower the functional programming entry level. It also abstracts asynchrony by resolving Promises passed from one middleware to another.

The Pipeline can be concatenated with other Pipelines with concat which enables multiple sets of middleware to be composed together.

Features

  • Composition of pipelines via concat
  • Promise returned by middleware within Pipeline is resolved before being passed as an argument to the next middleware
  • If middleware returns undefined or null, result of previous computation is passed to the next middleware
  • Pipeline is a Semigroup (associativity) with p.concat(o)
  • Pipeline is a Monoid (right identity and left identity) with p.empty() and P.empty()

Installation

npm i -S @priestine/pipeline

Usage

Pipeline

Node.js
import { Pipeline } from "@priestine/pipeline";
import * as readline from "readline";

const addSpaceIfMissing = question => (question.charAt(question.length - 1) == " " ? question : question.concat(" "));
const transformToContextObject = question => ({ question });
const createReadlineInterface = ctx => ({
  ...ctx,
  rl: readline.createInterface(process.stdin, process.stdout),
});
const askQuestionAsync = ({ rl, question }) =>
  new Promise(res => rl.question(question, (answer: string) => res(answer)));
const makeAnswerGreen = (answer: string) => `\x1b[32m${answer}\x1b[0m`;
const logToConsole = (answer: string) => console.log(answer);
const exit = () => process.exit(0);

Pipeline.from([
  addSpaceIfMissing,
  transformToContextObject,
  createReadlineInterface,
  askQuestionAsync,
  makeAnswerGreen,
  logToConsole,
])
  .process("What is the answer to life, the universe and everything?")
  .then(exit);
Browser
import { Pipeline } from "@priestine/pipeline";

const isOdd = (num: number) => num % 2 == 0;
const negate = (f: Function) => (x: any) => !f(x);
const mockFetch = (x: string): Promise<number[]> => new Promise(resolve => resolve([1, 2, 3, 4, 5]));
const filterOutOddNumbers = (nums: number[]): number[] => nums.filter(negate(isOdd));
const logToConsole = (answer: number[]): void => console.log(answer);

Pipeline.of(mockFetch)
  .pipe(filterOutOddNumbers)
  .pipe(logToConsole)
  .process("https://example.com/arbitrary-numbers")
  .catch(console.error);

Keywords

FAQs

Package last updated on 24 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc