First, import paginate:
// TypeScript or ES Module
import { Paginate } from '@the-nerd-cave/paginate';
// CommonJS
const { Paginate } = require('@the-nerd-cave/paginate');
Then to use the module, we're going to need an array object:
const arr = [
{ name: 'John Doe', age: 21 },
{ name: 'John Doe', age: 21 },
{ name: 'John Doe', age: 21 },
{ name: 'Jane Doe', age: 20 },
{ name: 'Jane Doe', age: 20 },
{ name: 'Jane Doe', age: 20 },
{ name: 'Bob Dylan', age: 19 },
{ name: 'Bob Dylan', age: 19 },
{ name: 'Bob Dylan', age: 19 }
];
You can generate a new paginated array multiple ways:
// Pass values via the constructor
const paginated = new Paginate(arr, 3);
// Pass values using methods
const paginated = new Paginate()
.setArray(arr)
.setSize(3);
Then to access the generated pages, use the Paginate#getPaginatedArray()
method:
// Get the new paginated array
const pages = paginated.getPaginatedArray();
// You can access whichever page you like by passing in the index
console.log(pages[0]); // [{ name: 'John Doe', age: 21 }, { name: 'John Doe', age: 21 }, { name: 'John Doe', age: 21 }]
To test this live, clone this repository and go to the example
directory. From there, run npm test
.