diff --git a/types/stream-chain/index.d.ts b/types/stream-chain/index.d.ts new file mode 100644 index 0000000000..a95e21892f --- /dev/null +++ b/types/stream-chain/index.d.ts @@ -0,0 +1,32 @@ +// Type definitions for stream-chain 2.0 +// Project: https://github.com/uhop/stream-chain#readme +// Definitions by: Eugene Lazutkin +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// +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; +} diff --git a/types/stream-chain/stream-chain-tests.ts b/types/stream-chain/stream-chain-tests.ts new file mode 100644 index 0000000000..8447179b49 --- /dev/null +++ b/types/stream-chain/stream-chain-tests.ts @@ -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 { + 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)); +} diff --git a/types/stream-chain/tsconfig.json b/types/stream-chain/tsconfig.json new file mode 100644 index 0000000000..cee2462320 --- /dev/null +++ b/types/stream-chain/tsconfig.json @@ -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"] +} diff --git a/types/stream-chain/tslint.json b/types/stream-chain/tslint.json new file mode 100644 index 0000000000..4e88071852 --- /dev/null +++ b/types/stream-chain/tslint.json @@ -0,0 +1 @@ +{"extends": "dtslint/dt.json"}