Skip to main content
Deno 2 is finally here 🎉️
Learn more

Deno-pipe

Simple generic pipeline mixin

Examples

Basic

import {
    Pipe, Use
} from 'mod.ts'

// middleware factory
function createInc(i: number): Use<number> {
    return (x, next) => next(x + i)
}

// create executable
const execute = Pipe.create<number>(x => {
    return x ** 2
}, [ 
    1, 
    2, 
    3, 
    4, 
    5,
].map(createInc))

console.log(execute(0))

// console output:
// ---------------
// 225

Chaining

import {
    Pipe, Use
} from 'mod.ts'

// middleware factory
function createUse(i: number): Use<number, void> {
    return (x, next) => (console.log(i), next(x))
}

// special middleware
const spec: Use<number, void> = (x, next) => {

    // if x is even -> chain
    if (x % 2 == 0) {

        // chaining
        next = Pipe.create( next ,
            createUse(1),
            createUse(2),
            createUse(3),
        )

    }

    return next(x)

}

// create process function
const process = Pipe.create<number, void>(() => void 0 , createUse(0) , spec , createUse(4))

process(1)

// console output:
// ---------------
// 0
// 4

process(2)

// console output:
// ---------------
// 0
// 1
// 2
// 3
// 4