UNPKG

array-paginator

Version:

A module for simplify paginate with arrays

83 lines (82 loc) 2.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Paginator = void 0; class Paginator { constructor(data, maxPerPerPage, currentPage) { this.maxPerPage = maxPerPerPage || 5; this.currentPage = currentPage || 1; this.load(data); } load(data) { this.data = data; this.totalPages = Math.ceil(data.length / this.maxPerPage); } get current() { return this.currentPage; } set current(page) { if (page) this.currentPage = page; } get all() { return this.data; } get total() { return this.totalPages; } push(newData) { if (!this.data) return; this.data.push(newData); this.totalPages = Math.ceil(this.data.length / this.maxPerPage); } set(newData) { this.load(newData); return true; } clear() { this.data = undefined; this.totalPages = 0; } page(page) { if (!page) return undefined; this.current = page; const skipValues = page * this.maxPerPage - this.maxPerPage; const pageArray = this.data.slice(skipValues, skipValues + this.maxPerPage); if (!pageArray || !pageArray.length) return undefined; return pageArray; } first() { return this.page(1); } last() { return this.page(this.totalPages); } next() { return this.page(this.currentPage + 1); } previous() { return this.page(this.currentPage - 1); } hasFirst() { return this.totalPages ? true : false; } hasPrevious(index) { if (index) { return index - 1 > 0 ? true : false; } return this.currentPage - 1 > 0 ? true : false; } hasNext(index) { if (index) { return this.totalPages > index + 1 ? true : false; } return this.totalPages > this.currentPage ? true : false; } hasLast() { return this.totalPages ? true : false; } } exports.Paginator = Paginator;