wu: lint, expose WuIterable interface

This commit is contained in:
Andy Hanson
2018-05-24 10:25:52 -07:00
parent 0e7c267406
commit b7bbd0eb79
3 changed files with 68 additions and 74 deletions

123
types/wu/index.d.ts vendored
View File

@@ -3,71 +3,74 @@
// Definitions by: phiresky <https://github.com/phiresky>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace Wu {
declare function wu<T>(iterable: Iterable<T>): wu.WuIterable<T>;
export = wu;
export as namespace wu;
declare namespace wu {
type Consumer<T> = (t: T) => void;
type Filter<T> = (t: T) => boolean;
interface WuStatic {
<T>(iterable: Iterable<T>): WuIterable<T>;
// only static
chain<T>(...iters: Array<Iterable<T>>): WuIterable<T>;
count(start?: number, step?: number): WuIterable<number>;
curryable<T>(fun: (...x: any[]) => T, expected?: number): any;
entries<T>(obj: { [i: string]: T }): WuIterable<[string, T]>;
keys<T>(obj: { [i: string]: T }): WuIterable<string>;
values<T>(obj: { [i: string]: T }): WuIterable<T>;
repeat<T>(obj: T, times?: number): WuIterable<T>;
// also copied to WuInterface
asyncEach<T>(fn: Consumer<T>, maxBlock?: number, timeout?: number): void;
drop<T>(n: number, iter: Iterable<T>): WuIterable<T>;
dropWhile<T>(fn: Filter<T>, iter: Iterable<T>): WuIterable<T>;
cycle<T>(iter: Iterable<T>): Iterable<T>;
chunk<T>(n: number, iter: Iterable<T>): WuIterable<T[]>;
concatMap<T, U>(fn: (t: T) => Iterable<U>, iter: Iterable<T>): WuIterable<U>;
enumerate<T>(iter: Iterable<T>): Iterable<[number, T]>;
every<T>(fn: Filter<T>, iter: Iterable<T>): boolean;
filter<T>(fn: Filter<T>, iter: Iterable<T>): WuIterable<T>;
find<T>(fn: Filter<T>, iter: Iterable<T>): T;
flatten(iter: Iterable<any>): WuIterable<any>;
flatten(shallow: boolean, iter: Iterable<any>): WuIterable<any>;
forEach<T>(fn: Consumer<T>, iter: Iterable<T>): void;
has<T>(t: T, iter: Iterable<T>): boolean;
// invoke<T, U>(name:string, ...t:T[], iter: Iterable<(t:T)=>U>): WuIterable<U>;
invoke: any;
map<T, U>(fn: (t: T) => U, iter: Iterable<T>): WuIterable<U>;
// pluck<T>(attribute:string, iter: Iterable<{[attribute]: T}>): WuIterable<T>;
pluck(attribute: string, iter: Iterable<any>): WuIterable<any>;
reduce<T>(fn: (a: T, b: T) => T, iter: Iterable<T>): T;
reduce<T>(fn: (a: T, b: T) => T, initial: T, iter: Iterable<T>): T;
reduce<T, U>(fn: (a: U, b: T) => U, iter: Iterable<T>): U;
reduce<T, U>(fn: (a: U, b: T) => U, initial: U, iter: Iterable<T>): U;
reductions<T>(fn: (a: T, b: T) => T, iter: Iterable<T>): WuIterable<T>;
reductions<T>(fn: (a: T, b: T) => T, initial: T, iter: Iterable<T>): WuIterable<T>;
reductions<T, U>(fn: (a: U, b: T) => U, iter: Iterable<T>): WuIterable<U>;
reductions<T, U>(fn: (a: U, b: T) => U, initial: U, iter: Iterable<T>): WuIterable<U>;
reject<T>(fn: Filter<T>, iter: Iterable<T>): WuIterable<T>;
slice<T>(iter: Iterable<T>): WuIterable<T>;
slice<T>(start: number, iter: Iterable<T>): WuIterable<T>;
slice<T>(start: number, stop: number, iter: Iterable<T>): WuIterable<T>;
some<T>(fn: Filter<T>, iter: Iterable<T>): boolean;
spreadMap<T>(fn: (...x: any[]) => T, iter: Iterable<any[]>): WuIterable<T>;
take<T>(n: number, iter: Iterable<T>): WuIterable<T>;
takeWhile<T>(fn: Filter<T>, iter: Iterable<T>): WuIterable<T>;
tap<T>(fn: Consumer<T>, iter: Iterable<T>): WuIterable<T>;
unique<T>(iter: Iterable<T>): WuIterable<T>;
zip<T, U>(iter2: Iterable<T>, iter: Iterable<U>): WuIterable<[T, U]>;
zipLongest<T, U>(iter2: Iterable<T>, iter: Iterable<U>): WuIterable<[T, U]>;
zipWith: any;
unzip: any;
tee<T>(iter: Iterable<T>): Array<WuIterable<T>>;
tee<T>(n: number, iter: Iterable<T>): Array<WuIterable<T>>;
}
// only static
function chain<T>(...iters: Array<Iterable<T>>): WuIterable<T>;
function count(start?: number, step?: number): WuIterable<number>;
function curryable(fun: (...x: any[]) => any, expected?: number): any;
function entries<T>(obj: { [i: string]: T }): WuIterable<[string, T]>;
function keys(obj: { [i: string]: any }): WuIterable<string>;
function values<T>(obj: { [i: string]: T }): WuIterable<T>;
function repeat<T>(obj: T, times?: number): WuIterable<T>;
// also copied to WuIterable
function asyncEach(fn: Consumer<any>, maxBlock?: number, timeout?: number): void;
function drop<T>(n: number, iter: Iterable<T>): WuIterable<T>;
function dropWhile<T>(fn: Filter<T>, iter: Iterable<T>): WuIterable<T>;
function cycle<T>(iter: Iterable<T>): Iterable<T>;
function chunk<T>(n: number, iter: Iterable<T>): WuIterable<T[]>;
function concatMap<T, U>(fn: (t: T) => Iterable<U>, iter: Iterable<T>): WuIterable<U>;
function enumerate<T>(iter: Iterable<T>): Iterable<[number, T]>;
function every<T>(fn: Filter<T>, iter: Iterable<T>): boolean;
function filter<T>(fn: Filter<T>, iter: Iterable<T>): WuIterable<T>;
function find<T>(fn: Filter<T>, iter: Iterable<T>): T;
function flatten(iter: Iterable<any>): WuIterable<any>;
function flatten(shallow: boolean, iter: Iterable<any>): WuIterable<any>;
function forEach<T>(fn: Consumer<T>, iter: Iterable<T>): void;
function has<T>(t: T, iter: Iterable<T>): boolean;
// invoke<T, U>(name:string, ...t:T[], iter: Iterable<(t:T)=>U>): WuIterable<U>;
const invoke: any;
function map<T, U>(fn: (t: T) => U, iter: Iterable<T>): WuIterable<U>;
// pluck<T>(attribute:string, iter: Iterable<{[attribute]: T}>): WuIterable<T>;
function pluck(attribute: string, iter: Iterable<any>): WuIterable<any>;
function reduce<T>(fn: (a: T, b: T) => T, iter: Iterable<T>): T;
function reduce<T>(fn: (a: T, b: T) => T, initial: T, iter: Iterable<T>): T;
function reduce<T, U>(fn: (a: U, b: T) => U, iter: Iterable<T>): U;
function reduce<T, U>(fn: (a: U, b: T) => U, initial: U, iter: Iterable<T>): U;
function reductions<T>(fn: (a: T, b: T) => T, iter: Iterable<T>): WuIterable<T>;
function reductions<T>(fn: (a: T, b: T) => T, initial: T, iter: Iterable<T>): WuIterable<T>;
function reductions<T, U>(fn: (a: U, b: T) => U, iter: Iterable<T>): WuIterable<U>;
function reductions<T, U>(fn: (a: U, b: T) => U, initial: U, iter: Iterable<T>): WuIterable<U>;
function reject<T>(fn: Filter<T>, iter: Iterable<T>): WuIterable<T>;
function slice<T>(iter: Iterable<T>): WuIterable<T>;
function slice<T>(start: number, iter: Iterable<T>): WuIterable<T>;
function slice<T>(start: number, stop: number, iter: Iterable<T>): WuIterable<T>;
function some<T>(fn: Filter<T>, iter: Iterable<T>): boolean;
function spreadMap<T>(fn: (...x: any[]) => T, iter: Iterable<any[]>): WuIterable<T>;
function take<T>(n: number, iter: Iterable<T>): WuIterable<T>;
function takeWhile<T>(fn: Filter<T>, iter: Iterable<T>): WuIterable<T>;
function tap<T>(fn: Consumer<T>, iter: Iterable<T>): WuIterable<T>;
function unique<T>(iter: Iterable<T>): WuIterable<T>;
function zip<T, U>(iter2: Iterable<T>, iter: Iterable<U>): WuIterable<[T, U]>;
function zipLongest<T, U>(iter2: Iterable<T>, iter: Iterable<U>): WuIterable<[T, U]>;
const zipWith: any;
const unzip: any;
function tee<T>(iter: Iterable<T>): Array<WuIterable<T>>;
function tee<T>(n: number, iter: Iterable<T>): Array<WuIterable<T>>;
interface WuIterable<T> extends IterableIterator<T> {
// generated from section "copied to WuIterable" above via
// sed -r 's/(, )?iter: Iterable<\w+>//' |
// sed -r 's/^(\s+\w+)<T>/\1/' |
// sed -r 's/^(\s+\w+)<T, /\1</'
asyncEach<T>(fn: Consumer<T>, maxBlock?: number, timeout?: number): any;
asyncEach(fn: Consumer<any>, maxBlock?: number, timeout?: number): any;
drop(n: number): WuIterable<T>;
dropWhile(fn: Filter<T>): WuIterable<T>;
cycle(): Iterable<T>;
@@ -97,13 +100,13 @@ declare namespace Wu {
takeWhile(fn: Filter<T>): WuIterable<T>;
tap(fn: Consumer<T>): WuIterable<T>;
unique(): WuIterable<T>;
// TODO: this makes no sense, where did the second entry come from?
// tslint:disable-next-line no-unnecessary-generics
zip<U>(iter2: Iterable<T>): WuIterable<[T, U]>;
// tslint:disable-next-line no-unnecessary-generics
zipLongest<U>(iter2: Iterable<T>): WuIterable<[T, U]>;
zipWith: any;
unzip: any;
tee(n?: number): Array<WuIterable<T>>;
}
}
declare const wu: Wu.WuStatic;
export = wu;
export as namespace wu;

View File

@@ -1,8 +1 @@
{
"extends": "dtslint/dt.json",
"rules": {
// TODO
"no-void-expression": false,
"no-unnecessary-generics": false
}
}
{ "extends": "dtslint/dt.json" }

View File

@@ -222,15 +222,13 @@ describe("wu.has", () => {
});
describe("wu.invoke", () => {
it("should yield the method invokation on each item", () => {
function Greeter(name: string) {
this.name = name;
class Greeter {
constructor(readonly name: string) {}
greet(tail: string) { return `hello ${this.name}${tail}`; }
}
Greeter.prototype.greet = function(tail: string) {
return `hello ${this.name}${tail}`;
};
assert.eqArray(["hello world!", "hello test!"],
wu.invoke("greet", "!",
[Greeter("world"), Greeter("test")]));
[new Greeter("world"), new Greeter("test")]));
});
});
describe("wu.keys", () => {