https://github.com/iplaylf2/collection-query
A collection processing library.
https://github.com/iplaylf2/collection-query
collection query stream typescript
Last synced: 2 months ago
JSON representation
A collection processing library.
- Host: GitHub
- URL: https://github.com/iplaylf2/collection-query
- Owner: iplaylf2
- License: bsd-3-clause
- Created: 2020-07-11T14:14:23.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-02-19T09:55:18.000Z (over 4 years ago)
- Last Synced: 2025-03-05T22:09:03.587Z (3 months ago)
- Topics: collection, query, stream, typescript
- Language: TypeScript
- Homepage:
- Size: 166 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# collection-query
A collection processing library.
English | [中文](https://github.com/Iplaylf2/collection-query/blob/master/doc/README.cn.md)
-## Feature
- Simple to use.
- The entity which represent the collection is close to the native object.
- Methods are few and common.## Install
``` bash
npm install collection-query
```## Usage
``` typescript
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 actively
- `push` 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.
``` typescript
const pull_stream = function* () {
yield 0;
};
```Pull stream always generates data synchronously.
``` typescript
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`
``` typescript
const push_stream = create(function (emit) {
emit(EmitType.Next, 0);
emit(EmitType.Complete);
});
```Push stream can generate data synchronously or asynchronously.
``` typescript
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.
``` typescript
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.
```[Document](https://github.com/Iplaylf2/collection-query/blob/master/doc/document.md)