[from2] introduce typings (#18353)

This commit is contained in:
Dimitri Benin
2017-07-24 21:00:50 +02:00
committed by Andy
parent 73c39cf916
commit 8a87cfd26d
4 changed files with 131 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
import from = require('from2');
function fromString(str: string) {
return from((size, next) => {
if (str.length <= 0) {
return next(null, null);
}
const chunk = str.slice(0, size);
str = str.slice(size);
next(null, chunk);
});
}
fromString('hello world').pipe(process.stdout);
let stream: NodeJS.ReadableStream;
stream = from((size, next) => next(null, 'foo'));
stream = from((size, next) => next(null, new Buffer(1)));
stream = from((size, next) => next(null, new Uint8Array(1)));
stream = from((size, next) => next(null, null));
stream = from(['foo', new Buffer(1), new Uint8Array(1), null]);
// following should fail:
// stream = from((size, next) => next(null, {}));
stream = from({objectMode: false}, (size, next) => next(null, 'foo'));
stream = from({objectMode: false}, (size, next) => next(null, new Buffer(1)));
stream = from({objectMode: false}, (size, next) => next(null, new Uint8Array(1)));
stream = from({objectMode: false}, (size, next) => next(null, null));
stream = from({objectMode: false}, ['foo', new Buffer(1), new Uint8Array(1), null]);
// following should fail:
// stream = from({objectMode: false}, (size, next) => next(null, {}));
stream = from({highWaterMark: 1}, (size, next) => next(null, 'foo'));
stream = from({highWaterMark: 1}, (size, next) => next(null, new Buffer(1)));
stream = from({highWaterMark: 1}, (size, next) => next(null, new Uint8Array(1)));
stream = from({highWaterMark: 1}, (size, next) => next(null, null));
stream = from({highWaterMark: 1}, ['foo', new Buffer(1), new Uint8Array(1), null]);
// following should fail:
// stream = from({highWaterMark: 1}, (size, next) => next(null, {}));
stream = from({objectMode: true}, (size, next) => next(null, {}));
stream = from({objectMode: true}, (size, next) => next(null, null));
stream = from({objectMode: true}, [1, 'foo', {}, null]);
stream = from.obj((size, next) => next(null, {}));
stream = from.obj((size, next) => next(null, null));
stream = from.obj([1, 'foo', {}, null]);
stream = from.obj({objectMode: true}, (size, next) => next(null, {}));
stream = from.obj({objectMode: true}, (size, next) => next(null, null));
stream = from.obj({objectMode: true}, [1, 'foo', {}, null]);
// following should fail:
// stream = from.obj({objectMode: false}, [1, 'foo', {}, null]);
stream = from.obj({highWaterMark: 1}, (size, next) => next(null, {}));
stream = from.obj({highWaterMark: 1}, (size, next) => next(null, null));
stream = from.obj({highWaterMark: 1}, [1, 'foo', {}, null]);
let Ctor: from.From2Ctor<from.ReadInput>;
Ctor = from.ctor({objectMode: false});
Ctor = from.ctor();
stream = new Ctor((size, next) => next(null, 'foo'));
stream = new Ctor((size, next) => next(null, new Buffer(1)));
stream = new Ctor((size, next) => next(null, new Uint8Array(1)));
stream = new Ctor((size, next) => next(null, null));
stream = new Ctor(['foo', new Buffer(1), new Uint8Array(1), null]);
// following should fail:
// stream = new Ctor((size, next) => next(null, {}));
let Ctor2: from.From2Ctor<from.ReadObjectInput>;
Ctor2 = from.ctor({objectMode: true});
stream = new Ctor2((size, next) => next(null, {}));
stream = new Ctor2((size, next) => next(null, null));
stream = new Ctor2([1, 'foo', {}, null]);

33
types/from2/index.d.ts vendored Normal file
View File

@@ -0,0 +1,33 @@
// Type definitions for from2 2.3
// Project: https://github.com/hughsk/from2
// Definitions by: BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import * as stream from 'stream';
export = from2;
declare function from2(read: from2.ReadInput): NodeJS.ReadableStream;
declare function from2(opts: from2.ObjectModeOptions, read: from2.ReadObjectInput): NodeJS.ReadableStream;
declare function from2(opts: from2.Options, read: from2.ReadInput): NodeJS.ReadableStream;
declare namespace from2 {
function obj(read: ReadObjectInput): NodeJS.ReadableStream;
function obj(opts: { objectMode?: true } & stream.ReadableOptions, read: ReadObjectInput): NodeJS.ReadableStream;
function ctor(opts?: Options): From2Ctor<ReadInput>;
function ctor(opts: ObjectModeOptions): From2Ctor<ReadObjectInput>;
type ObjectModeOptions = { objectMode: true } & stream.ReadableOptions;
type Options = { objectMode?: false } & stream.ReadableOptions;
type From2Ctor<R extends ReadInput | ReadObjectInput> = new(read: R) => NodeJS.ReadableStream;
type ReadObjectInput = ReadCallback<NextObjectCallback> | any[];
type ReadInput = ReadCallback<NextCallback> | Chunk[];
type ReadCallback<N extends NextCallback | NextObjectCallback> = (size: number, next: N) => void;
type NextCallback = (err: any | undefined, chunk: Chunk) => void;
type NextObjectCallback = (err: any | undefined, chunk: any) => void;
type Chunk = string | Buffer | Uint8Array | null;
}

22
types/from2/tsconfig.json Normal file
View File

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

1
types/from2/tslint.json Normal file
View File

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