Merge pull request #21263 from rubensworks/feature/asynciterator

Add asynciterator typings
This commit is contained in:
Daniel Rosenwasser
2017-11-06 11:05:51 -08:00
committed by GitHub
4 changed files with 323 additions and 0 deletions
+135
View File
@@ -0,0 +1,135 @@
import { ArrayIterator, AsyncIterator, BufferedIterator, ClonedIterator, EmptyIterator, IntegerIterator,
MultiTransformIterator, SingletonIterator, SimpleTransformIterator, TransformIterator } from "asynciterator";
function test_asynciterator() {
// We can't instantiate an abstract class.
const it1: AsyncIterator<number> = <any> {};
const read1: number = it1.read();
it1.each((data: number) => console.log(data));
it1.each((data: number) => console.log(data), {});
it1.close();
const it2: AsyncIterator<string> = <any> {};
const read2: string = it2.read();
it2.each((data: string) => console.log(data));
it2.each((data: string) => console.log(data), {});
it2.close();
const it3: AsyncIterator<AsyncIterator<string>> = <any> {};
const read3: AsyncIterator<string> = it3.read();
it3.each((data: AsyncIterator<string>) => data.each((data: string) => console.log(data)));
it3.each((data: AsyncIterator<string>) => data.each((data: string) => console.log(data), {}), {});
it3.close();
const readable2: boolean = it1.readable;
const closed2: boolean = it1.closed;
const ended2: boolean = it1.ended;
it1.setProperty('name1', 123);
it2.setProperty('name2', 'someValue');
const p1: number = it1.getProperty('name1');
const p2: string = it1.getProperty('name2');
it1.getProperty('name1', (value: number) => console.log(value));
it1.getProperty('name2', (value: string) => console.log(value));
const ps1: {[id: string]: any} = it1.getProperties();
it1.setProperties({ name1: 1234, name2: 'someOtherValue' });
it1.copyProperties(it2, [ 'name1', 'name2' ]);
const str: string = it1.toString();
const stit1: SimpleTransformIterator<number, string> = it1.transform();
const stit2: SimpleTransformIterator<number, string> = it1.map((number: number) => 'i' + number);
const stit3: AsyncIterator<string> = it1.map((number: number) => 'i' + number);
const stit4: AsyncIterator<number> = it2.map(parseInt);
const stit5: AsyncIterator<number> = it1.map((number: number) => number + 1);
const stit6: AsyncIterator<number> = it1.filter((number: number) => number < 10);
const stit7: AsyncIterator<number> = it1.prepend([0, 1, 2]);
const stit8: AsyncIterator<number> = it1.append([0, 1, 2]);
const stit9: AsyncIterator<number> = it1.surround([0, 1, 2], [0, 1, 2]);
const stit10: AsyncIterator<number> = it1.skip(2);
const stit11: AsyncIterator<number> = it1.take(2);
const stit12: AsyncIterator<number> = it1.range(2, 20);
const stit13: AsyncIterator<number> = it1.clone();
const intit1: IntegerIterator = AsyncIterator.range(10, 100, 1);
const intit2: IntegerIterator = AsyncIterator.range(10, 100);
const intit3: IntegerIterator = AsyncIterator.range(10);
const intit4: IntegerIterator = AsyncIterator.range();
}
function test_emptyiterator() {
const it1: AsyncIterator<number> = new EmptyIterator();
const it2: AsyncIterator<string> = new EmptyIterator();
}
function test_singletoniterator() {
const it1: AsyncIterator<number> = new SingletonIterator(3);
const it2: AsyncIterator<string> = new SingletonIterator('a');
}
function test_arrayiterator() {
const it1: AsyncIterator<number> = new ArrayIterator([1, 2, 3]);
const it2: AsyncIterator<string> = new ArrayIterator(['a', 'b', 'c']);
}
function test_integeriterator() {
const it1: IntegerIterator = new IntegerIterator();
const it2: AsyncIterator<number> = new IntegerIterator({});
const it3: AsyncIterator<number> = new IntegerIterator({ start: 0 });
const it4: AsyncIterator<number> = new IntegerIterator({ end: 100 });
const it5: AsyncIterator<number> = new IntegerIterator({ step: 10 });
}
function test_bufferediterator() {
const it1: BufferedIterator<number> = new BufferedIterator();
const it2: AsyncIterator<number> = new BufferedIterator({});
const it3: AsyncIterator<number> = new BufferedIterator({ maxBufferSize: 10 });
const it4: AsyncIterator<number> = new BufferedIterator({ autoStart: true });
}
function test_transformiterator() {
const it1: TransformIterator<number, string> = new TransformIterator<number, string>();
const it2: AsyncIterator<string> = new TransformIterator<number, string>();
const it3: AsyncIterator<number> = new TransformIterator<string, number>(it1);
const it4: AsyncIterator<number> = new TransformIterator<string, number>(it1, {});
const it5: AsyncIterator<number> = new TransformIterator<string, number>(it1, { optional: true });
const it6: AsyncIterator<number> = new TransformIterator<string, number>({ source: it1 });
const source: AsyncIterator<number> = it1.source;
}
function test_simpletransformiterator() {
const it1: SimpleTransformIterator<number, string> = new SimpleTransformIterator<number, string>();
const it2: TransformIterator<number, string> = new SimpleTransformIterator<number, string>();
const it3: AsyncIterator<string> = new SimpleTransformIterator<number, string>();
const it4: AsyncIterator<number> = new SimpleTransformIterator<string, number>(it1);
const it5: AsyncIterator<number> = new SimpleTransformIterator<string, number>(it1, {});
const it6: AsyncIterator<number> = new SimpleTransformIterator<string, number>({});
const it7: AsyncIterator<number> = new SimpleTransformIterator<string, number>({ optional: true });
const it8: AsyncIterator<number> = new SimpleTransformIterator<string, number>({ source: it1 });
const it9: AsyncIterator<number> = new SimpleTransformIterator<string, number>({ offset: 2 });
const it10: AsyncIterator<number> = new SimpleTransformIterator<string, number>({ limit: 2 });
const it11: AsyncIterator<number> = new SimpleTransformIterator<string, number>({ prepend: [0, 1, 2] });
const it12: AsyncIterator<number> = new SimpleTransformIterator<string, number>({ append: [0, 1, 2] });
const it13: AsyncIterator<number> = new SimpleTransformIterator<number, number>(
{ filter: (val: number) => val > 10 });
const it14: AsyncIterator<number> = new SimpleTransformIterator<number, number>({ map: (val: number) => val + 1 });
const it15: AsyncIterator<number> = new SimpleTransformIterator<number, number>(
{ transform: (val: number, cb: (result: number) => void) => cb(val + 1) });
}
function test_multitransformiterator() {
const it1: MultiTransformIterator<number, string> = new MultiTransformIterator<number, string>();
const it2: TransformIterator<number, string> = new MultiTransformIterator<number, string>();
const it3: AsyncIterator<string> = new MultiTransformIterator<number, string>();
const it4: AsyncIterator<number> = new MultiTransformIterator<string, number>(it1);
const it5: AsyncIterator<number> = new MultiTransformIterator<string, number>(it1, {});
const it6: AsyncIterator<number> = new MultiTransformIterator<string, number>({});
const it7: AsyncIterator<number> = new MultiTransformIterator<string, number>({ optional: true });
const it8: AsyncIterator<number> = new MultiTransformIterator<string, number>({ source: it1 });
}
function test_clonediterator() {
const it1: ClonedIterator<number> = new ClonedIterator<number>();
const it2: ClonedIterator<number> = new ClonedIterator<number>(it1);
}
+164
View File
@@ -0,0 +1,164 @@
// Type definitions for asynciterator 1.1
// Project: https://github.com/rubenverborgh/AsyncIterator#readme
// Definitions by: Ruben Taelman <https://github.com/rubensworks>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
/// <reference types="node" />
import { EventEmitter } from "events";
export abstract class AsyncIterator<T> extends NodeJS.EventEmitter {
protected static STATES: ['INIT', 'OPEN', 'CLOSING', 'CLOSED', 'ENDED'];
protected static INIT: 0;
protected static OPEN: 1;
protected static CLOSING: 2;
protected static CLOSED: 3;
protected static ENDED: 4;
protected _state: number;
protected _readable: boolean;
protected _destination?: AsyncIterator<any>;
readable: boolean;
closed: boolean;
ended: boolean;
constructor();
read(): T;
each(callback: (data: T) => void, self?: any): void;
close(): void;
protected _changeState(newState: number, eventAsync?: boolean): void;
private _hasListeners(eventName: string | symbol): boolean;
// tslint:disable-next-line ban-types
private _addSingleListener(eventName: string | symbol, listener: Function): void;
protected _end(): void;
getProperty(propertyName: string, callback?: (value: any) => void): any;
setProperty(propertyName: string, value: any): void;
getProperties(): {[id: string]: any};
setProperties(properties: {[id: string]: any}): void;
copyProperties(source: AsyncIterator<any>, propertyNames: string[]): void;
toString(): string;
protected _toStringDetails(): string;
transform<T2>(options?: SimpleTransformIteratorOptions<T, T2>): SimpleTransformIterator<T, T2>;
map<T2>(mapper: (item: T) => T2, self?: object): SimpleTransformIterator<T, T2>;
filter(filter: (item: T) => boolean, self?: object): SimpleTransformIterator<T, T>;
prepend(items: T[]): SimpleTransformIterator<T, T>;
append<T>(items: T[]): SimpleTransformIterator<T, T>;
surround<T>(prepend: T[], append: T[]): SimpleTransformIterator<T, T>;
skip<T>(offset: number): SimpleTransformIterator<T, T>;
take<T>(limit: number): SimpleTransformIterator<T, T>;
range<T>(start: number, end: number): SimpleTransformIterator<T, T>;
clone(): ClonedIterator<T>;
static range(start?: number, end?: number, step?: number): IntegerIterator;
}
export class EmptyIterator<T> extends AsyncIterator<T> {
_state: 4;
}
export class SingletonIterator<T> extends AsyncIterator<T> {
constructor(item?: T);
}
export class ArrayIterator<T> extends AsyncIterator<T> {
constructor(items?: T[]);
}
export interface IntegerIteratorOptions {
step?: number;
end?: number;
start?: number;
}
export class IntegerIterator extends AsyncIterator<number> {
protected _step: number;
protected _last: number;
protected _next: number;
constructor(options?: IntegerIteratorOptions);
}
export interface BufferedIteratorOptions {
maxBufferSize?: number;
autoStart?: boolean;
}
export class BufferedIterator<T> extends AsyncIterator<T> {
maxBufferSize: number;
protected _pushedCount: number;
protected _buffer: T[];
protected _init(autoStart: boolean): void;
protected _begin(done: () => void): void;
protected _read(count: number, done: () => void): void;
protected _push(item: T): void;
protected _fillBuffer(): void;
protected _completeClose(): void;
protected _flush(done: () => void): void;
constructor(options?: BufferedIteratorOptions);
}
export interface TransformIteratorOptions<S> extends BufferedIteratorOptions {
optional?: boolean;
source?: AsyncIterator<S>;
}
export class TransformIterator<S, T> extends BufferedIterator<T> {
protected _optional: boolean;
source: AsyncIterator<S>;
protected _validateSource(source: AsyncIterator<S>, allowDestination?: boolean): void;
protected _transform(item: S, done: (result: T) => void): void;
protected _closeWhenDone(): void;
constructor(source?: AsyncIterator<S> | TransformIteratorOptions<S>, options?: TransformIteratorOptions<S>);
}
export interface SimpleTransformIteratorOptions<S, T> extends TransformIteratorOptions<S> {
offset?: number;
limit?: number;
prepend?: T[];
append?: T[];
filter?(item: S): boolean;
map?(item: S): T;
transform?(item: S, callback: (result: T) => void): void;
}
export class SimpleTransformIterator<S, T> extends TransformIterator<S, T> {
protected _offset: number;
protected _limit: number;
protected _prepender?: ArrayIterator<T>;
protected _appender?: ArrayIterator<T>;
protected _filter?(item: S): boolean;
protected _map?(item: S): T;
protected _transform(item: S, done: (result: T) => void): void;
protected _insert(inserter: AsyncIterator<S>, done: () => void): void;
constructor(source?: AsyncIterator<S> | SimpleTransformIteratorOptions<S, T>,
options?: SimpleTransformIteratorOptions<S, T>);
}
export class MultiTransformIterator<S, T> extends TransformIterator<S, T> {
_transformerQueue: S[];
protected _createTransformer(): AsyncIterator<S>;
constructor(source?: AsyncIterator<S> | TransformIteratorOptions<S>, options?: TransformIteratorOptions<S>);
}
export class ClonedIterator<T> extends TransformIterator<T, T> {
_readPosition: number;
constructor(source?: AsyncIterator<T>);
}
+23
View File
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"asynciterator-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }