import { Result } from "./Result"; export interface IResultCollection { add(result: Result): void; hasSomeFaultyResult(): boolean; getFirstFaultyResult(): Result; } export class ResultCollection implements IResultCollection { private _collection: Result[] = []; constructor(results?: Result[]) { this._collection = results ?? []; } public add(result: Result): void { this._collection.push(result); } public reset(): void { this._collection = []; } public hasSomeFaultyResult(): boolean { return this._collection.some((result) => result.isFailure); } public getFirstFaultyResult(): Result { return this._collection.find((result) => result.isFailure)!; } public getAllFaultyResults(): Result[] { return this._collection.filter((result) => result.isFailure); } public get objects(): T[] { return this._collection .filter((result) => result.isSuccess) .map((result) => result.object); } public get errors(): E[] { return this._collection .filter((result) => result.isFailure) .map((result) => result.error); } }