UNPKG

@matheustrres/brasilapi

Version:

Lightweight, easy-to-use & free of dependencies wrapper for BrasilAPI

100 lines 3.19 kB
"use strict"; /** * @author matheustrres * @see {@link [gist](https://gist.github.com/matheustrres/5d6b1647a547e009b33c1cb7117a7e27)} */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Paginator = void 0; /** * A facilitator for data pagination * * @prop {Number} itemsPerPage - The limit of items per page */ class Paginator { itemsPerPage; #items; #pages; /** * Create a new Paginator instance * * @param {PaginatorConfig<T>} config -The configuration options for Paginator * @param {T[]} config.items - The items to be paginated * @param {Number} config.itemsPerPage - The limit of items per page * @param {Number} [config.take] - The number of items to be taken * @param {Number} [config.skip] - The number of items to be skipped */ constructor({ items, itemsPerPage, skip, take }) { this.#items = items; this.#skipItems(skip); this.#takeItems(take); this.itemsPerPage = itemsPerPage ? Math.floor(itemsPerPage) : 20; } /** * Load all pages and it's items * * @returns {T[][]} */ loadPages() { if (!this.#pages || !this.#pages.length) { this.#pages = this.#chunkArr(this.#items, this.itemsPerPage); } return this.#pages; } /** * Load a single page and it's items * * @param {Number} p - The page to be loaded * @returns {T[]} */ loadPage(p = 1) { const pages = this.loadPages(); const page = pages[p - 1]; return page || []; } /** * Take an amount of items from the original items array * * @param {Number} [amount] - The amount of items to be taken (defaults to `itemsPerPage`) * @returns {void} */ #takeItems(amount = this.itemsPerPage) { this.#items = this.#sliceArr(this.#items, 0, amount); } /** * Make a copy of a section from the original items array * * @param {T[]} items - The original items array * @param {Number} start - The start index * @param {Number} [end] - The end index * @returns {T[]} */ #sliceArr(items, start, end) { return items.slice(start, end); } /** * Skip an amount of items from the original items array * * @param {Number} [amount] - The amount of items to be skipped (defaults to `0`) * @returns {void} */ #skipItems(amount = 0) { this.#items = this.#sliceArr(this.#items, amount); } /** * Divides the list of items into pages according to the specified size. * Each page will contain a maximum number of items equal to the specified size. * If the division is not uniform, the last page may contain fewer items. * * @param {T[]} items - The items to be chunked * @param {Number} chunkSize - The amount of items per page * @returns {T[][]} */ #chunkArr(items, chunkSize) { const pages = []; for (let i = 0; i < items.length; i += chunkSize) pages.push(items.slice(i, i + chunkSize)); return pages; } } exports.Paginator = Paginator; //# sourceMappingURL=paginator.js.map