1.0.1 • Published 1 year ago

pipe-data v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

pipe-data

用管道的方式处理数据

安装:

npm

npm i pipe-data

yarn

yarn add pipe-data

使用:

引入

const Pipe = require('pipe-data')

创建一个管道

const pipe = new Pipe([data])
  • data 在创建时刻传入数据 可选

添加数据处理函数

pipe.use(async (data, context) => {})
  • data 传入的数据

  • context \ 上下文对象

    • params \ 为每个处理函数提供一个数据存放位置
    • setParams(key, value) \ 设置 params 参数

    • next([key, value]) \ 放行, 可选向下一个函数传递数据, 将会放置在 params 上

添加数据

不在创建管道时传入数据可使用该方法传入

pipe.setData(key, value)

运行管道

可选传递数据, 将会放置在 params 上

pipe.start([key, value])

示例

演示

const Pipe = require('pipe-data');
const pipe = new Pipe();

pipe.use(async (data, context) => {
    const {params, setParams, next} = context;
    console.log(1);
    next('func1', 10);
});

pipe.use(async (data, context) => {
    const {params, setParams, next} = context;
    console.log(2);
    next('func2', 20);
});

pipe.use(async (data, context) => {
    const {params, setParams, next} = context;
    console.log(3);
    next('func3', 30);
});

pipe.use(async (data, context) => {
    const {params, setParams, next} = context;
    console.log(4);
    console.log(context);
});

pipe.setData({
    name: 'xiaoMing',
    age: 20
});

pipe.start('func0', 0);
1.0.1

1 year ago

1.0.0

1 year ago