npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

waterflow

v0.1.6-alpha.7

Published

A pipeline deal with data like water. support Map, Reduce, Async Pipes and Middlewares.

Downloads

13

Readme

Waterflow

是一个通过同步管道、异步管道、Map 管道、Reduce 管道、Map 异步管道,以数据流的形式处理逻辑的工具。

安装

npm install waterflow --save

用法

管道(Pipe)和管线(Pipeline)

管道通过一个对象定义,本质是其 handle 属性,handle 是一个函数,接受数据和一个可以中断整个 Pipeline 的函数。详见Pipe 定义 管线是由多个管道按顺序连接的数据处理流。

定义管线有两种方式:

  1. 一次性链式调用管线。使用import {pipeline} from 'waterflow'引用。
  2. 预定义的可重用管线。使用import Pipeline from 'waterflow'引用。
// 链式调用一次性管线
import {pipeline} from 'waterflow';
// 预定义可重用管线
import Pipeline from 'waterflow';

// 管线配置,可在 Middleware 的参数 `state` 通过 `state.settings`获得,详见 Middlewares 一节
let settings = {
    logging: false // LoggerMiddleware 使用的配置,设置为 false 可以不打印信息
}
let output = pipeline(10, {
    name: 'myPipeline',
    middlewares: [],
    settings
})
.flow((v, breakPipeline) => -v) // 调用 breakPipeine 后,后面的管道将不会被执行
.flow({name: 'Plus1', handle: v => ++v, middlewares: []})
.finish();

console.log(output);
// -9

// Pipeline 预定一条管线,通过其实例后的 flow() 方法处理不同的数据
let ppl = new Pipeline('myPipeline', [
    {handle: v => -v, type: 'flow'}, // type 属性默认是'flow'所以如果是`flow`类型的,不设置 type 属性
    {name: 'Plus1', handle: v => ++v, middlewares: []}
])

let output10 = ppl.flow(10, settings);
let output20 = ppl.flow(20, settings);

console.log(output10);
// -9

console.log(output20);
// -19

同步管道(SyncPipe)和异步管道(AsyncPipe)

同步管道,顾名思义就是 Pipe 是同步执行的。而异步管道的 Pipe 是同步执行的。

一次性调用管线的同步管道有pipeline.flowpipeline.flowMappipeline.flowReduce。异步管道有pipeline.flowAsyncpipeline.flowMapAsync

可重用管线的同步管道有flow(默认的),mapreduce。异步管道有asyncmapAsync。 通过初始化时指定 type 属性,如:

let ppl = new Pipeline('myPipeline', [
    {
        name: 'myPipe',
        handle: Function,
        type: 'undefined|''|null|map|reduce|async|mapAsync'
    }
]);

Pipe 定义

Pipe 的基本属性通过以下方式进行定义

{
    name: 'my-async-pipe',
    handle: Function,
    middlewares: [],
    ...otherProperties
}

特殊的 Pipe 需要额外的属性(otherProperties ),详见各个 Pipe 的定义。

可重用 Pipeline 的 type 属性 可重用 Pipeline 定义时需要在 Pipe 基本属性的基础上声明 type 属性。该属性的值有: undefinednull'''async''map''mapAsync''reduce'undefinednull'' 均使用默认类型'flow'。但是不要设置 type 属性为'flow'

一次性 Pipeline 的简写 如果使用一次性调用方式声明 Pipeline,可以只传递一个函数,而不是对象作为 Pipe

let output = pipeline(10)
    .flow(v => -v)
    .finish();

AsyncPipe(pipeline.flowAsync 或者 Pipelineasync 类型)

定义:

参数: data 输入的数据 breakPipeline 中断 Pipeline 执行的参数

以下均用pipeline方法写样例。你可以根据管道中查看如何转换成new Pipeline形式。

MapPipe(pipeline.flowMap 或者 Pipelinemap 类型)

MapAsyncPipe(pipeline.flowMapAsync 或者 PipelinemapAsync 类型)