From 8a87cfd26de63e8c9ddaae0bea07123fc91134df Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Mon, 24 Jul 2017 21:00:50 +0200 Subject: [PATCH] [from2] introduce typings (#18353) --- types/from2/from2-tests.ts | 75 ++++++++++++++++++++++++++++++++++++++ types/from2/index.d.ts | 33 +++++++++++++++++ types/from2/tsconfig.json | 22 +++++++++++ types/from2/tslint.json | 1 + 4 files changed, 131 insertions(+) create mode 100644 types/from2/from2-tests.ts create mode 100644 types/from2/index.d.ts create mode 100644 types/from2/tsconfig.json create mode 100644 types/from2/tslint.json diff --git a/types/from2/from2-tests.ts b/types/from2/from2-tests.ts new file mode 100644 index 0000000000..8ad402c2ed --- /dev/null +++ b/types/from2/from2-tests.ts @@ -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; +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; +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]); diff --git a/types/from2/index.d.ts b/types/from2/index.d.ts new file mode 100644 index 0000000000..59872471b9 --- /dev/null +++ b/types/from2/index.d.ts @@ -0,0 +1,33 @@ +// Type definitions for from2 2.3 +// Project: https://github.com/hughsk/from2 +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// +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; + function ctor(opts: ObjectModeOptions): From2Ctor; + + type ObjectModeOptions = { objectMode: true } & stream.ReadableOptions; + type Options = { objectMode?: false } & stream.ReadableOptions; + + type From2Ctor = new(read: R) => NodeJS.ReadableStream; + + type ReadObjectInput = ReadCallback | any[]; + type ReadInput = ReadCallback | Chunk[]; + type ReadCallback = (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; +} diff --git a/types/from2/tsconfig.json b/types/from2/tsconfig.json new file mode 100644 index 0000000000..e632fe943f --- /dev/null +++ b/types/from2/tsconfig.json @@ -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" + ] +} diff --git a/types/from2/tslint.json b/types/from2/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/from2/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }