diff --git a/tgui/packages/common/collections.ts b/tgui/packages/common/collections.ts index f0419253e242..bf407b112343 100644 --- a/tgui/packages/common/collections.ts +++ b/tgui/packages/common/collections.ts @@ -311,6 +311,30 @@ export const binaryInsertWith = (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 = (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!!