import { Stream } from './Stream'; import { Optional } from '../Optional'; import { NumberStream } from './NumberStream'; import { NumericStream } from './NumericStream'; import { isNullOrUndefined } from '../Core'; export class ObjectStream implements Stream { public static of(values: Y[]): ObjectStream { return new ObjectStream(values); } private values: E[] | undefined; protected constructor(values: E[]) { if (values === null || values === undefined) { throw new Error('Cannot generate stream from src=[' + values + ']'); } else { this.values = [...values]; } } public allMatch(predicate: (c: E) => boolean): boolean { return this.ifOpen(() => this.wrapped().every((each) => predicate(each))); } public anyMatch(predicate: (c: E) => boolean): boolean { return this.ifOpen(() => this.wrapped().some((each) => predicate(each))); } public count(): number { return this.ifOpen(() => this.wrapped().length); } public concat(stream: Stream): Stream { return this.ifOpen(() => new ObjectStream(this.wrapped().concat(stream.toArray()))); } public distinct(identifier?: ((c: E) => any)): Stream { if (identifier === null || identifier === undefined) { return this.ifOpen(() => new ObjectStream(this.wrapped().filter((v, i, s) => s.indexOf(v) === i))); } else { const aux = new Array(); return this.ifOpen(() => new ObjectStream(this.wrapped().filter((v, i, self) => { const expected = (identifier as (c: E) => any)(v); if (aux.some((el) => el === expected)) { return false; } else { aux.push(expected); return true; } }))); } } public dropWhile(predicate: (c: E) => boolean): Stream { return this.ifOpen(() => new ObjectStream(this.wrapped().filter((v) => !predicate(v)))); } public filter(predicate: (c: E) => boolean): Stream { return this.ifOpen(() => new ObjectStream(this.wrapped().filter((v) => !predicate(v)))); } public findAny(predicate?: ((c: E) => boolean) | undefined): Optional { return this.ifOpen(() => { const values = this.wrapped().filter((v) => (predicate as (c: E) => boolean)(v)); return Optional.of(values.length > 0 ? values[Math.floor(Math.random() * values.length) + 0] : undefined); }); } public findFirst(predicate?: ((c: E) => boolean) | undefined): Optional { return this.ifOpen(() => { const values = this.wrapped().filter((v) => (predicate as (c: E) => boolean)(v)); return Optional.of(values.length > 0 ? values[0] : undefined); }); } public map(func: (c: E) => K): Stream { return this.ifOpen(() => new ObjectStream(this.wrapped().map((v) => func(v)))); } public mapToNumber(func: (c: E) => number): NumberStream { return NumericStream.of(this.wrapped().map(func)); } public flatMap(func: (c: E) => Stream): Stream { return this.ifOpen(() => { return new ObjectStream(this.wrapped().map((v) => func(v).toArray()) .reduce((acumulator, value) => isNullOrUndefined(acumulator) ? value : [...acumulator, ...value])); }); } public flatMapToNumber(func: (c: E) => Stream): NumberStream { return this.ifOpen(() => { return NumericStream.of(this.wrapped().map((v) => func(v).toArray()) .reduce((acumulator, value) => isNullOrUndefined(acumulator) ? value : [...acumulator, ...value])); }); } public forEach(consumer: (c: E) => void): void { this.ifOpen(() => { for (const each of this.wrapped()) { consumer(each); } return null; }); } public limit(size: number): Stream { return this.ifOpen(() => new ObjectStream(this.wrapped().slice(0, size))); } public noneMatch(predicate: (c: E) => boolean): boolean { return this.anyMatch((each) => !predicate(each)); } public peek(consumer: (c: E) => void): Stream { for (const each of this.wrapped()) { consumer(each); } return this; } public reduce(callback: (i: E, v: E) => E): E { return this.ifOpen(() => this.wrapped().reduce(callback)); } public skip(count: number): Stream { return this.ifOpen(() => new ObjectStream(this.wrapped().slice(count - 1, this.wrapped().length))); } public sorted(comparator: (c1: E, c2: E) => number): Stream { return this.ifOpen(() => new ObjectStream(this.wrapped().sort(comparator))); } public takeWhile(predicate: (c: E) => boolean): Stream { return this.ifOpen(() => new ObjectStream(this.wrapped().filter((v) => predicate(v)))); } public toArray(): E[] { return this.ifOpen(() => [...this.wrapped()]); } public isClosed(): boolean { return !this.isOpen(); } public close(): void { this.values = undefined; } protected ifOpen(func: () => U): U { if (this.isOpen()) { const value = func(); this.close(); return value; } else { throw new Error('Stream is already closed!'); } } protected isOpen() { return this.values !== null || this.values !== undefined; } protected wrapped(): E[] { return (this.values as E[]); } }