rich-domain
Version:
This package provide utils file and interfaces to assistant build a complex application with domain driving design
133 lines • 5.45 kB
TypeScript
import { _Iterator, ITeratorConfig } from "../types";
/**
* @description The Iterator class provides a way to traverse through a collection of items sequentially,
* without exposing the underlying data structure. It supports both forward and backward traversal,
* optional looping behavior, and methods to manipulate the underlying collection.
*/
export declare class Iterator<T> implements _Iterator<T> {
private currentIndex;
private readonly items;
private lastCommand;
private readonly returnCurrentOnReversion;
private readonly restartOnFinish;
private constructor();
/**
* @description Creates a new Iterator instance from the provided configuration.
* @param config Optional configuration object.
* @param config.initialData An initial array of items to iterate over.
* @param config.returnCurrentOnReversion If `true`, when switching iteration direction (from `next` to `prev` or vice versa),
* the current item will be returned again before moving on.
* @param config.restartOnFinish If `true`, when reaching the end of the items (`next` beyond last item or `prev` before first item),
* iteration continues from the opposite end, effectively looping the iteration indefinitely.
*
* @returns A new Iterator instance.
*/
static create<U>(config?: ITeratorConfig<U>): Iterator<U>;
/**
* @description Removes a specific item from the iterator's collection.
* If the item is found and removed, the current index is adjusted accordingly.
* @param item The item to be removed.
*/
removeItem(item: T): void;
/**
* @description Checks if there is another item after the current position.
* @returns `true` if another item is available after the current index, otherwise `false`.
*/
hasNext(): boolean;
/**
* @description Checks if there is another item before the current position.
* @returns `true` if another item is available before the current index, otherwise `false`.
*/
hasPrev(): boolean;
/**
* @description Checks if the iterator has no items.
* @returns `true` if there are no items, otherwise `false`.
*/
isEmpty(): boolean;
/**
* @description Moves the iterator forward and returns the next item.
* If `restartOnFinish` is `true` and the end is reached, iteration restarts from the beginning.
* If `restartOnFinish` is `false` and the end is reached, returns `null`.
*
* @returns The next item, or `null` if no next item exists and looping is disabled.
*/
next(): T;
/**
* @description Moves the iterator backward and returns the previous item.
* If `restartOnFinish` is `true` and the beginning is reached, iteration continues from the end.
* If `restartOnFinish` is `false` and the beginning is reached, returns `null`.
*
* @returns The previous item, or `null` if no previous item exists and looping is disabled.
*/
prev(): T;
/**
* @description Returns the first item in the collection without changing the current index.
* @returns The first item, or `undefined` if empty.
*/
first(): T;
/**
* @description Returns the last item in the collection without changing the current index.
* @returns The last item, or `undefined` if empty.
*/
last(): T;
/**
* @description Moves the internal cursor to the start of the collection.
* @returns The current Iterator instance.
*/
toFirst(): Iterator<T>;
/**
* @description Moves the internal cursor to the end of the collection.
* @returns The current Iterator instance.
*/
toLast(): Iterator<T>;
/**
* @description Removes all items from the collection.
* @returns The current Iterator instance.
*/
clear(): Iterator<T>;
/**
* @description Adds a new item to the end of the collection.
* @param data The item to add.
* @returns The current Iterator instance.
*/
addToEnd(data: T): Iterator<T>;
/**
* @description Alias for `addToEnd` - adds a new item to the end of the collection.
* @param data The item to add.
* @returns The current Iterator instance.
*/
add(data: T): Iterator<T>;
/**
* @description Adds a new item to the start of the collection and resets the cursor to before the first element.
* @param data The item to add.
* @returns The current Iterator instance.
*/
addToStart(data: T): Iterator<T>;
/**
* @description Removes the last item from the collection.
* @returns The current Iterator instance.
*/
removeLast(): Iterator<T>;
/**
* @description Removes the first item from the collection.
* @returns The current Iterator instance.
*/
removeFirst(): Iterator<T>;
/**
* @description Creates a new Iterator instance with the same configuration and current state as the existing one.
* @returns A cloned Iterator instance.
*/
clone(): _Iterator<T>;
/**
* @description Returns all items in the iterator as an array.
* @returns A copy of the internal array of items.
*/
toArray(): Array<T>;
/**
* @description Returns the total number of items in the iterator.
* @returns The count of items in the underlying collection.
*/
total(): number;
}
export default Iterator;
//# sourceMappingURL=iterator.d.ts.map