send
This commit is contained in:
@@ -306,3 +306,24 @@ export const zip = <T extends unknown[][]>(...arrays: T): Zip<T> => {
|
||||
export const zipWith = iterateeFn => (...arrays) => {
|
||||
return map(values => iterateeFn(...values))(zip(...arrays));
|
||||
};
|
||||
|
||||
/**
|
||||
* This method takes a collection of items and a number, returning a collection
|
||||
* of collections, where the maximum amount of items in each is that second arg
|
||||
*/
|
||||
export const paginate = <T>(collection: T[], maxPerPage: number): T[][] => {
|
||||
const pages: T[][] = [];
|
||||
let page: T[] = [];
|
||||
let itemsToAdd = maxPerPage;
|
||||
|
||||
for (const item of collection) {
|
||||
page.push(item);
|
||||
itemsToAdd--;
|
||||
if (!itemsToAdd) {
|
||||
itemsToAdd = maxPerPage;
|
||||
pages.push(page);
|
||||
page = [];
|
||||
}
|
||||
}
|
||||
return pages;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user