Skip to content

Latest commit

 

History

History
64 lines (56 loc) · 2.57 KB

README.md

File metadata and controls

64 lines (56 loc) · 2.57 KB

paginate

An npm module to create a paginated array with a defined length of elements per page.

Discord server NPM version NPM downloads Build status Dependencies Patreon

npm installnfo

Example

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.