@magicbell/core
Version:
Official MagicBell API wrapper
77 lines (76 loc) • 1.99 kB
TypeScript
export interface Identifiable {
id?: string | number | null;
}
/**
* A representation of collection of models.
*
* @example
* const store = new Store();
* store.at(0);
*/
export default class Store<T extends Identifiable> {
items: T[];
/**
* Number of models in the `items` array.
*
* If you want to get the total number of models in the server use `total`
* instead.
*/
get length(): number;
/**
* Returns true if the store has no models.
*/
get isEmpty(): boolean;
/**
* Get a model from the `items` array, specified by index.
*/
at(index: number): T;
/**
* Get a model from the `items` array by id.
*/
get(id: string | number): T;
/**
* Creates an array of values by running each model in `items` array thru
* iteratee.
*
* The iteratee is invoked with three arguments: `(model, index, itemsArray)`.
*
* @param fn The function invoked per iteration.
*/
map: (fn: (value: T, index?: number, collection?: T[]) => any) => any[];
/**
* Iterates over items of the store, returning an array of all elements
* `predicate` returns truthy for.
*
* @param predicate The function invoked per iteration.
*/
filter: (predicate: any) => T[];
/**
* Iterates over items of the store, returning the first element `predicate`
* returns truthy for.
*
* @param predicate The function invoked per iteration.
*/
find: (predicate: any) => T;
/**
* Append an item to the store.
*/
push(model: T): boolean;
/**
* Remove a model from the store.
*
* If you want to delete a model from the server, use the `delete` method of
* the model object instead.
*/
remove(model: T): boolean;
/**
* Set the list of items.
*
* @param items
*/
setItems(items: T[]): void;
/**
* Reset the store.
*/
reset(): void;
}