1.0.0 • Published 6 months ago

lite-pagination v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

Light Pagination Module

This Node.js module is utilized for crafting a pagination API. For example, if you need pagination on the frontend similar to what is shown in the image.

To accomplish this, you will require an API. This script is very straightforward. Simply integrate this script into your project following the instructions.

This module takes three arguments:

Installation

You can install the module using git or npm:

GIT

git clone https://github.com/serhiihonchargithub/lite-pagination.git

NPM

npm install --save lite-pagination@1.0.0

Usage

// Importing the module
let pagination = require('path/to/lite-pagination');

Example Usage

For example, you have an address like domain/posts?offset=50&limit=5 in your API. The script behind it sends a request to the database, and the database returns the count of posts as 285. Your limit is set to 5, and you are currently on page 50.

// Using the pagination function
let result = pagination.pagination(285, 50, 5);

// Example output
console.log(result);

Return

"pagination" : {
    "count": {
      "posts": 285,
      "pages": 11
    },
    "control": {
      "prev": {
        "number": 10,
        "offset": 45
      },
      "current": {
        "number": 11,
        "offset": 50
      },
      "next": {
        "number": 12,
        "offset": 55
      },
      "last": {
        "number": 57,
        "offset": 280
      }
    },
    "nav": [
      {
        "number": 1,
        "offset": 0,
        "current": false
      },
      {
        "number": "...",
        "offset": 40,
        "current": false
      },
      {
        "number": 10,
        "offset": 45,
        "current": false
      },
      {
        "number": 11,
        "offset": 50,
        "current": true
      },
      {
        "number": 12,
        "offset": 55,
        "current": false
      },
      null,
      {
        "number": "...",
        "offset": 60,
        "current": false
      },
      {
        "number": 57,
        "offset": 280,
        "current": false
      }
    ]
  }
1.0.0

6 months ago