Skip to content

Instantly share code, notes, and snippets.

@m1chaeldev
Last active December 4, 2023 15:17
Show Gist options
  • Select an option

  • Save m1chaeldev/9bc69d60a9b06d47349114ab91f03fa9 to your computer and use it in GitHub Desktop.

Select an option

Save m1chaeldev/9bc69d60a9b06d47349114ab91f03fa9 to your computer and use it in GitHub Desktop.
const getPaginationPages = (current) => {
const total = 50;
const min = (() => {
if (total <= 5) return 1;
// reached to total pages
if (current + 2 > total) {
return total - 4;
}
return current - 2 > 1 ? current - 2 : 1;
})();
const arr = Array.from({ length: 5 }, (_, i) => i + min);
console.log(current, arr);
};
getPaginationPages(1); // result: [ 1, 2, 3, 4, 5 ]
getPaginationPages(2); // result: [ 1, 2, 3, 4, 5 ]
getPaginationPages(3); // result: [ 1, 2, 3, 4, 5 ]
getPaginationPages(4); // result: [ 2, 3, 4, 5, 6 ]
getPaginationPages(5); // result: [ 3, 4, 5, 6, 7 ]
getPaginationPages(24); // result: [ 22, 23, 24, 25, 26 ]
getPaginationPages(25); // result: [ 23, 24, 25, 26, 27 ]
getPaginationPages(26); // result: [ 24, 25, 26, 27, 28 ]
getPaginationPages(46); // result: [ 44, 45, 46, 47, 48 ]
getPaginationPages(47); // result: [ 45, 46, 47, 48, 49 ]
getPaginationPages(48); // result: [ 46, 47, 48, 49, 50 ]
getPaginationPages(49); // result: [ 46, 47, 48, 49, 50 ]
getPaginationPages(50); // result: [ 46, 47, 48, 49, 50 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment