一个集合处理类库。
English | 中文
- 使用简单。
- 用来表示集合的实体与原生对象相近。
- 提供的方法数量少且常见。
npm install collection-query
import { take } from "collection-query/pull";
// 创建一个 pull 流
const s = function* () {
let count = 0;
while (true) {
yield count++;
}
};
// 将流转化成另一个流
const new_s = take(3)(s);
// 遍历 pull 流里的数据
for (const x of new_s()) {
console.log(x);
}
// 打印 0 1 2
Pull 和 push 启发自 rxjs.
他们是两种生产数据的方式。
pull
数据: 被动生产,主动消费push
数据: 主动生产,被动消费
collection-query
用以下四种流作为 pull 和 push 的实现。
"pull", "async-pull", "push", "async-push"
流 pull
and async-pull
基于 javascript 的 generator.
const pull_stream = function* () {
yield 0;
};
Pull 流以同步的方式生产数据.
const async_pull_stream = async function* () {
yield 0;
};
Async pull 流以异步的方式生产数据.
流 push
and async-push
基于 Emitter
const push_stream = create(function (emit) {
emit(EmitType.Next, 0);
emit(EmitType.Complete);
});
Push 流既能同步又能异步地生产数据。
const async_push_stream = create_async(function (emit) {
emit(EmitType.Next, 0);
emit(EmitType.Complete);
});
Async push 流以异步的方式生产数据.
虽然 collection-query
用不同的方法集去操作流,但是这些方法基本上拥有同样的名字,也有同样的语义。
import * as pull from "collection-query/pull";
import * as asyncPull from "collection-query/async-pull";
import * as push from "collection-query/push";
import * as asyncPush from "collection-query/async-push";
// 例如, 他们都有同样的“take”方法.