Update collections.ts

This commit is contained in:
Molti
2024-09-11 16:58:22 -05:00
parent ad3ddc393f
commit ff93cf170a

View File

@@ -311,6 +311,30 @@ export const binaryInsertWith = <T, U = unknown>(getKey: (value: T) => U):
};
};
/**
* 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 = [];
}
}
if (page.length) {
pages.push(page);
}
return pages;
};
const isObject = (obj: unknown) => typeof obj === 'object' && obj !== null;
// Does a deep merge of two objects. DO NOT FEED CIRCULAR OBJECTS!!