import { DetailedResult, Result } from '../base'; import { CollectibleFactoryCallback, CollectibleKey, ICollectible } from './collectible'; import { KeyValueEntry } from './common'; import { IReadOnlyResultMap, ResultMapForEachCb, ResultMapResultDetail } from './readonlyResultMap'; /** * Additional success or failure details for mutating collector calls. * @public */ export type CollectorResultDetail = ResultMapResultDetail | 'invalid-index'; /** * A read-only interface exposing only the non-mutating methods of a {@link Collections.Collector | ICollector}. * @public */ export interface IReadOnlyCollector> extends IReadOnlyResultMap, TITEM> { /** * Gets the item at a specified index. * @param index - The index of the item to retrieve. * @returns Returns {@link Success | Success} with the item if it exists, or {@link Failure | Failure} * with an error if the index is out of range. */ getAt(index: number): Result; /** * Gets all items in the collection, ordered by index. * @returns An array of items in the collection, ordered by index. */ valuesByIndex(): ReadonlyArray; } /** * Parameters for constructing a {@link Collections.Collector | ICollector}. * @public */ export interface ICollectorConstructorParams> { items?: TITEM[]; } /** * A {@link Collections.Collector | Collector} that is a specialized collection * which contains items of type {@link Collections.ICollectible | ICollectible}, * which have a unique key and a write-once index. * * Items are assigned an index sequentially as they are added to the collection. * Once added, items are immutable - they cannot be removed or replaced. * @public */ export declare class Collector> implements IReadOnlyCollector { private readonly _byKey; private readonly _byIndex; /** * {@inheritdoc Collections.ResultMap.size} */ get size(): number; /** * Constructs a new {@link Collections.Collector | Collector}. * @param params - Optional {@link Collections.ICollectorConstructorParams | initialization parameters} used * to construct the collector. */ constructor(params?: ICollectorConstructorParams); /** * Creates a new {@link Collections.Collector | Collector} instance. * @param params - Optional {@link Collections.ICollectorConstructorParams | initialization parameters} used * to create the collector. * @returns Returns {@link Success | Success} with the new collector if it was created successfully, * or {@link Failure | Failure} with an error if the collector could not be created. */ static createCollector>(params?: ICollectorConstructorParams): Result>; /** * Adds an item to the collection, failing if a different item with the same key already exists. Note * that adding an object that is already in the collection again will succeed without updating the collection. * @param item - The item to add. * @returns Returns {@link DetailedSuccess | Success} with the item and detail `added` if it was added * or detail `exists` if the item was already in the map. Returns {@link DetailedFailure | Failure} with * an error message and appropriate detail if the item could not be added. */ add(item: TITEM): DetailedResult; /** * {@inheritdoc Collections.ResultMap.entries} */ entries(): IterableIterator, TITEM>>; /** * {@inheritdoc Collections.ResultMap.forEach} */ forEach(callback: ResultMapForEachCb, TITEM>, arg?: unknown): void; /** * {@inheritdoc Collections.ResultMap.get} */ get(key: CollectibleKey): DetailedResult; /** * {@inheritdoc Collections.IReadOnlyCollector.getAt} */ getAt(index: number): Result; /** * Gets an existing item with a key matching that of a supplied item, or adds the supplied * item to the collector if no item with that key exists. * @param item - The item to get or add. * @returns Returns {@link DetailedSuccess | Success} with the item stored in the collector - * detail `exists` indicates that an existing item return and detail `added` indicates that the * item was added. Returns {@link DetailedFailure | Failure} with an error and appropriate * detail if the item could not be added. */ getOrAdd(item: TITEM): DetailedResult; /** * Gets an existing item with a key matching the supplied key, or adds a new item to the collector * using a factory callback if no item with that key exists. * @param key - The key of the item to add. * @param callback - The factory callback to create the item. * @returns Returns {@link DetailedSuccess | Success} with the item stored in the collector - * detail `exists` indicates that an existing item return and detail `added` indicates that the * item was added. Returns {@link DetailedFailure | Failure} with an error and appropriate * detail if the item could not be added. */ getOrAdd(key: CollectibleKey, factory: CollectibleFactoryCallback): DetailedResult; /** * {@inheritdoc Collections.ResultMap.has} */ has(key: CollectibleKey): boolean; /** * {@inheritdoc Collections.ResultMap.keys} */ keys(): IterableIterator>; /** * {@inheritdoc Collections.ResultMap.values} */ values(): IterableIterator; /** * {@inheritdoc Collections.IReadOnlyCollector.valuesByIndex} */ valuesByIndex(): ReadonlyArray; /** * Gets a read-only version of this collector. */ toReadOnly(): IReadOnlyCollector; /** * Gets an iterator over the map entries. * @returns An iterator over the map entries. */ [Symbol.iterator](): IterableIterator, TITEM>>; protected _isItem(keyOrItem: CollectibleKey | TITEM): keyOrItem is TITEM; } //# sourceMappingURL=collector.d.ts.map