/**
* Apply pagination to an array of items.
*
* @param items - Array of items to paginate
* @param offset - Number of items to skip (default: 0)
* @param limit - Maximum number of items to return (default: undefined, returns all)
* @returns Paginated slice of the input array
*
* @example
* ```typescript
* const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
*
* applyPagination(items, 0, 5); // [1, 2, 3, 4, 5]
* applyPagination(items, 5, 5); // [6, 7, 8, 9, 10]
* applyPagination(items, 5); // [6, 7, 8, 9, 10]
* applyPagination(items, 100, 5); // []
* ```
*/
export function applyPagination<T>(items: T[], offset?: number, limit?: number): T[] {
const start = offset ?? 0;
// No pagination needed
if (start === 0 && limit === undefined) {
return items;
}
// Calculate end index
const end = limit !== undefined ? start + limit : undefined;
return items.slice(start, end);
}