@visulima/pagination 1.0.0 → 1.0.2
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/CHANGELOG.md +14 -0
- package/package.json +7 -8
- package/src/index.ts +0 -8
- package/src/paginator.ts +0 -173
- package/src/types.d.ts +0 -35
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## @visulima/pagination [1.0.2](https://github.com/visulima/visulima/compare/@visulima/pagination@1.0.1...@visulima/pagination@1.0.2) (2022-12-10)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* fixed wrong file name for module key in package.json ([2c90a2d](https://github.com/visulima/visulima/commit/2c90a2d75848a159bc33dc58f303af36ee127570))
|
|
7
|
+
|
|
8
|
+
## @visulima/pagination [1.0.1](https://github.com/visulima/visulima/compare/@visulima/pagination@1.0.0...@visulima/pagination@1.0.1) (2022-12-01)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* added correct folder to files key in package.json ([da9f987](https://github.com/visulima/visulima/commit/da9f9871462a0b2663046cde5f05e9a90df4c496))
|
|
14
|
+
|
|
1
15
|
## @visulima/pagination 1.0.0 (2022-11-15)
|
|
2
16
|
|
|
3
17
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@visulima/pagination",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Pagination for node.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"anolilab",
|
|
@@ -39,12 +39,11 @@
|
|
|
39
39
|
"./package.json": "./package.json"
|
|
40
40
|
},
|
|
41
41
|
"main": "dist/index.js",
|
|
42
|
-
"module": "dist/index.
|
|
42
|
+
"module": "dist/index.mjs",
|
|
43
43
|
"source": "src/index.ts",
|
|
44
44
|
"types": "dist/index.d.ts",
|
|
45
45
|
"files": [
|
|
46
|
-
"
|
|
47
|
-
"dist",
|
|
46
|
+
"dist/**",
|
|
48
47
|
"README.md",
|
|
49
48
|
"CHANGELOG.md",
|
|
50
49
|
"LICENSE.md"
|
|
@@ -55,7 +54,7 @@
|
|
|
55
54
|
"clean": "rimraf node_modules dist",
|
|
56
55
|
"coverage": "vitest run --coverage",
|
|
57
56
|
"dev": "pnpm predev && pnpm run build --watch",
|
|
58
|
-
"lint:eslint": "cross-env NO_LOGS=true eslint --ext js,jsx,ts,tsx --max-warnings=0 --config .eslintrc.cjs",
|
|
57
|
+
"lint:eslint": "cross-env NO_LOGS=true eslint . --ext js,jsx,ts,tsx --max-warnings=0 --config .eslintrc.cjs --cache --cache-strategy content .",
|
|
59
58
|
"lint:eslint:fix": "pnpm run lint:eslint --fix",
|
|
60
59
|
"test": "vitest"
|
|
61
60
|
},
|
|
@@ -87,12 +86,12 @@
|
|
|
87
86
|
"eslint-plugin-optimize-regex": "^1.2.1",
|
|
88
87
|
"eslint-plugin-promise": "^6.0.1",
|
|
89
88
|
"eslint-plugin-radar": "^0.2.1",
|
|
90
|
-
"eslint-plugin-react": "7.31.
|
|
89
|
+
"eslint-plugin-react": "7.31.11",
|
|
91
90
|
"eslint-plugin-react-hooks": "4.6.0",
|
|
92
91
|
"eslint-plugin-simple-import-sort": "^8.0.0",
|
|
93
92
|
"eslint-plugin-sort-keys-fix": "^1.1.2",
|
|
94
93
|
"eslint-plugin-testing-library": "^5.7.2",
|
|
95
|
-
"eslint-plugin-unicorn": "^
|
|
94
|
+
"eslint-plugin-unicorn": "^45.0.0",
|
|
96
95
|
"eslint-plugin-you-dont-need-lodash-underscore": "^6.12.0",
|
|
97
96
|
"eslint-plugin-you-dont-need-momentjs": "^1.6.0",
|
|
98
97
|
"prettier": "^2.7.1",
|
|
@@ -101,7 +100,7 @@
|
|
|
101
100
|
"semantic-release": "^19.0.5",
|
|
102
101
|
"tsup": "^6.2.3",
|
|
103
102
|
"typescript": "^4.8.4",
|
|
104
|
-
"vitest": "^0.
|
|
103
|
+
"vitest": "^0.25.0"
|
|
105
104
|
},
|
|
106
105
|
"engines": {
|
|
107
106
|
"node": ">=16"
|
package/src/index.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import Paginator from "./paginator";
|
|
2
|
-
import type { Paginator as PaginatorInterface } from "./types";
|
|
3
|
-
|
|
4
|
-
export type { Paginator as PaginatorInterface } from "./types.d";
|
|
5
|
-
export { default as Paginator } from "./paginator";
|
|
6
|
-
|
|
7
|
-
// eslint-disable-next-line max-len
|
|
8
|
-
export const paginate = <Result>(page: number, perPage: number, total: number, rows: Result[]): PaginatorInterface<Result> => new Paginator(total, Number(perPage), Number(page), ...rows);
|
package/src/paginator.ts
DELETED
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import qs from "qs";
|
|
2
|
-
|
|
3
|
-
import type { Paginator as IPaginator } from "./types";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Simple paginator works with the data set provided by the standard
|
|
7
|
-
* `offset` and `limit` based pagination.
|
|
8
|
-
*/
|
|
9
|
-
export default class Paginator<T = any> extends Array implements IPaginator<T> {
|
|
10
|
-
private qs: { [key: string]: any } = {};
|
|
11
|
-
|
|
12
|
-
private url: string = "/";
|
|
13
|
-
|
|
14
|
-
private readonly rows: any[];
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* The first page is always 1
|
|
18
|
-
*/
|
|
19
|
-
public readonly firstPage: number = 1;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Find if results set is empty or not
|
|
23
|
-
*/
|
|
24
|
-
public readonly isEmpty: boolean;
|
|
25
|
-
|
|
26
|
-
constructor(private readonly totalNumber: number, public readonly perPage: number, public currentPage: number, ...rows: any[]) {
|
|
27
|
-
super(...rows);
|
|
28
|
-
|
|
29
|
-
this.totalNumber = Number(totalNumber);
|
|
30
|
-
|
|
31
|
-
this.rows = rows;
|
|
32
|
-
this.isEmpty = this.rows.length === 0;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Find if there are total records or not. This is not same as
|
|
37
|
-
* `isEmpty`.
|
|
38
|
-
*
|
|
39
|
-
* The `isEmpty` reports about the current set of results. However, `hasTotal`
|
|
40
|
-
* reports about the total number of records, regardless of the current.
|
|
41
|
-
*/
|
|
42
|
-
get hasTotal(): boolean {
|
|
43
|
-
return this.total > 0;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Find if there are more pages to come
|
|
48
|
-
*/
|
|
49
|
-
get hasMorePages(): boolean {
|
|
50
|
-
return this.lastPage > this.currentPage;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Find if there are enough results to be paginated or not
|
|
55
|
-
*/
|
|
56
|
-
get hasPages(): boolean {
|
|
57
|
-
return this.lastPage !== 1;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* The Last page number
|
|
62
|
-
*/
|
|
63
|
-
get lastPage(): number {
|
|
64
|
-
return Math.max(Math.ceil(this.total / this.perPage), 1);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Casting `total` to a number. Later, we can think of situations
|
|
69
|
-
* to cast it to a bigint
|
|
70
|
-
*/
|
|
71
|
-
get total(): number {
|
|
72
|
-
return Number(this.totalNumber);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* A reference to the result rows
|
|
77
|
-
*/
|
|
78
|
-
public all() {
|
|
79
|
-
return this.rows;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Returns JSON meta data
|
|
84
|
-
*/
|
|
85
|
-
public getMeta() {
|
|
86
|
-
return {
|
|
87
|
-
total: this.total,
|
|
88
|
-
perPage: this.perPage,
|
|
89
|
-
page: this.currentPage,
|
|
90
|
-
lastPage: this.lastPage,
|
|
91
|
-
firstPage: this.firstPage,
|
|
92
|
-
firstPageUrl: this.getUrl(1),
|
|
93
|
-
lastPageUrl: this.getUrl(this.lastPage),
|
|
94
|
-
nextPageUrl: this.getNextPageUrl(),
|
|
95
|
-
previousPageUrl: this.getPreviousPageUrl(),
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Returns JSON representation of the paginated
|
|
101
|
-
* data
|
|
102
|
-
*/
|
|
103
|
-
public toJSON() {
|
|
104
|
-
return {
|
|
105
|
-
meta: this.getMeta(),
|
|
106
|
-
data: this.all(),
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Define query string to be appended to the pagination links
|
|
112
|
-
*/
|
|
113
|
-
public queryString(values: { [key: string]: any }): this {
|
|
114
|
-
this.qs = values;
|
|
115
|
-
|
|
116
|
-
return this;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Define base url for making the pagination links
|
|
121
|
-
*/
|
|
122
|
-
public baseUrl(url: string): this {
|
|
123
|
-
this.url = url;
|
|
124
|
-
|
|
125
|
-
return this;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Returns url for a given page. Doesn't validate the integrity of the
|
|
130
|
-
* page
|
|
131
|
-
*/
|
|
132
|
-
public getUrl(page: number): string {
|
|
133
|
-
const qstring = qs.stringify({ ...this.qs, page: page < 1 ? 1 : page });
|
|
134
|
-
|
|
135
|
-
return `${this.url}?${qstring}`;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Returns url for the next page
|
|
140
|
-
*/
|
|
141
|
-
public getNextPageUrl(): string | null {
|
|
142
|
-
if (this.hasMorePages) {
|
|
143
|
-
return this.getUrl(this.currentPage + 1);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return null;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Returns URL for the previous page
|
|
151
|
-
*/
|
|
152
|
-
public getPreviousPageUrl(): string | null {
|
|
153
|
-
if (this.currentPage > 1) {
|
|
154
|
-
return this.getUrl(this.currentPage - 1);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return null;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Returns an array of urls under a given range
|
|
162
|
-
*/
|
|
163
|
-
public getUrlsForRange(start: number, end: number) {
|
|
164
|
-
const urls: { url: string; page: number; isActive: boolean }[] = [];
|
|
165
|
-
|
|
166
|
-
// eslint-disable-next-line no-plusplus
|
|
167
|
-
for (let index = start; index <= end; index++) {
|
|
168
|
-
urls.push({ url: this.getUrl(index), page: index, isActive: index === this.currentPage });
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return urls;
|
|
172
|
-
}
|
|
173
|
-
}
|
package/src/types.d.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
export type PaginationResult<Result> = { meta: PaginationMeta; data: Result[] };
|
|
2
|
-
export type PaginationMeta = {
|
|
3
|
-
total: number;
|
|
4
|
-
perPage: number;
|
|
5
|
-
page: number;
|
|
6
|
-
lastPage: number;
|
|
7
|
-
firstPage: number;
|
|
8
|
-
firstPageUrl: null | string;
|
|
9
|
-
lastPageUrl: null | string;
|
|
10
|
-
nextPageUrl: null | string;
|
|
11
|
-
previousPageUrl: null | string;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export interface Paginator<Result> extends Array<Result> {
|
|
15
|
-
all(): Result[];
|
|
16
|
-
|
|
17
|
-
readonly firstPage: number;
|
|
18
|
-
readonly perPage: number;
|
|
19
|
-
readonly currentPage: number;
|
|
20
|
-
readonly lastPage: number;
|
|
21
|
-
readonly hasPages: boolean;
|
|
22
|
-
readonly hasMorePages: boolean;
|
|
23
|
-
readonly isEmpty: boolean;
|
|
24
|
-
readonly total: number;
|
|
25
|
-
readonly hasTotal: boolean;
|
|
26
|
-
|
|
27
|
-
baseUrl(url: string): this;
|
|
28
|
-
queryString(values: { [key: string]: any }): this;
|
|
29
|
-
getUrl(page: number): string;
|
|
30
|
-
getMeta(): PaginationMeta;
|
|
31
|
-
getNextPageUrl(): string | null;
|
|
32
|
-
getPreviousPageUrl(): string | null;
|
|
33
|
-
getUrlsForRange(start: number, end: number): { url: string; page: number; isActive: boolean }[];
|
|
34
|
-
toJSON(): PaginationResult<Result>;
|
|
35
|
-
}
|