🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

pipeline-async

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pipeline-async

Allows for both sync and async pipelines with TS support

0.0.2
latest
Source
npm
Version published
Weekly downloads
4
33.33%
Maintainers
1
Weekly downloads
 
Created
Source

pipe-async

This package emulates proposed functionality of pipelines in https://github.com/tc39/proposal-pipeline-operator

Explanation

Some languages offer bash-like pipelines straight out of the box. This allows you to simplify complex operation as you can write


5 | firstFn | secondFn | thirdFn

instead of the typical


thirdFn(secondFn(firstFn(5)))

The first example is arguably more natural to read, since the logic goes from left to right.

Features:

While there are many other packages that add pipeline functionality for JS apps, this one is different:

Seamless typescript support for any length of pipeline

You can call up to 9 pipings at once, but can call the resulting pipeline again with more params for unlimited and/or conditional piping!


import pipe from "pipeline-async";

const basicPipe = pipe(5, x => x + 2, x => x + 1);
const extendedPipe = basicPipe(x => x + 3, x => x + 4); // Does not mutate the original pipe!

Supports, but does not require await/async

You have synchronous pipeline packages, you have asynchronous pipeline packages, but this package supports both. It has a configurable interface for when you use non-Promise class based promises (import { setConfig }), and you never have to deal with it returning a promise, unless one of the pipes returns one! This behavior is fully supported by the used TS-typing with conditional types. Works especially well when you're trying to evaluate request, or requests, to server.


const basicPipe = pipe(5, x => x + 2);
console.log(basicPipe()) // prints 7
// You can have multiple asynchronous functions and need only 1 await
const asyncPipe = await basicPipe(async x => x + 4, x => x + 1 /* Does NOT need await/async! */);
console.log(await asyncPipe()) // prints 12

Only evaluates a pipeline when called

Normally, most libraries will simply evaluate a pipeline, which is fine for most cases, but delayed evaluation may be important for optimization or advanced chaining.


const basicPipe = pipe(5, x => x + 5);
// Does not evaluate yet
console.log(basicPipe) // prints a function
console.log(basicPipe()) // prints 10

If you encounter any bugs, or have ideas for improvement, do not hesitate to add a task or a pull request.

Keywords

pipe

FAQs

Package last updated on 08 Oct 2023

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