mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-15 09:37:52 +08:00
Added typings for stream-chain 2.0.
This commit is contained in:
32
types/stream-chain/index.d.ts
vendored
Normal file
32
types/stream-chain/index.d.ts
vendored
Normal 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;
|
||||
}
|
||||
158
types/stream-chain/stream-chain-tests.ts
Normal file
158
types/stream-chain/stream-chain-tests.ts
Normal 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));
|
||||
}
|
||||
15
types/stream-chain/tsconfig.json
Normal file
15
types/stream-chain/tsconfig.json
Normal 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"]
|
||||
}
|
||||
1
types/stream-chain/tslint.json
Normal file
1
types/stream-chain/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{"extends": "dtslint/dt.json"}
|
||||
Reference in New Issue
Block a user