Add README demo in samples, update README and update typings
This commit is contained in:
parent
8bb6016fbf
commit
976330134d
68
README.md
68
README.md
@ -18,21 +18,23 @@ const { sleep, once, delay, every, stream } = require("mhysa");
|
||||
async function main() {
|
||||
const collector = stream
|
||||
.concat(
|
||||
stream.fromArray(["a", "b", "c"]),
|
||||
stream.fromArray(["d", "e"])
|
||||
stream.fromArray(["a\n", "b\n", "c\n"]),
|
||||
stream.fromArray(["d", "e"]).pipe(stream.join("-"))
|
||||
)
|
||||
.pipe(stream.split("\n"))
|
||||
.pipe(stream.flatMap(s => [s, s.toUpperCase()]))
|
||||
.pipe(stream.collect({ objectMode: true }));
|
||||
|
||||
const collected = await once(collector, "data");
|
||||
await sleep(1000); // undefined (after one second)
|
||||
await delay(collected, 1000); // [ 'a', 'b', 'c', 'd', 'e' ] (after another second)
|
||||
await delay(collected, 1000); // [ 'a', 'A', 'b', 'B', 'c', 'C', 'd-e', 'D-E' ] (after another second)
|
||||
await every(
|
||||
[Promise.resolve("ab"), delay("cd", 1000)],
|
||||
c => c.length === 2
|
||||
s => s.length === 2
|
||||
); // true (after another second)
|
||||
await every(
|
||||
[Promise.resolve("ab"), delay("cd", 1000)],
|
||||
c => c.length === 1
|
||||
s => s.length === 1
|
||||
); // false (instantly)
|
||||
}
|
||||
main();
|
||||
@ -49,6 +51,56 @@ main();
|
||||
*/
|
||||
export declare function fromArray(array: any[]): NodeJS.ReadableStream;
|
||||
|
||||
/**
|
||||
* Return a ReadWrite stream that maps streamed chunks
|
||||
* @param mapper The mapper function, mapping each (chunk, encoding) to a new chunk (or a promise of such)
|
||||
* @param options
|
||||
* @param options.readableObjectMode Whether this stream should behave as a readable stream of objects
|
||||
* @param options.writableObjectMode Whether this stream should behave as a writable stream of objects
|
||||
*/
|
||||
export declare function map<T, R>(
|
||||
mapper: (chunk: T, encoding: string) => R,
|
||||
{
|
||||
readableObjectMode,
|
||||
writableObjectMode,
|
||||
}?: {
|
||||
readableObjectMode?: boolean | undefined;
|
||||
writableObjectMode?: boolean | undefined;
|
||||
},
|
||||
): NodeJS.ReadWriteStream;
|
||||
|
||||
/**
|
||||
* Return a ReadWrite stream that flat maps streamed chunks
|
||||
* @param mapper The mapper function, mapping each (chunk, encoding) to an array of new chunks (or a promise of such)
|
||||
* @param options
|
||||
* @param options.readableObjectMode Whether this stream should behave as a readable stream of objects
|
||||
* @param options.writableObjectMode Whether this stream should behave as a writable stream of objects
|
||||
*/
|
||||
export declare function flatMap<T, R>(
|
||||
mapper:
|
||||
| ((chunk: T, encoding: string) => R[])
|
||||
| ((chunk: T, encoding: string) => Promise<R[]>),
|
||||
{
|
||||
readableObjectMode,
|
||||
writableObjectMode,
|
||||
}?: {
|
||||
readableObjectMode?: boolean | undefined;
|
||||
writableObjectMode?: boolean | undefined;
|
||||
},
|
||||
): NodeJS.ReadWriteStream;
|
||||
|
||||
/**
|
||||
* Return a ReadWrite stream that splits streamed chunks using the given separator
|
||||
* @param separator The separator to split by, defaulting to "\n"
|
||||
*/
|
||||
export declare function split(separator?: string): NodeJS.ReadWriteStream;
|
||||
|
||||
/**
|
||||
* Return a ReadWrite stream that joins streamed chunks using the given separator
|
||||
* @param separator The separator to join with
|
||||
*/
|
||||
export declare function join(separator: string): NodeJS.ReadWriteStream;
|
||||
|
||||
/**
|
||||
* Return a ReadWrite stream that collects streamed objects or bytes into an array or buffer
|
||||
* @param options
|
||||
@ -99,9 +151,9 @@ export declare function once<T>(
|
||||
): Promise<T>;
|
||||
|
||||
/**
|
||||
* Resolve to false as soon as any of the promises has resolved to a value for which the predicate is
|
||||
* falsey, or resolve to true when all of the promises have resolved to a value for which the predicate is
|
||||
* thruthy, or rejects with the reason of the first promise rejection
|
||||
* Eagerly resolve to false as soon as any of the promises has resolved to a value for which the
|
||||
* predicate is falsey, or resolve to true when all of the promises have resolved to a value for which
|
||||
* the predicate is thruthy, or rejects with the reason of the first promise rejection
|
||||
*
|
||||
* @param promises Promises whose resolved values will be tested by the predicate
|
||||
* @param predicate Predicate to apply
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "mhysa",
|
||||
"version": "0.4.0",
|
||||
"version": "0.4.1",
|
||||
"description": "Promise, Stream and EventEmitter utils for Node.js",
|
||||
"keywords": [
|
||||
"promise",
|
||||
@ -35,7 +35,7 @@
|
||||
"@types/node": "^10.12.10",
|
||||
"ava": "^1.0.0-rc.2",
|
||||
"chai": "^4.2.0",
|
||||
"mhysa": "0.3.6",
|
||||
"mhysa": ".",
|
||||
"prettier": "^1.14.3",
|
||||
"ts-node": "^7.0.1",
|
||||
"tslint": "^5.11.0",
|
||||
|
25
samples/demo.js
Normal file
25
samples/demo.js
Normal file
@ -0,0 +1,25 @@
|
||||
const { sleep, once, delay, every, stream } = require("mhysa");
|
||||
|
||||
async function main() {
|
||||
const collector = stream
|
||||
.concat(
|
||||
stream.fromArray(["a\n", "b\n", "c\n"]),
|
||||
stream.fromArray(["d", "e"]).pipe(stream.join("-")),
|
||||
)
|
||||
.pipe(stream.split("\n"))
|
||||
.pipe(stream.flatMap(s => [s, s.toUpperCase()]))
|
||||
.pipe(stream.collect({ objectMode: true }));
|
||||
|
||||
const collected = await once(collector, "data");
|
||||
await sleep(1000); // undefined (after one second)
|
||||
console.log(await delay(collected, 1000)); // [ 'a', 'A', 'b', 'B', 'c', 'C', 'd-e', 'D-E' ] (after another second)
|
||||
await every(
|
||||
[Promise.resolve("ab"), delay("cd", 1000)],
|
||||
s => s.length === 2,
|
||||
); // true (after another second)
|
||||
await every(
|
||||
[Promise.resolve("ab"), delay("cd", 1000)],
|
||||
s => s.length === 1,
|
||||
); // false (instantly)
|
||||
}
|
||||
main();
|
@ -52,9 +52,6 @@ export function map<T, R>(
|
||||
});
|
||||
}
|
||||
|
||||
type FlatMapper<T, R> =
|
||||
| ((chunk: T, encoding: string) => R[])
|
||||
| ((chunk: T, encoding: string) => Promise<R[]>);
|
||||
/**
|
||||
* Return a ReadWrite stream that flat maps streamed chunks
|
||||
* @param mapper The mapper function, mapping each (chunk, encoding) to an array of new chunks (or a promise of such)
|
||||
@ -63,7 +60,9 @@ type FlatMapper<T, R> =
|
||||
* @param options.writableObjectMode Whether this stream should behave as a writable stream of objects
|
||||
*/
|
||||
export function flatMap<T, R>(
|
||||
mapper: FlatMapper<T, R>,
|
||||
mapper:
|
||||
| ((chunk: T, encoding: string) => R[])
|
||||
| ((chunk: T, encoding: string) => Promise<R[]>),
|
||||
{ readableObjectMode = true, writableObjectMode = true } = {},
|
||||
): NodeJS.ReadWriteStream {
|
||||
return new Transform({
|
||||
@ -93,12 +92,14 @@ export function flatMap<T, R>(
|
||||
* Return a ReadWrite stream that splits streamed chunks using the given separator
|
||||
* @param separator The separator to split by, defaulting to "\n"
|
||||
*/
|
||||
export function split(separator: string = "\n"): NodeJS.ReadWriteStream {
|
||||
export function split(
|
||||
separator: string | RegExp = "\n",
|
||||
): NodeJS.ReadWriteStream {
|
||||
let buffered: string = "";
|
||||
return new Transform({
|
||||
readableObjectMode: true,
|
||||
writableObjectMode: true,
|
||||
async transform(chunk, encoding, callback) {
|
||||
async transform(chunk: string, encoding, callback) {
|
||||
const splitted = chunk.split(separator);
|
||||
if (buffered.length > 0 && splitted.length > 1) {
|
||||
splitted[0] = buffered.concat(splitted[0]);
|
||||
|
@ -39,9 +39,9 @@ export function once<T>(
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve to false as soon as any of the promises has resolved to a value for which the predicate is
|
||||
* falsey, or resolve to true when all of the promises have resolved to a value for which the predicate is
|
||||
* thruthy, or rejects with the reason of the first promise rejection
|
||||
* Eagerly resolve to false as soon as any of the promises has resolved to a value for which the
|
||||
* predicate is falsey, or resolve to true when all of the promises have resolved to a value for which
|
||||
* the predicate is thruthy, or rejects with the reason of the first promise rejection
|
||||
*
|
||||
* @param promises Promises whose resolved values will be tested by the predicate
|
||||
* @param predicate Predicate to apply
|
||||
|
15
yarn.lock
15
yarn.lock
@ -330,7 +330,14 @@
|
||||
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.1.7.tgz#1b8e33b61a8c09cbe1f85133071baa0dbf9fa71a"
|
||||
integrity sha512-2Y8uPt0/jwjhQ6EiluT0XCri1Dbplr0ZxfFXUz+ye13gaqE8u5gL5ppao1JrUYr9cIip5S6MvQzBS7Kke7U9VA==
|
||||
|
||||
"@types/node@^10.12.10":
|
||||
"@types/event-stream@^3.3.34":
|
||||
version "3.3.34"
|
||||
resolved "https://registry.yarnpkg.com/@types/event-stream/-/event-stream-3.3.34.tgz#104bcedd5c61f90917b734bde04e61c6e64f03e1"
|
||||
integrity sha512-LLiivgWKii4JeMzFy3trrxqkRrVSdue8WmbXyHuSJLwNrhIQU5MTrc65jhxEPwMyh5HR1xevSdD+k2nnSRKw9g==
|
||||
dependencies:
|
||||
"@types/node" "*"
|
||||
|
||||
"@types/node@*", "@types/node@^10.12.10":
|
||||
version "10.12.10"
|
||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.10.tgz#4fa76e6598b7de3f0cb6ec3abacc4f59e5b3a2ce"
|
||||
integrity sha512-8xZEYckCbUVgK8Eg7lf5Iy4COKJ5uXlnIOnePN0WUwSQggy9tolM+tDJf7wMOnT/JT/W9xDYIaYggt3mRV2O5w==
|
||||
@ -2059,10 +2066,8 @@ meow@^5.0.0:
|
||||
trim-newlines "^2.0.0"
|
||||
yargs-parser "^10.0.0"
|
||||
|
||||
mhysa@0.3.6:
|
||||
version "0.3.6"
|
||||
resolved "https://registry.yarnpkg.com/mhysa/-/mhysa-0.3.6.tgz#c33885ca34c5797486ff9af10e33d17e2cd2b2a9"
|
||||
integrity sha512-X7AIhISZ/r5xOHaod3SwLCTXr11ttu/fDiKQPpsI0p/RsTTqweBR7hGJeRIF9Rt6XMW/UY21pJEf/ftUkfvkHg==
|
||||
mhysa@.:
|
||||
version "0.4.1"
|
||||
|
||||
micromatch@^3.1.10, micromatch@^3.1.4:
|
||||
version "3.1.10"
|
||||
|
Loading…
Reference in New Issue
Block a user