1.0.3 • Published 6 months ago

nodeinate v1.0.3

Weekly downloads
-
License
GPL-3.0-only
Repository
github
Last release
6 months ago

NodeInate

NodeInate is a lightweight and flexible pagination library for Node.js, designed to simplify data pagination in your applications. Whether you're handling large datasets or need simple pagination for API responses, NodeInate provides an easy-to-use solution.

Installation

To install NodeInate, you can use npm or yarn:

npm install nodeinate

or

yarn add nodeinate

Usage

Basic Example

To use NoteInate in your project, simply require it and call the paginate function with your data parameters.

const pageinate = require('nodeinate');

// Example: Paginate 200 items with 10 items per page on the current page 1.
const pagination = paginate(200, 1 {
    itemsPerPage: 20, // Items per page.
});

console.log(pagination);

Parameters

totalItems (required)

  • The total number of items to paginate.
  • Type: number
  • Example: 200

currentPage (required)

  • The current page number.
  • Type number
  • Example: 1

options (optional)

  • An object containing optional parameters for customizing pagination.

itemsPerPage

  • The number of items per page.
  • Type: number (default: config.itemsPerPage)
  • Example: 20

debug

  • Whether to enable debug mode.
  • Type: boolean (default: config.debug)
  • Example: true

maxPageHyperlinks

  • The maximum number of page hyperlinks to display.
  • Type: number (default: config.maxPageHyperlinks)
  • Example: 5

includeFirstHyperlink

  • Whether to include a "Go to first page" hyperlink.
  • Type: boolean (default: config.includeFirstHyperlink)
  • Exmaple: true

includeLastHyperlink

  • Whether to include a "Go to last page" hyperlink.
  • Type: boolean (default: config.includeLastHyperlink)
  • Example: true

Example Output

{
  "currentPage": 1,
  "totalPages": 10,
  "totalItems": 200,
  "itemsPerPage": 20,
  "pageLinks": [
    { "page": 1, "active": true },
    { "page": 2, "active": false },
    { "page": 3, "active": false },
    { "page": 4, "active": false },
    { "page": 5, "active": false }
  ],
  "hasPrevious": false,
  "hasNext": true,
  "previousPage": null,
  "nextPage": 2,
  "firstPage": 1,
  "lastPage": 10,
  "debugInfo": {
    "startPage": 1,
    "endPage": 5,
    "maxPageHyperlinks": 5
  }
}

Explanation of Output

  • currentPage: The current page number.
  • totalPages: The total number of pages based on the total items and items per page.
  • totalItems: The total number of items.
  • itemsPerPage: The number of items per page.
  • pageLinks: An array of page link objects with page and active properties.
    • page: The page number.
    • active: A boolean indicating if this page is the current page.
  • hasPrevious: true if there is a previous page, otherwise false.
  • hasNext: true if there is a next page, otherwise false.
  • previousPage: The page number of the previous page, or null if there is no previous page.
  • nextPage: The page number of the next page, or null if there is no next page.
  • firstPage: The first page link if enabled.
  • lastPage: The last page link if enabled.
  • debugInfo: (only shown when debug is true): Contains details about the range of pages displayed (startPage, endPage) and the max number of page hyperlinks (maxPageHyperlinks).

Configuration

You can configure the default values of NodeInate by modifying the config.js file.

Default Configuration

The default configuration values are as follows:

module.exports = {
  itemsPerPage: 20,
  debug: false,
  maxPageHyperlinks: 8,
  includeFirstHyperlink: true,
  includeLastHyperlinks: true,
};
  • itemsPerPage: Default number of items displayed per page.
  • debug: Enables debug mode which shows additional information about the pagination range.
  • maxPageHyperlinks: Maximum number of page hyperlinks to display in the pagination.
  • includeFirstHyperlink: Whether to include a "Go to first page" link.
  • includeLastHyperlink: Whether to include a "Go to last page" link.

To modify the default settings, change the values in the config.js file.

Tests

NodeInate is tested using the AVA test runner. You can run the tests to ensure that everything works correctly.

Running Tests

To run the tests, use the following command:

npm test

Or if you are using Yarn:

yarn test

License

NodeInate is licensed under the GNU General Public Licsnse v3.0. See the LICENSE file for more details.

Contributing

If you would like to contribute to the development of NodeInate, please feel free to fork the repository and submit all pull requests.

For any issues or feature requests, please open an issue in the repository.