tiny-essentials
Version:
Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.
78 lines • 3.28 kB
text/typescript
export default TinyArrayPaginator;
/**
* A predicate function used to determine whether an item should be included in the filtered results.
* Works similarly to the callback function of `Array.prototype.filter`.
*/
export type GetFilter = (value: any, index: number, array: any[]) => boolean;
/**
* A predicate function used to determine whether an item should be included in the filtered results.
* Works similarly to the callback function of `Array.prototype.filter`.
*
* @callback GetFilter
* @param {any} value - The current element being processed in the array.
* @param {number} index - The index of the current element within the array.
* @param {any[]} array - The full array being processed.
* @returns {boolean} Returns `true` to include the element in the results, or `false` to exclude it.
*/
/**
* A encapsulated wrapper for array pagination.
* Provides methods to retrieve paginated results with metadata,
* while keeping the source array safe from direct modifications.
*/
declare class TinyArrayPaginator {
/**
* Creates a new paginator instance for the given data array.
* @param {any[]|Set<any>} data - The array to be paginated.
* @throws {TypeError} If the provided data is not an array.
*/
constructor(data: any[] | Set<any>);
/**
* Replaces the current data array.
* @param {any[]|Set<any>} value - The new array to be used as the data source.
* @throws {TypeError} If the provided value is not an array.
*/
set data(value: any[] | Set<any>);
/**
* Gets current stored array.
* @returns {any[]|Set<any>}
*/
get data(): any[] | Set<any>;
/**
* Gets the total number of items in the current data array.
* @returns {number} Total number of stored items.
*/
get size(): number;
/**
* Filters data according to a search query and returns paginated results.
* @param {Object} settings
* @param {number} settings.page - The page number (1-based index).
* @param {number} settings.perPage - Items per page.
* @param {Record<string, any> | GetFilter} [settings.filter=null] - Filtering criteria:
* - Object: key-value pairs for exact match, substring match, or RegExp
* - Function: custom filter function returning true for items to include
* @returns {{
* items: any[], // The subset of items for the requested page.
* page: number, // The current (validated) page number.
* perPage: number, // Number of items per page used in the calculation.
* totalItems: number, // Total number of items in the filtered data.
* totalPages: number, // Total number of pages available.
* hasPrev: boolean, // Whether a previous page exists.
* hasNext: boolean // Whether a next page exists.
* }}
*/
get({ page, perPage, filter }: {
page: number;
perPage: number;
filter?: Record<string, any> | GetFilter | undefined;
}): {
items: any[];
page: number;
perPage: number;
totalItems: number;
totalPages: number;
hasPrev: boolean;
hasNext: boolean;
};
#private;
}
//# sourceMappingURL=TinyArrayPaginator.d.mts.map