Skip to content

Instantly share code, notes, and snippets.

@russo97
Created August 16, 2023 00:02
Show Gist options
  • Save russo97/f7141348c8f13f970ad16dd448b273e3 to your computer and use it in GitHub Desktop.
Save russo97/f7141348c8f13f970ad16dd448b273e3 to your computer and use it in GitHub Desktop.
generate a clamp for a range of page indexes
const clampedPaginatedIndex = computed(() => {
const page = parseInt(response.products?.page ?? 1);
const lastPage = response.products?.last_page ?? 1;
const clampRange = Math.min(5, lastPage);
const offset = Math.max(0, Math.min(page - Math.ceil(clampRange / 2), lastPage - clampRange));
return Array.from({ length: clampRange }, (_, index) => offset + index + 1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment