apphouse
Version:
Component library for React that uses observable state management and theme-able components.
117 lines (116 loc) • 3.41 kB
TypeScript
/**
* Minimum requirements values for an item in the list
*/
export interface IListItem {
/**
* Unique id for the item
*/
id: string;
}
/**
* A model to handle lists of items
*/
export declare class List<T extends IListItem> {
/**
* Key value pairs of all items in the list
*/
private stack;
/**
* Array of ids only, it keeps track of the order of the items
*/
private order;
/**
* Initializes the list
* The list is initialized empty by default
*/
constructor(list?: T[]);
/**
* Get the array of all items in the list, the list will be ordered
* @returns array of items
*/
get values(): T[];
/**
* returns the count of items in the list
*/
get length(): number;
/**
* Update the list with an array of items
* use this method to update the list with an array of items
* this method resets the list with theses items
* use addValues instead if you want to append to the list
* @param values array of items to initialize the list with
*/
private updateValues;
/**
* Update the list with an item
* If the item already exists it will be updated
* If the item does not exist it will be created
* @param value item to be added/updated in the list
*/
private updateValue;
/**
* Utility method to get the index of an item in the list
* @param id id of the item
* @returns the index of the item or -1 if not found
*/
private getIndex;
/**
* Get an item from the list by id
* @param id id of the item or the index of the item in the list
* @returns the item or undefined if not found
*/
get: (id: string | number) => T | undefined;
/**
* Get an item from the list by index
* @param index the index of the item in the list
* @returns the item or undefined if not found
*/
private getByIndex;
/**
* Add an item to the list or update it if it already exists
* @param item item to be added/updated in the list
*/
set: (item: T | T[]) => void;
/**
* Deletes the last item in the list and returns that item
* @returns the last item in the list
*/
pop: () => T | undefined;
/**
* Deletes the first item in the list and returns that item
* @returns the first item in the list
*/
shift: () => T | undefined;
/**
* Get the last item of the list
* @returns the last item in the list
*/
peakLast: () => T | undefined;
/**
* Get the first item of the list
* @returns the first item in the list
*/
peakFirst: () => T | undefined;
/**
* Delete an item from the list
* @param id id of the item to be removed
* @returns true if item was removed, false if item was not found
*/
delete: (id: string) => boolean;
/**
* Swaps two items in the list
* @param index1 index of the first item to be swapped
* @param index2 index of the second item to be swapped
*/
swap: (index1: number, index2: number) => void;
/**
* Swaps two items in the list by id
* @param id1 the id of the first item to be swapped
* @param id2 the id of the second item to be swapped
*/
swapById: (id1: string, id2: string) => void;
/**
* clears the list
*/
reset: () => void;
}