@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.
- 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
ornull
, 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()
andP.empty()
npm i -S @priestine/pipeline
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);
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);