@gruvjs/paginator 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +55 -0
- package/package.json +38 -0
- package/src/core/fromQuery.js +45 -0
- package/src/core/paginate.js +42 -0
- package/src/index.js +8 -0
- package/src/stateful/Paginator.js +95 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Saiteja Madha
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<br />
|
|
3
|
+
<p>
|
|
4
|
+
<a href="https://github.com/gruvjs/gruvjs"><img src="https://raw.githubusercontent.com/gruvjs/gruvjs/main/assets/logo.svg" width="546" alt="gruvjs" /></a>
|
|
5
|
+
</p>
|
|
6
|
+
<br />
|
|
7
|
+
<p>
|
|
8
|
+
<a href="https://www.npmjs.com/package/@gruvjs/paginator"><img src="https://img.shields.io/npm/v/@gruvjs/paginator.svg?maxAge=3600" alt="npm version" /></a>
|
|
9
|
+
<a href="https://www.npmjs.com/package/@gruvjs/paginator"><img src="https://img.shields.io/npm/dt/@gruvjs/paginator.svg?maxAge=3600" alt="npm downloads" /></a>
|
|
10
|
+
</p>
|
|
11
|
+
</div>
|
|
12
|
+
|
|
13
|
+
## About
|
|
14
|
+
|
|
15
|
+
`@gruvjs/paginator` is a universal pagination engine for bots and dashboards.
|
|
16
|
+
|
|
17
|
+
Node.js 18.0.0 or newer is required.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npm install @gruvjs/paginator
|
|
23
|
+
yarn add @gruvjs/paginator
|
|
24
|
+
pnpm add @gruvjs/paginator
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Examples
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
const { paginate, Paginator, fromQuery } = require("@gruvjs/paginator");
|
|
31
|
+
|
|
32
|
+
// Slice any array
|
|
33
|
+
const result = paginate(items, { page: 2, limit: 10 });
|
|
34
|
+
result.items; // → items 11–20
|
|
35
|
+
result.totalPages; // → 5
|
|
36
|
+
result.hasPrev; // → true
|
|
37
|
+
result.hasNext; // → true
|
|
38
|
+
|
|
39
|
+
// Stateful — Discord buttons
|
|
40
|
+
const p = new Paginator({ total: 50, limit: 10 });
|
|
41
|
+
p.next();
|
|
42
|
+
p.buttons(); // → { prev: { disabled: false }, next: { disabled: false }, label: "Page 2 / 5" }
|
|
43
|
+
p.slice(allItems); // → items for current page
|
|
44
|
+
|
|
45
|
+
// REST API
|
|
46
|
+
const { skip, limit, meta } = fromQuery(req.query, { total: 200 });
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
See the [docs](./docs) folder for detailed usage.
|
|
50
|
+
|
|
51
|
+
## Links
|
|
52
|
+
|
|
53
|
+
- [npm](https://www.npmjs.com/package/@gruvjs/paginator)
|
|
54
|
+
- [GitHub](https://github.com/gruvjs/gruvjs/tree/main/packages/paginator)
|
|
55
|
+
- [gruvjs monorepo](https://github.com/gruvjs/gruvjs)
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gruvjs/paginator",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Universal pagination engine for bots and dashboards. Supports arrays, REST APIs and Discord button state.",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"author": "gruvjs",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=18.0.0"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"pagination",
|
|
13
|
+
"paginator",
|
|
14
|
+
"discord",
|
|
15
|
+
"bot",
|
|
16
|
+
"dashboard",
|
|
17
|
+
"rest",
|
|
18
|
+
"zero-dependency"
|
|
19
|
+
],
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/gruvjs/gruvjs.git",
|
|
23
|
+
"directory": "packages/paginator"
|
|
24
|
+
},
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/gruvjs/gruvjs/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/gruvjs/gruvjs/tree/main/packages/paginator#readme",
|
|
29
|
+
"files": [
|
|
30
|
+
"src/",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE"
|
|
33
|
+
],
|
|
34
|
+
"dependencies": {},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"test": "node tests/run.js"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts REST query string params to skip/limit values + metadata.
|
|
3
|
+
* Useful for REST API routes backed by any database.
|
|
4
|
+
*
|
|
5
|
+
* @param {object} query
|
|
6
|
+
* @param {string|number} [query.page]
|
|
7
|
+
* @param {string|number} [query.limit]
|
|
8
|
+
* @param {object} options
|
|
9
|
+
* @param {number} options.total Total document count (e.g. from db.count())
|
|
10
|
+
* @param {number} [options.defaultLimit=20]
|
|
11
|
+
* @param {number} [options.maxLimit=100] Cap on limit to prevent abuse
|
|
12
|
+
* @returns {{ skip: number, limit: number, page: number, meta: object }}
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // GET /users?page=2&limit=20
|
|
16
|
+
* const { skip, limit, meta } = fromQuery(req.query, { total: 200 });
|
|
17
|
+
* const users = await db.users.find({}).skip(skip).limit(limit);
|
|
18
|
+
* res.json({ users, meta });
|
|
19
|
+
*/
|
|
20
|
+
function fromQuery(query = {}, options = {}) {
|
|
21
|
+
const { total = 0, defaultLimit = 20, maxLimit = 100 } = options;
|
|
22
|
+
|
|
23
|
+
const limit = Math.min(Math.max(1, parseInt(query.limit ?? defaultLimit, 10) || defaultLimit), maxLimit);
|
|
24
|
+
const totalPages = Math.max(1, Math.ceil(total / limit));
|
|
25
|
+
const page = Math.min(Math.max(1, parseInt(query.page ?? 1, 10) || 1), totalPages);
|
|
26
|
+
const skip = (page - 1) * limit;
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
skip,
|
|
30
|
+
limit,
|
|
31
|
+
page,
|
|
32
|
+
meta: {
|
|
33
|
+
page,
|
|
34
|
+
limit,
|
|
35
|
+
total,
|
|
36
|
+
totalPages,
|
|
37
|
+
hasPrev: page > 1,
|
|
38
|
+
hasNext: page < totalPages,
|
|
39
|
+
from: total === 0 ? 0 : skip + 1,
|
|
40
|
+
to: Math.min(skip + limit, total),
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
module.exports = { fromQuery };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slices an array for the given page and returns full pagination metadata.
|
|
3
|
+
*
|
|
4
|
+
* @param {Array} items
|
|
5
|
+
* @param {object} [options]
|
|
6
|
+
* @param {number} [options.page=1] 1-indexed page number
|
|
7
|
+
* @param {number} [options.limit=10] Items per page
|
|
8
|
+
* @returns {PaginateResult}
|
|
9
|
+
*
|
|
10
|
+
* @typedef {object} PaginateResult
|
|
11
|
+
* @property {Array} items
|
|
12
|
+
* @property {number} page
|
|
13
|
+
* @property {number} limit
|
|
14
|
+
* @property {number} total
|
|
15
|
+
* @property {number} totalPages
|
|
16
|
+
* @property {boolean} hasPrev
|
|
17
|
+
* @property {boolean} hasNext
|
|
18
|
+
* @property {number} from 1-indexed position of first item on this page
|
|
19
|
+
* @property {number} to 1-indexed position of last item on this page
|
|
20
|
+
*/
|
|
21
|
+
function paginate(items, options = {}) {
|
|
22
|
+
const total = items.length;
|
|
23
|
+
const limit = Math.max(1, options.limit ?? 10);
|
|
24
|
+
const totalPages = Math.max(1, Math.ceil(total / limit));
|
|
25
|
+
const page = Math.min(Math.max(1, options.page ?? 1), totalPages);
|
|
26
|
+
const from = (page - 1) * limit;
|
|
27
|
+
const to = Math.min(from + limit, total);
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
items: items.slice(from, to),
|
|
31
|
+
page,
|
|
32
|
+
limit,
|
|
33
|
+
total,
|
|
34
|
+
totalPages,
|
|
35
|
+
hasPrev: page > 1,
|
|
36
|
+
hasNext: page < totalPages,
|
|
37
|
+
from: total === 0 ? 0 : from + 1,
|
|
38
|
+
to,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports = { paginate };
|
package/src/index.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stateful paginator. Tracks current page and provides Discord button state.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* const p = new Paginator({ total: 50, limit: 10 });
|
|
6
|
+
* p.next();
|
|
7
|
+
* p.buttons(); // → { prev: { disabled: false }, next: { disabled: false }, label: "Page 2 / 5" }
|
|
8
|
+
* p.slice(allItems); // → items for the current page
|
|
9
|
+
*/
|
|
10
|
+
class Paginator {
|
|
11
|
+
/**
|
|
12
|
+
* @param {object} options
|
|
13
|
+
* @param {number} options.total Total item count.
|
|
14
|
+
* @param {number} [options.limit=10] Items per page.
|
|
15
|
+
* @param {number} [options.page=1] Initial page.
|
|
16
|
+
*/
|
|
17
|
+
constructor(options = {}) {
|
|
18
|
+
if (typeof options.total !== "number" || options.total < 0) {
|
|
19
|
+
throw new TypeError("total must be a non-negative number");
|
|
20
|
+
}
|
|
21
|
+
this._limit = Math.max(1, options.limit ?? 10);
|
|
22
|
+
this._total = options.total;
|
|
23
|
+
this._totalPages = Math.max(1, Math.ceil(this._total / this._limit));
|
|
24
|
+
this._page = Math.min(Math.max(1, options.page ?? 1), this._totalPages);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ── Navigation ─────────────────────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
/** Advance to the next page. No-op if already on the last page. */
|
|
30
|
+
next() { if (this._page < this._totalPages) this._page++; return this; }
|
|
31
|
+
|
|
32
|
+
/** Go back to the previous page. No-op if already on the first page. */
|
|
33
|
+
prev() { if (this._page > 1) this._page--; return this; }
|
|
34
|
+
|
|
35
|
+
/** Jump to a specific page. Clamped to [1, totalPages]. */
|
|
36
|
+
goTo(page) { this._page = Math.min(Math.max(1, page), this._totalPages); return this; }
|
|
37
|
+
|
|
38
|
+
/** Jump to the first page. */
|
|
39
|
+
first() { return this.goTo(1); }
|
|
40
|
+
|
|
41
|
+
/** Jump to the last page. */
|
|
42
|
+
last() { return this.goTo(this._totalPages); }
|
|
43
|
+
|
|
44
|
+
// ── Getters ────────────────────────────────────────────────────────────────
|
|
45
|
+
|
|
46
|
+
get page() { return this._page; }
|
|
47
|
+
get limit() { return this._limit; }
|
|
48
|
+
get total() { return this._total; }
|
|
49
|
+
get totalPages() { return this._totalPages; }
|
|
50
|
+
get hasPrev() { return this._page > 1; }
|
|
51
|
+
get hasNext() { return this._page < this._totalPages; }
|
|
52
|
+
|
|
53
|
+
/** 0-indexed offset for the current page. Useful for array slicing. */
|
|
54
|
+
get offset() { return (this._page - 1) * this._limit; }
|
|
55
|
+
|
|
56
|
+
// ── Helpers ────────────────────────────────────────────────────────────────
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Returns Discord-ready button state for prev/next navigation.
|
|
60
|
+
* @param {object} [labels]
|
|
61
|
+
* @param {string} [labels.prev="◀"]
|
|
62
|
+
* @param {string} [labels.next="▶"]
|
|
63
|
+
* @returns {{ prev: { disabled: boolean, label: string }, next: { disabled: boolean, label: string }, label: string }}
|
|
64
|
+
*/
|
|
65
|
+
buttons(labels = {}) {
|
|
66
|
+
return {
|
|
67
|
+
prev: { disabled: !this.hasPrev, label: labels.prev ?? "◀" },
|
|
68
|
+
next: { disabled: !this.hasNext, label: labels.next ?? "▶" },
|
|
69
|
+
label: labels.label ?? `Page ${this._page} / ${this._totalPages}`,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Slices an array for the current page.
|
|
75
|
+
* @param {Array} items
|
|
76
|
+
* @returns {Array}
|
|
77
|
+
*/
|
|
78
|
+
slice(items) {
|
|
79
|
+
return items.slice(this.offset, this.offset + this._limit);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
toJSON() {
|
|
83
|
+
return {
|
|
84
|
+
page: this._page,
|
|
85
|
+
limit: this._limit,
|
|
86
|
+
total: this._total,
|
|
87
|
+
totalPages: this._totalPages,
|
|
88
|
+
hasPrev: this.hasPrev,
|
|
89
|
+
hasNext: this.hasNext,
|
|
90
|
+
offset: this.offset,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
module.exports = { Paginator };
|