collection-query
v0.1.2
Published
A collection processing library.
Downloads
8
Maintainers
Readme
collection-query
A collection processing library.
English | 中文
Feature
- Simple to use.
- The entity which represent the collection is close to the native object.
- Methods are few and common.
Install
npm install collection-query
Usage
import { take } from "collection-query/pull";
// Create a pull stream
const s = function* () {
let count = 0;
while (true) {
yield count++;
}
};
// Convert the stream to another
const new_s = take(3)(s);
// Each the pull stream
for (const x of new_s()) {
console.log(x);
}
// Print 0 1 2
Pull and push
Pull and push are inspired by rxjs.
There are 2 ways to generate data.
pull
data: Produced passively, consumed activelypush
data: Produced actively, consumed passively
collection-query
provides 4 streams to implement the pull and push respectively.
"pull", "async-pull", "push", "async-push"
Pull
The streams pull
and async-pull
base on javascript generator.
const pull_stream = function* () {
yield 0;
};
Pull stream always generates data synchronously.
const async_pull_stream = async function* () {
yield 0;
};
Async pull stream always generates data asynchronously.
Push
The streams push
and async-push
base on Emitter
const push_stream = create(function (emit) {
emit(EmitType.Next, 0);
emit(EmitType.Complete);
});
Push stream can generate data synchronously or asynchronously.
const async_push_stream = create_async(function (emit) {
emit(EmitType.Next, 0);
emit(EmitType.Complete);
});
Async push stream always generates data asynchronously.
Method
collection-query
provides different set of methods to access the stream, but they almost always have the same name and the same semantics.
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";
// For example, they all have a "take" method.