Added typings for stream-chain 2.0.

This commit is contained in:
Eugene Lazutkin
2018-06-25 19:38:48 -05:00
parent 8cfe3b2c5a
commit e93ca5046d
4 changed files with 206 additions and 0 deletions

32
types/stream-chain/index.d.ts vendored Normal file
View File

@@ -0,0 +1,32 @@
// Type definitions for stream-chain 2.0
// Project: https://github.com/uhop/stream-chain#readme
// Definitions by: Eugene Lazutkin <https://github.com/uhop>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import { Readable, Writable, Duplex, Transform, DuplexOptions } from 'stream';
export = Chain;
interface ITransformFunction {
(chunk: any, encoding?: string): any;
}
declare type Stream = Readable | Writable | Duplex | Transform;
declare type StreamItem = Stream | ITransformFunction;
declare class Chain extends Duplex {
constructor(fns: StreamItem[], options?: Chain.ChainOptions);
input: Stream;
output: Stream;
streams: Stream[];
}
declare namespace Chain {
interface ChainOptions extends DuplexOptions {
skipEvents?: boolean;
}
function chain(fns: StreamItem[], options?: ChainOptions): Chain;
}

View File

@@ -0,0 +1,158 @@
import * as Chain from 'stream-chain';
import { Readable, Writable, Transform } from 'stream';
const streamFromArray = (array: number[]): Readable => {
let index = 0;
return new Readable({
objectMode: true,
read(): void {
this.push(index < array.length ? array[index++] : null);
}
});
};
const streamToArray = (array: number[]): Writable =>
new Writable({
objectMode: true,
write(chunk, encoding, callback) {
array.push(chunk);
callback(null);
}
});
{
// generic function
const chain = new Chain([(x: number) => 2 * x]);
const out1: number[] = [];
const out2: number[] = [];
streamFromArray([1, 2, 3])
.pipe(chain)
.pipe(streamToArray(out1));
chain.on('data', value => out2.push(+value));
}
{
// generator
const chain = new Chain([
function*(x: number): IterableIterator<number> {
const n = +x;
yield 2 * n - 1;
return 2 * n;
}
]);
const out1: number[] = [];
const out2: number[] = [];
streamFromArray([1, 2, 3])
.pipe(chain)
.pipe(streamToArray(out1));
chain.on('data', value => out2.push(+value));
}
{
// async function
const chain = new Chain([async (x: number) => Promise.resolve(x)]);
const out1: number[] = [];
const out2: number[] = [];
streamFromArray([1, 2, 3])
.pipe(chain)
.pipe(streamToArray(out1));
chain.on('data', value => out2.push(+value));
}
{
// function that returns an array of values
const chain = new Chain([(x: number) => [x, x + 1]]);
const out1: number[] = [];
const out2: number[] = [];
streamFromArray([1, 2, 3])
.pipe(chain)
.pipe(streamToArray(out1));
chain.on('data', value => out2.push(+value));
}
{
// chain of functions
const chain = new Chain([(x: number) => x * x, (x: number) => 2 * x + 1]);
const out1: number[] = [];
const out2: number[] = [];
streamFromArray([1, 2, 3])
.pipe(chain)
.pipe(streamToArray(out1));
chain.on('data', value => out2.push(+value));
}
{
// Transform + a function
const chain = new Chain([
new Transform({
objectMode: true,
transform(x: number, _, callback) {
callback(undefined, x * x);
}
}),
(x: number) => 2 * x + 1
]);
const out1: number[] = [];
const out2: number[] = [];
streamFromArray([1, 2, 3])
.pipe(chain)
.pipe(streamToArray(out1));
chain.on('data', value => out2.push(+value));
}
{
// factory
const chain = Chain.chain([(x: number) => 2 * x]);
const out1: number[] = [];
const out2: number[] = [];
streamFromArray([1, 2, 3])
.pipe(chain)
.pipe(streamToArray(out1));
chain.on('data', value => out2.push(+value));
}
{
// include Readable
const chain = new Chain([streamFromArray([1, 2, 3]), (x: number) => 2 * x]);
const out1: number[] = [];
const out2: number[] = [];
chain.pipe(streamToArray(out1));
chain.on('data', value => out2.push(+value));
}
{
// include Writable
const out1: number[] = [];
const chain = new Chain([(x: number) => 2 * x], streamToArray(out1));
const out2: number[] = [];
streamFromArray([1, 2, 3]).pipe(chain);
chain.on('data', value => out2.push(+value));
}
{
// include Readable and Writable
const out1: number[] = [];
const chain = new Chain([streamFromArray([1, 2, 3]), (x: number) => 2 * x], streamToArray(out1));
const out2: number[] = [];
chain.on('data', value => out2.push(+value));
}

View File

@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": ["index.d.ts", "stream-chain-tests.ts"]
}

View File

@@ -0,0 +1 @@
{"extends": "dtslint/dt.json"}