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.
105 lines • 4.09 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<ArrayItem extends unknown> = (value: ArrayItem, index: number, array: ArrayItem[]) => boolean;
export type GetterResult<ArrayItem extends unknown> = {
/**
* - The subset of items for the requested page.
*/
items: ArrayItem[];
/**
* - The current (validated) page number.
*/
page: number;
/**
* - Number of items per page used in the calculation.
*/
perPage: number;
/**
* - Total number of items in the filtered data.
*/
totalItems: number;
/**
* - Total number of pages available.
*/
totalPages: number;
/**
* - Whether a previous page exists.
*/
hasPrev: boolean;
/**
* - Whether a next page exists.
*/
hasNext: 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`.
*
* @template {any} ArrayItem
* @callback GetFilter
* @param {ArrayItem} value - The current element being processed in the array.
* @param {number} index - The index of the current element within the array.
* @param {ArrayItem[]} array - The full array being processed.
* @returns {boolean} Returns `true` to include the element in the results, or `false` to exclude it.
*/
/**
* @template {any} ArrayItem
* @typedef {Object} GetterResult
* @property {ArrayItem[]} items - The subset of items for the requested page.
* @property {number} page - The current (validated) page number.
* @property {number} perPage - Number of items per page used in the calculation.
* @property {number} totalItems - Total number of items in the filtered data.
* @property {number} totalPages - Total number of pages available.
* @property {boolean} hasPrev - Whether a previous page exists.
* @property {boolean} hasNext - Whether a next page exists.
*/
/**
* A encapsulated wrapper for array pagination.
* Provides methods to retrieve paginated results with metadata,
* while keeping the source array safe from direct modifications.
* @template {any} ArrayItem
*/
declare class TinyArrayPaginator<ArrayItem extends unknown> {
/**
* Creates a new paginator instance for the given data array.
* @param {ArrayItem[]|Set<ArrayItem>} data - The array to be paginated.
* @throws {TypeError} If the provided data is not an array.
*/
constructor(data: ArrayItem[] | Set<ArrayItem>);
/**
* Replaces the current data array.
* @param {ArrayItem[]|Set<ArrayItem>} value - The new array to be used as the data source.
* @throws {TypeError} If the provided value is not an array.
*/
set data(value: ArrayItem[] | Set<ArrayItem>);
/**
* Gets current stored array.
* @returns {ArrayItem[]|Set<ArrayItem>}
*/
get data(): ArrayItem[] | Set<ArrayItem>;
/**
* 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, ArrayItem|RegExp> | GetFilter<ArrayItem>} [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 {GetterResult<ArrayItem>}
*/
get({ page, perPage, filter }: {
page: number;
perPage: number;
filter?: Record<string, RegExp | ArrayItem> | GetFilter<ArrayItem> | undefined;
}): GetterResult<ArrayItem>;
#private;
}
//# sourceMappingURL=TinyArrayPaginator.d.mts.map