A function to generate page numbers with dots in between for navigational purposes.
All credits go to sarifconrati who provided the gist. See also this gist (original by kottenator for other implementations of this algorithm and for translations to other programming languages.
Install using npm or yarn
npm install koko-pagination-numbers
or
yarn add koko-pagination-numbers
The first parameter is the current page. The second parameter is the last page. The third (optional) parameter is the width, or the amount of page numbers between the dots and the current page number (default = 2).
const getPageNumbers = require("koko-pagination-numbers");
const pageNumbers = getPageNumbers(7, 30);
// returns [1, "...", 5, 6, 7, 8, 9, "...", 30]
const widerPageNumbers = getPageNumbers(15, 30, 4);
// returns [1, "...", 11, 12, 13, 14, 15, 16, 17, 18, 19, "...", 30]