import { Comparator, Consumer, Decuple, Maybe, Nonuple, Octuple, Pair, Predicate, Quadruple, Quintuple, Septuple, Sextuple, Single, Triple, TypedBiFunction, TypedFunction, } from "andross"; import { AbstractStream } from "./AbstractStream"; import { IStream, ITry, ITryStream } from "./Interfaces"; import { chunk, chunkBy, concat, consume, cycle, filter, filterBy, flatMap, index, limit, map, promise, reverse, skip, sort, sortBy, tryMap, unique, uniqueBy, visit, zip, zipSame, } from "./Methods"; export class TypesafeStream extends AbstractStream { public ["constructor"]: (typeof TypesafeStream); public chunk(chunkSize: 0): IStream; public chunk(chunkSize: 1): IStream>; public chunk(chunkSize: 2): IStream | Single>; public chunk(chunkSize: 3): IStream | Pair | Single>; public chunk(chunkSize: 4): IStream | Triple | Pair | Single>; public chunk(chunkSize: 5): IStream | Quadruple | Triple | Pair | Single>; public chunk(chunkSize: 6): IStream | Quintuple | Quadruple | Triple | Pair | Single>; public chunk(chunkSize: 7): IStream | Sextuple | Quintuple | Quadruple | Triple | Pair | Single>; public chunk(chunkSize: 8): IStream | Septuple | Sextuple | Quintuple | Quadruple | Triple | Pair | Single>; public chunk(chunkSize: 9): IStream | Octuple | Septuple | Sextuple | Quintuple | Quadruple | Triple | Pair | Single>; public chunk(chunkSize: 10): IStream | Nonuple | Octuple | Septuple | Sextuple | Quintuple | Quadruple | Triple | Pair | Single>; public chunk(chunkSize: number): IStream; public chunk(chunkSize: number): IStream { this.check(); return new TypesafeStream(chunk(this.iterable, chunkSize)); } public chunkBy(classifier: TypedBiFunction): IStream { this.check(); return new TypesafeStream(chunkBy(this.iterable, classifier)); } public concat(...iterables: Iterable[]): this { this.check(); return new this.constructor(concat(this.iterable, ...iterables)) as this; } public consume(sink: T[] | Consumer, offset?: number, maxAmount?: number): this { this.check(); return new this.constructor(consume(this.iterable, sink, offset, maxAmount)) as this; } public cycle(count?: number): this { this.check(); return new this.constructor(cycle(this.iterable, count)) as this; } public flatMap(mapper: TypedFunction>): IStream { this.check(); return new TypesafeStream(flatMap(this.iterable, mapper)); } public filter(predicate: Predicate): this { this.check(); return new this.constructor(filter(this.iterable, predicate)) as this; } public filterBy(target: K, keyExtractor: TypedFunction, comparator?: Comparator): this { this.check(); return new this.constructor(filterBy(this.iterable, target, keyExtractor, comparator)) as this; } public index(): IStream<{index: number, value: T}> { this.check(); return new TypesafeStream(index(this.iterable)); } public limit(limitTo?: number): this { this.check(); return new this.constructor(limit(this.iterable, limitTo)) as this; } public map(mapper: TypedFunction): IStream { this.check(); return new TypesafeStream(map(this.iterable, mapper)); } public promise(promiseConverter: TypedFunction>): Promise> { this.check(); return promise(this.iterable, promiseConverter) .then(iterable => new TypesafeStream(iterable)); } public replace(mapper: TypedFunction): this { this.check(); return new this.constructor(map(this.iterable, mapper)) as this; } public reverse(): this { this.check(); return new this.constructor(reverse(this.iterable)) as this; } public skip(toSkip?: number): this { this.check(); return new this.constructor(skip(this.iterable, toSkip)) as this; } public sort(comparator?: Comparator): this { this.check(); return new this.constructor(sort(this.iterable, comparator)) as this; } public sortBy(keyExtractor: TypedFunction, comparator?: Comparator): this { this.check(); return new this.constructor(sortBy(this.iterable, keyExtractor, comparator)) as this; } public try(operation: TypedFunction): ITryStream { this.check(); return new TryStreamImpl(tryMap(this.iterable, operation)); } public unique(comparator?: Comparator): this { this.check(); return new this.constructor(unique(this.iterable, comparator)) as this; } public uniqueBy(keyExtractor: TypedFunction): this { this.check(); return new this.constructor(uniqueBy(this.iterable, keyExtractor)) as this; } public visit(consumer: Consumer): this { this.check(); return new this.constructor(visit(this.iterable, consumer)) as this; } public zip(other: Iterable): IStream<[Maybe, Maybe]> { this.check(); return new TypesafeStream(zip(this.iterable, other)); } public zipSame(...others: Iterable[]): IStream[]> { this.check(); return new TypesafeStream(zipSame(this.iterable, others)); } protected clone(iterable: Iterable): this { return new this.constructor(iterable) as this; } } class TryStreamImpl extends TypesafeStream> implements ITryStream { public forEachResult(success: Consumer, error?: Consumer): void { if (error === undefined) { error = console.error; } return this.forEach(x => x.ifPresent(success, error)); } public include(predicate: Predicate): this { return this.replace(x => x.include(predicate)); } public onError(handler: Consumer): this { return this.visit(x => x.ifAbsent(e => handler(e))); } public onSuccess(success: Consumer, failure: Consumer): this { return this.visit(x => x.ifPresent(success, failure)); } public orThrow(): IStream { return this.map(x => x.orThrow()); } public orElse(backup: T): IStream { return this.map(x => x.orElse(backup)); } public discardError(handler: Consumer = console.error): IStream { return this.onError(handler).filter(x => x.success).orThrow(); } public flatConvert(operation: TypedFunction>, backup?: TypedFunction>): ITryStream { this.check(); const x = map(this.iterable, x => x.flatConvert(operation, backup)); return new this.constructor(x) as object as ITryStream; } public convert(operation: TypedFunction, backup?: TypedFunction): ITryStream { this.check(); const x = map(this.iterable, x => x.convert(operation, backup)); return new this.constructor(x) as object as ITryStream; } public orTry(backup: TypedFunction): this { this.check(); const x = map(this.iterable, y => y.orTry(backup)); return new this.constructor(x) as object as this; } }