hd-utils
Version:
A handy utils for modern JS developers
81 lines (80 loc) • 2.46 kB
TypeScript
type Step<T> = T | ((...args: any[]) => T);
type Options = Partial<{
defaultStepIndex: number;
circularPoint: boolean;
}>;
/**
* @description A class to manage a sequence of steps, allowing navigation and dynamic modification..
* Supports adding and removing steps at the beginning, end, and specific positions.
* @example const stepper = new StepDeque(["step1", "step2", (condition) => condition ? "step3" : "step4"]);
* stepper.next(); // "step1"
* stepper.next(); // "step2"
* stepper.next(true); // "step3"
* stepper.previous(); // "step2"
*/
export default class StepDeque<T> {
private steps;
private defaultStepIndex;
private currentIndex;
private isCircular;
constructor(steps: Step<T>[], options?: Options);
getCurrentStepIndex(): number;
getSteps(): Step<T>[];
/**
* Moves to the next step and returns its value.
* If there are no more steps, it returns undefined.
*/
next(...args: any[]): T | undefined;
/**
* Moves to the previous step and returns its value.
* If already at the first step, it returns undefined.
*/
previous(...args: any[]): T | undefined;
/**
* Returns the value of the current step without changing the current index.
*/
current(...args: any[]): T | undefined;
/**
* Resets the stepper to the initial step.
*/
reset(): void;
/**
* Adds a new step at the beginning.
* @param step - The step to add.
*/
addFirst(step: T): void;
/**
* Adds a new step at the end.
* @param step - The step to add.
*/
addLast(step: T): void;
/**
* Adds a new step at a specific index.
* @param step - The step to add.
* @param index - The index at which to add the step.
* @throws If the index is out of bounds.
*/
addAt(step: T, index: number): void;
/**
* Sets a new set of steps and resets the stepper.
* @param steps - An array of step functions.
*/
setSteps(steps: (Step<T> | T)[]): void;
/**
* Removes the first step.
* @throws If there are no steps.
*/
removeFirst(): void;
/**
* Removes the last step.
* @throws If there are no steps.
*/
removeLast(): void;
/**
* Removes the step at a specific index.
* @param index - The index of the step to remove.
* @throws If the index is out of bounds.
*/
removeAt(index: number): void;
}
export {};