Page numbers are one-based because that is what a person reads, and array indices are zero-based because that is what a computer reads. Every off-by-one in pagination lives in the gap between those two facts.
The module under test
| 1 | export function totalPages(count, perPage) { | ||
| 2 | if (perPage <= 0) { | ||
| 3 | return 0; | ||
| 4 | } | ||
| 5 | return Math.ceil(count / perPage); | ||
| 6 | } | ||
| 7 | |||
| 8 | export function pageOf(items, page, perPage) { | ||
| 9 | const pages = totalPages(items.length, perPage); | ||
| 10 | if (page < 1 || page > pages) { | ||
| 11 | return []; | ||
| 12 | } | ||
| 13 | const start = (page - 1) * perPage; | ||
| 14 | return items.slice(start, start + perPage); | ||
| 15 | } | ||
| 16 |
Your tests
Mutants
0%
Mutation score
- Survived
- 0
- Killed
- 0 / 0
- Line coverage
- —
- Tests
- 0
- Assertion width
- —
- Cycle
- —
survivedkilledtimed outequivalentnot run