mirror of
https://github.com/yogstation13/Yogstation.git
synced 2025-02-26 09:04:50 +00:00
Update collections.ts
This commit is contained in:
@@ -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!!
|
||||
|
||||
Reference in New Issue
Block a user