← Bug fix + test

A small fix, with a test that catches the original bug

Synthetic demonstration. This example was created for this page. It is not client work or a customer result.

A pagination helper uses an inclusive end index. With a page size of two, item C appears on both page one and page two.

Patch

export function page(items, pageNumber, size) {
  const start = (pageNumber - 1) * size;
- return items.filter((_, index) => index >= start && index <= start + size);
+ return items.filter((_, index) => index >= start && index < start + size);
}

Regression test

import { strict as assert } from 'node:assert';
import { page } from './page.js';

assert.deepEqual(page(['A', 'B', 'C'], 1, 2), ['A', 'B']);
assert.deepEqual(page(['A', 'B', 'C'], 2, 2), ['C']);

Run with node sample.test.js. The first assertion fails on the original helper (['A', 'B', 'C'] instead of ['A', 'B']) and both pass after the patch. Actual customer work will use the agreed repository's own test setup.

Return to the offer and request form