Skip to content

Instantly share code, notes, and snippets.

@nullxx
Created February 23, 2022 08:09
Show Gist options
  • Save nullxx/7c7ee44bf115231affd932f187728bc1 to your computer and use it in GitHub Desktop.
Save nullxx/7c7ee44bf115231affd932f187728bc1 to your computer and use it in GitHub Desktop.
Paginate reverse order
const requestedPage = 5;
const page = Math.max(1, requestedPage + 1);
console.log('Requested page', page);
const totalNotis = 20;
const paginate = 3;
const skip = Math.max(0, totalNotis - page * paginate);
console.log('Skip', skip);
let target = Math.max(0, (totalNotis - page*paginate) + paginate)
const limit = target - skip
console.log('limit', limit);
console.log(`Page ${page}:`, 'From', skip, 'to', target);
const nextTarget = (totalNotis - (page+1)*paginate) + paginate
const hasNextPage = nextTarget > 0;
console.log('hasNextPage', hasNextPage)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment