Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/smaug6739/array-paginator
This module is a paginator of arrays for simplify the pagination with arrays. This module split yours arrays in pages and you can recover this pages.
https://github.com/smaug6739/array-paginator
array pager pages paginator
Last synced: 8 days ago
JSON representation
This module is a paginator of arrays for simplify the pagination with arrays. This module split yours arrays in pages and you can recover this pages.
- Host: GitHub
- URL: https://github.com/smaug6739/array-paginator
- Owner: Smaug6739
- License: mit
- Created: 2021-04-08T12:48:28.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-10-24T17:46:39.000Z (about 2 years ago)
- Last Synced: 2024-11-03T09:05:26.391Z (17 days ago)
- Topics: array, pager, pages, paginator
- Language: TypeScript
- Homepage:
- Size: 52.7 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Paginator of arrays
![NPM](https://nodei.co/npm/array-paginator.png)
![[NPM package](https://www.npmjs.com/package/array-paginator)](https://badge.fury.io/js/array-paginator.svg)
![[LICENSE](https://github.com/Smaug6739/array-paginator.js/blob/master/LICENSE)](https://img.shields.io/github/license/Smaug6739/array-paginator.svg)This module is a paginator of arrays for simplify the pagination with arrays.
This module split yours arrays in pages and you can recover this pages.## Getting started
### Installation
With npm :
```sh-session
npm install array-paginator
```With yarn :
```sh-session
yarn add array-paginator
```### Import module
With CommonJS syntax :
```js
const { Paginator } = require("array-paginator");
```With module syntax :
```js
import { Paginator } from "array-paginator";
```## Usage
Create a new Paginator with an array as a parameter and two optionals parameters : max items per page (default:5) and current page (default:1) :
```js
const paginator = new Paginator(data, 15, 1);
```## Paginator#push(newData)
Push data in array.
Parameters :
- newData : A new data (type : `any`).
Type : Boolean
```js
paginator.push(data); // Push data
```## Paginator#set(newData)
Set data in array.
Parameters :
- newData : An array with new data (type : `Array`).
Type : Boolean
```js
paginator.set(data); // Set data
```## Paginator#clear(newData)
Clear data in array.
Parameters :
- newData : An array with new data (type : `Array`).
Type : Boolean
```js
paginator.clear(data); // Clear data
```## Paginator#page(page)
Return a specific page.
Parameters :
- page : The page to get (type : `number`)
Type : Array
```js
const page = paginator.page(1); // Get page one
```## Paginator#first()
Return the first page.
Type : Array
```js
const firstPage = paginator.first(); // Get first page
```## Paginator#last()
Return the last page.
Type : Array
```js
const lastPage = paginator.last(); // Get last page
```## Paginator#next()
Return the next page.
Type : Array
```js
const nextPage = paginator.next(); // Get next page
```## Paginator#previous()
Return the previous page.
Type : Array
```js
const previousPage = paginator.previous(); // Get previous page
```## Paginator#hasFirst()
Return true if first page exist.
Type : Boolean
```js
const previousPage = paginator.previous(); // Get previous page
```## Paginator#hasNext()
Return true if next page exist.
Type : Boolean
```js
const hasNextPage = paginator.hasNext(); // true if next page exists
```## Paginator#hasPrevious()
Return true if previous page exist.
Type : Boolean
```js
const hasPreviousPage = paginator.hasPrevious(); // true if previous page exists
```## Paginator#hasLast()
Return true if last page exist.
Type : Boolean
```js
const hasLastPage = paginator.hasLast(); // true if last page exists
```## Properties
---
## Paginator.current
The current page.
Type : Number## Paginator.all
Return all data.
Type : Array## total
The total of pages.
Type : Number## Example
```js
const { Paginator } = require("array-paginator");
const data = Array.from(new Array(50).keys());
const pager = new Paginator(data, 10);pager.page(2); // [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
pager.first(); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
pager.last(); // [40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
pager.next(); // [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
pager.previous(); // undefined
pager.hasNext(); // true
pager.hasPrevious(); // falsepager.current; //5
pager.all; // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ... 49]
pager.total; //5
```