Skip to content

Commit

Permalink
fix divider showing for single page gap
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainCodeman committed Sep 14, 2024
1 parent 80a16e2 commit ba7db17
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "svelte-page-links",
"description": "Generate pagination links for svelte",
"version": "0.0.3",
"version": "0.0.4",
"keywords": [
"svelte",
"page",
Expand Down
34 changes: 33 additions & 1 deletion src/lib/pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function render(links: Generator<Link>) {
return results.join()
}

describe('pages', () => {
describe('10 pages, 2 end, 1 adjacent', () => {
test.each([
{
page: 1,
Expand Down Expand Up @@ -78,3 +78,35 @@ describe('pages', () => {
expect(render(pages(page, total, 2))).deep.eq(expected)
})
})

describe('5 pages, 2 end, 0 adjacent', () => {
test.each([
{
page: 1,
total: 5,
expected: '<1,1,2,3,4,5,2>',
},
{
page: 2,
total: 5,
expected: '<1,1,2,3,4,5,3>',
},
{
page: 3,
total: 5,
expected: '<2,1,2,3,4,5,4>',
},
{
page: 4,
total: 5,
expected: '<3,1,2,3,4,5,5>',
},
{
page: 5,
total: 5,
expected: '<4,1,2,3,4,5,5>',
},
])('pages($page, $total) -> $expected', ({ page, total, expected }) => {
expect(render(pages(page, total, 2, 0))).deep.eq(expected)
})
})
3 changes: 2 additions & 1 deletion src/lib/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export function* pages(page: number, total: number, ends = 1, adjacent = 1): Gen
(p >= left && p <= right) ||
// avoid dividers if it's a single page
(p === left - 1 && p === first + 1) ||
(p === right + 1 && p === last - 1)
(p === right + 1 && p === last - 1) ||
(p == first + 1 && p === last - 1)
) {
yield { page: p, type: 'link', active: p === page, disabled: p === page }
p++
Expand Down

0 comments on commit ba7db17

Please sign in to comment.