Git Product home page Git Product logo

or-pipets's Introduction

||pipets

or-pipets logo

lint ava

Maintainability codecov

bundlephobia: minzip

XO code style code style: prettier versioning: or-release

Typed pipeable interface for imperative sequences in FP.

This package provides two constructs - Pipe and AsyncPipe. AsyncPipe provides the same methods as Pipe but its methods (except for .concat) end with P, e.g. .pipeP instead of .pipe. This is done to distinguish in the code when a Pipe transforms to an AsyncPipe.

  • Pipe.of(func) and AsyncPipe.of(asyncFunc) wrap a function into pipe or async pipe accordingly

  • Pipe.from(...funcs) and AsyncPipe.from(...asyncFuncs) wrap multiple functions

  • Pipe.empty() and AsyncPipe.empty() create empty pipes

  • pipe.pipe(func) extends the sequence with a function

  • .pipeTap(func) extends the sequence with a function. The function will be executed but the pipe will ignore its return value and preserve previous context instead

  • .pipeExtend(func) extends the sequence with a function. The function will be executed and the return value will be merged with the previous context. Should only be used with objects

  • a.concat(b) creates a new pipe that contains sequences of both pipes. If either a or b is an AsyncPipe, an AsyncPipe is returned, no matter what another pipe was.

Installation

yarn add or-pipets

or

npm i -S or-pipets

Examples

Bored

Suggests you ideas in case you get bored.

import { EventEmitter } from 'events'
// You also need node-fetch for this to work
import fetch, { Response } from 'node-fetch'
import { AsyncPipe } from 'or-pipets'

const emitter = new EventEmitter()

const getJson = async (response: Response) => response.json()

const boredPipe = AsyncPipe.of(() => 'https://www.boredapi.com/api/activity')
	.pipeTapP(() => console.log('Searching for activities... ๐Ÿค”'))
	.pipeP(fetch)
	.pipeP(getJson)
	.pipeP((json: { activity: string }) => json.activity)
	.pipeTapP(() => console.log('Found one ๐ŸŽ‰'))
	.pipeP((activity) => `  ๐Ÿ’ก ${activity}`)
	.pipeP(console.log)

emitter.on('bored', boredPipe.processP)

setTimeout(() => emitter.emit('bored'), 300)

Add 10

Adds 10 to the number you provide as a CLI argument.

import { Pipe } from 'or-pipets'

// Start with removing the first two strings in the argv
Pipe.of((argv: string[]) => argv.slice(2))
	// .pipeTap executes provided function but the return value is
	// ignored - the argument is passed to the next function instead
	// Log the context to console
	.pipeTap((x) => console.log('>>>', x))
	// Get the first item in the array and parse a number, 0 if NaN
	.pipe(([numberString]) => Number.parseInt(numberString, 10) || 0)
	// Log the context to console
	.pipeTap((x) => console.log('>>>', x))
	// Add 10
	.pipe((number) => number + 10)
	// Log the result to console
	.pipe((result) => console.log(`๐Ÿงฎ ${result}`))
	// Pass argv to the context of the Pipe
	.process(process.argv)

Welcome to localhost

Welcomes you to localhost:3000.

import * as express from 'express'
import { Pipe } from 'or-pipets'

const app = express()

interface ICtx {
	request: express.Request
	response: express.Response
}

// Here we start with Pipe.empty() because our first function returns
// void. If we pass it to Pipe.of, it will pass nothing to the first
// pipeExtend and the code will fail. This is because pipeExtend
// manages saving previous context, not the Pipe itself.
const getSlashPipeline = Pipe.empty<ICtx>()
	.pipeExtend(({ response }) => response.setHeader('X-Powered-By', 'Express with Pipe'))
	// Puts the value of the Host header to the context
	.pipeExtend(({ request }) => ({ host: request.header('host') ?? '๐Ÿค”' }))
	// Creates the response string with the host
	.pipeExtend(({ host }) => ({ responseBody: `Welcome to ${host}!` }))
	// Wraps the response string into h1
	.pipeExtend(({ responseBody }) => ({ responseBody: `<h1>${responseBody}</h1>` }))
	// Sends the response
	.pipe(({ response, responseBody }) => response.send(responseBody))

app.get('/', (request, response) => getSlashPipeline.process({ request, response }))

app.listen(3000, () => {
	console.log(`Example app listening on port 3000`)
})

or-pipets's People

Contributors

dependabot[bot] avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.