2.3.3 • Published 2 years ago
paging-util v2.3.3
:arrow_down: Install package
Install with yarn:
$ yarn add paging-util
:file_folder: Basic Usage
- Using (import/export) ESM with TypeScript:
import { paginate, offsetBased, range } from 'paging-util';
const offset = offsetBased(2, 10); // => output: 10
const paging = paginate({ recods: 100, setRange: true });
{
offset: 0,
pagination: {
records: 100,
totalPages: 10,
currentPage: 1,
firstPage: 1,
limit: 10,
next: 2,
previous: null,
hasNext: true,
hasPrevious: false,
isLastPage: false,
firstIndex: 0,
lastIndex: 9,
length: 10,
range: [
1, 2, 3, 4, 5,
6, 7, 8, 9, 10
], // array: range/total pages
},
constants: {
MIN_LIMIT: 10,
MAX_LIMIT: 20,
}
}
range(1, 5); // output: [1, 2, 3, 4, 5]
range(5, 1); // output: [5, 4, 3, 2, 1]
range(-5, 1); // output: [-5, -4, -3, -2, -1, 0, 1]
- Using (require/node) CommonJS:
const { paginate } = require('paging-util');
const { range } = paginate({ records: 100, setRange: true });
const odds = range.filter(value => !(value % 2));
console.log(odds); // output: [2, 4, 6, 8, 10]
:wrench: API
- paginate({ records: 100, ...options }):
Options
Property name | Required | Default | Description |
---|---|---|---|
records | true | - | Resources/records |
page | false | 1 | Current page |
limit | false | 10 | Total (resources) to show per page |
setRange | false | false | Set array of pages |
setConstants | false | true | Get constants |
min | false | 10 | Min limit |
max | false | 20 | Max limit |
Output:
- Object:
- records - Total records/resources to paging.
- totalPages - Total pages
- currentPage - Current page, default:
1
- firstPage - First page, default:
1
- limit - Total items to show per page, default:
10
- next - Next page
- previous - Previous page,
- hasNext -
true
orfalse
- hasPrevious -
true
orfalse
- firstIndex, lastIndex - First and last result (index).
- length - Results length.
- offSet - Offset-based pagination.
- constants - Fixed values.
- range - Array of pages, default:
null
range(start, end?, step?): Array of pages.
Offset-based pagination algorithm/logic:
import logic from 'node/logic';
logic.explode(); // 😄
/**
* Output:
* - Offset-based pagination:
* ((1 - 1) * 10) = 0 * 10 = 0
* ((2 - 1) * 10) = 1 * 10 = 10
*
* Position 1: 0 - 9
* Position 2: 10 - 19
* Position 3: 20 - 29
*/
:open_hands: Contributing
Contributions, issues and feature requests are welcome!Feel free to check issues page.
📝 License
Copyright © 2021 José Lucas. This project is MIT licensed.