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 InplaceStream extends AbstractStream { public ["constructor"]: (typeof InplaceStream); 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.iterable = chunk(this.iterable, chunkSize); return this; } public chunkBy(classifier: TypedBiFunction): IStream { this.iterable = chunkBy(this.iterable, classifier); return this; } public concat(...iterables: Iterable[]): this { this.iterable = concat(this.iterable, ...iterables); return this; } public consume(sink: any[] | Consumer, offset?: number, maxAmount?: number): this { this.checkOnly(); this.iterable = consume(this.iterable, sink, offset, maxAmount); return this; } public cycle(count?: number): this { this.iterable = cycle(this.iterable, count); return this; } public flatMap(mapper: TypedFunction>): IStream { this.iterable = flatMap(this.iterable, mapper); return this; } public filter(predicate: Predicate): this { this.iterable = filter(this.iterable, predicate); return this; } public filterBy(target: K, keyExtractor: TypedFunction, comparator?: Comparator): this { this.iterable = filterBy(this.iterable, target, keyExtractor, comparator); return this; } public index(): IStream<{index: number, value: any}> { this.iterable = index(this.iterable); return this; } public limit(limitTo?: number): this { this.iterable = limit(this.iterable, limitTo); return this; } public map(mapper: TypedFunction): IStream { this.iterable = map(this.iterable, mapper); return this; } public promise(promiseConverter: TypedFunction>): Promise> { this.check(); return promise(this.iterable, promiseConverter) .then(iterable => new InplaceStream(iterable)); } public replace(mapper: TypedFunction): this { this.iterable = map(this.iterable, mapper); return this; } public visit(consumer: Consumer): this { this.iterable = visit(this.iterable, consumer); return this; } public reverse(): this { this.iterable = reverse(this.iterable); return this; } public skip(toSkip?: number): this { this.iterable = skip(this.iterable, toSkip); return this; } public sort(comparator?: Comparator): this { this.iterable = sort(this.iterable, comparator); return this; } public sortBy(keyExtractor: TypedFunction, comparator?: Comparator): this { this.iterable = sortBy(this.iterable, keyExtractor, comparator); return this; } public try(operation: TypedFunction): ITryStream { const x = tryMap(this.iterable, operation); return new TryStreamImpl(x); } public unique(comparator?: Comparator): this { this.iterable = unique(this.iterable, comparator); return this; } public uniqueBy(keyExtractor: TypedFunction): this { this.iterable = uniqueBy(this.iterable, keyExtractor); return this; } public zip(other: Iterable): IStream<[Maybe, Maybe]> { this.iterable = zip(this.iterable, other); return this; } public zipSame(...others: Iterable[]): IStream[]> { this.iterable = zipSame(this.iterable, others); return this; } protected clone(iterable: Iterable): this { return new this.constructor(iterable) as this; } } class TryStreamImpl extends InplaceStream 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: ITry) => 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: any): 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.iterable = map(this.iterable, x => x.flatConvert(operation, backup)); return this; } public convert(operation: TypedFunction, backup?: TypedFunction): ITryStream { this.iterable = map(this.iterable, x => x.convert(operation, backup)); return this; } public orTry(backup: TypedFunction): this { this.iterable = map(this.iterable, y => y.orTry(backup)); return this; } }