@aria-framework/kit 0.3.0 → 0.3.1
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/package.json +24 -24
- package/paginate.js +124 -97
package/package.json
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@aria-framework/kit",
|
|
3
|
-
"version": "0.3.
|
|
4
|
-
"description": "Aria App Framework — kit module. Small dependency-free server utilities: open-redirect guard (safeReturnTo), SQL LIKE escaping, magic-byte upload validation (fileSniff), Crockford base32 tracking IDs, and person-name compose/split helpers.",
|
|
5
|
-
"license": "UNLICENSED",
|
|
6
|
-
"private": false,
|
|
7
|
-
"publishConfig": {
|
|
8
|
-
"access": "public"
|
|
9
|
-
},
|
|
10
|
-
"main": "index.js",
|
|
11
|
-
"files": [
|
|
12
|
-
"index.js",
|
|
13
|
-
"safeReturnTo.js",
|
|
14
|
-
"likeEscape.js",
|
|
15
|
-
"isEmail.js",
|
|
16
|
-
"fileSniff.js",
|
|
17
|
-
"trackingId.js",
|
|
18
|
-
"personName.js",
|
|
19
|
-
"paginate.js"
|
|
20
|
-
],
|
|
21
|
-
"scripts": {
|
|
22
|
-
"test": "node test/smoke.js && node test/paginate.js"
|
|
23
|
-
}
|
|
24
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@aria-framework/kit",
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "Aria App Framework — kit module. Small dependency-free server utilities: open-redirect guard (safeReturnTo), SQL LIKE escaping, magic-byte upload validation (fileSniff), Crockford base32 tracking IDs, and person-name compose/split helpers.",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"private": false,
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"main": "index.js",
|
|
11
|
+
"files": [
|
|
12
|
+
"index.js",
|
|
13
|
+
"safeReturnTo.js",
|
|
14
|
+
"likeEscape.js",
|
|
15
|
+
"isEmail.js",
|
|
16
|
+
"fileSniff.js",
|
|
17
|
+
"trackingId.js",
|
|
18
|
+
"personName.js",
|
|
19
|
+
"paginate.js"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"test": "node test/smoke.js && node test/paginate.js"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/paginate.js
CHANGED
|
@@ -1,97 +1,124 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Page arithmetic. Pure — no DB, no request, no opinion about how the rows are fetched.
|
|
3
|
-
*
|
|
4
|
-
* Every list in these apps that pages has re-derived this, and the parts that get quietly
|
|
5
|
-
* wrong are always the same three:
|
|
6
|
-
*
|
|
7
|
-
* the clamp `pageSize` arrives from a query string. Unclamped, `?pageSize=100000` asks
|
|
8
|
-
* the database for everything, which is a denial of service written by the
|
|
9
|
-
* person browsing.
|
|
10
|
-
* the last page deleting rows can leave you on page 9 of 4. Returning an empty list there
|
|
11
|
-
* looks like "no records" rather than "you are past the end".
|
|
12
|
-
* the window a list long enough to need paging is long enough that rendering every page
|
|
13
|
-
* link is the same problem as rendering every row. 200 pages is 200 links.
|
|
14
|
-
*
|
|
15
|
-
* So those three are here, once.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
'use strict';
|
|
19
|
-
|
|
20
|
-
const DEFAULT_SIZES = [25, 50, 100, 200];
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
//
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Page arithmetic. Pure — no DB, no request, no opinion about how the rows are fetched.
|
|
3
|
+
*
|
|
4
|
+
* Every list in these apps that pages has re-derived this, and the parts that get quietly
|
|
5
|
+
* wrong are always the same three:
|
|
6
|
+
*
|
|
7
|
+
* the clamp `pageSize` arrives from a query string. Unclamped, `?pageSize=100000` asks
|
|
8
|
+
* the database for everything, which is a denial of service written by the
|
|
9
|
+
* person browsing.
|
|
10
|
+
* the last page deleting rows can leave you on page 9 of 4. Returning an empty list there
|
|
11
|
+
* looks like "no records" rather than "you are past the end".
|
|
12
|
+
* the window a list long enough to need paging is long enough that rendering every page
|
|
13
|
+
* link is the same problem as rendering every row. 200 pages is 200 links.
|
|
14
|
+
*
|
|
15
|
+
* So those three are here, once.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
'use strict';
|
|
19
|
+
|
|
20
|
+
const DEFAULT_SIZES = [25, 50, 100, 200];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A strict integer, or null.
|
|
24
|
+
*
|
|
25
|
+
* parseInt is wrong for query-string input in two ways that both fail QUIETLY: it truncates
|
|
26
|
+
* ('12abc' -> 12, '1e3' -> 1), so garbage becomes a different page rather than a rejected one;
|
|
27
|
+
* and given the array Express produces for a repeated parameter it silently reads the FIRST
|
|
28
|
+
* element, so `?pageSize=200&pageSize=25` keeps 200 and the picker appears not to work.
|
|
29
|
+
*/
|
|
30
|
+
function intOrNull(v) {
|
|
31
|
+
if (Array.isArray(v)) v = v[v.length - 1]; // a repeat means "the latest wins"
|
|
32
|
+
if (typeof v === 'number') return Number.isInteger(v) ? v : null;
|
|
33
|
+
if (typeof v !== 'string' || !/^-?\d+$/.test(v.trim())) return null;
|
|
34
|
+
const n = Number(v.trim());
|
|
35
|
+
return Number.isInteger(n) ? n : null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {object} opts
|
|
40
|
+
* @param {number|string} [opts.page=1] 1-based; garbage becomes 1
|
|
41
|
+
* @param {number|string} [opts.pageSize] clamped to `sizes`; defaults to the first
|
|
42
|
+
* @param {number} opts.total total matching rows
|
|
43
|
+
* @param {number[]} [opts.sizes] the allowed page sizes, smallest first
|
|
44
|
+
* @param {number} [opts.window=2] page links either side of the current one
|
|
45
|
+
* @returns {{page, pageSize, pages, total, offset, limit, from, to, isFirst, isLast,
|
|
46
|
+
* sizes, numbers, needed}}
|
|
47
|
+
*/
|
|
48
|
+
function paginate({ page, pageSize, total, sizes = DEFAULT_SIZES, window = 2 } = {}) {
|
|
49
|
+
const allowed = (Array.isArray(sizes) && sizes.length ? sizes : DEFAULT_SIZES)
|
|
50
|
+
.map(Number).filter((n) => Number.isFinite(n) && n > 0).sort((a, b) => a - b);
|
|
51
|
+
|
|
52
|
+
const t = Math.max(0, Math.floor(Number(total) || 0));
|
|
53
|
+
|
|
54
|
+
// CLAMPED TO THE ALLOWED SET, not merely bounded. A max-only check still lets `?pageSize=99`
|
|
55
|
+
// through, and then the size dropdown shows nothing selected — the control and the data
|
|
56
|
+
// disagree, which is the kind of small wrongness nobody reports and everybody notices.
|
|
57
|
+
// `intOrNull`, not parseInt: parseInt('12abc') is 12 and parseInt('1e3') is 1, so garbage
|
|
58
|
+
// becomes a DIFFERENT page rather than a rejected one. And a repeated query parameter
|
|
59
|
+
// arrives as an array — Express gives ['200','25'] for `?pageSize=200&pageSize=25` — where
|
|
60
|
+
// parseInt returns 200, silently ignoring the newer choice.
|
|
61
|
+
const size = allowed.indexOf(intOrNull(pageSize)) !== -1 ? intOrNull(pageSize) : allowed[0];
|
|
62
|
+
|
|
63
|
+
const pages = Math.max(1, Math.ceil(t / size));
|
|
64
|
+
|
|
65
|
+
// PAST THE END COMES BACK TO THE END. Deleting rows while someone is on page 9 of 4 must
|
|
66
|
+
// show them the last page, not an empty one that reads as "no records".
|
|
67
|
+
const asked = intOrNull(page);
|
|
68
|
+
const current = Math.min(Math.max(1, asked === null ? 1 : asked), pages);
|
|
69
|
+
|
|
70
|
+
const offset = (current - 1) * size;
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
page: current,
|
|
74
|
+
pageSize: size,
|
|
75
|
+
pages,
|
|
76
|
+
total: t,
|
|
77
|
+
offset,
|
|
78
|
+
limit: size,
|
|
79
|
+
// 1-based inclusive, for "showing 26–50 of 1,204". `from` is 0 on an empty list so the
|
|
80
|
+
// caller can say "no records" rather than "showing 1–0 of 0".
|
|
81
|
+
from: t === 0 ? 0 : offset + 1,
|
|
82
|
+
to: Math.min(offset + size, t),
|
|
83
|
+
isFirst: current === 1,
|
|
84
|
+
isLast: current === pages,
|
|
85
|
+
sizes: allowed,
|
|
86
|
+
numbers: windowed(current, pages, window),
|
|
87
|
+
// Worth showing the controls at all? Two separate questions, because they have two
|
|
88
|
+
// different answers and conflating them renders a pager whose only content is one
|
|
89
|
+
// disabled page number.
|
|
90
|
+
//
|
|
91
|
+
// needed there is more than one page AT THIS SIZE — i.e. anything to navigate
|
|
92
|
+
// resizable the list is longer than the smallest size, so the picker can still do
|
|
93
|
+
// something even when this page shows everything (200-per-page over 100 rows)
|
|
94
|
+
needed: pages > 1,
|
|
95
|
+
resizable: t > allowed[0]
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The page links to render: `[1, null, 4, 5, 6, 7, 8, null, 20]`, where null is a gap.
|
|
101
|
+
*
|
|
102
|
+
* First and last are always present so the ends stay reachable in one click — the case that
|
|
103
|
+
* matters is "jump to the oldest", which is otherwise a long walk.
|
|
104
|
+
*/
|
|
105
|
+
function windowed(current, pages, window) {
|
|
106
|
+
const w = Math.max(0, Math.floor(Number(window) || 0));
|
|
107
|
+
const keep = new Set([1, pages]);
|
|
108
|
+
for (let i = current - w; i <= current + w; i++) if (i >= 1 && i <= pages) keep.add(i);
|
|
109
|
+
|
|
110
|
+
const sorted = [...keep].sort((a, b) => a - b);
|
|
111
|
+
const out = [];
|
|
112
|
+
let prev = 0;
|
|
113
|
+
for (const n of sorted) {
|
|
114
|
+
// A gap of exactly one is silly — "1 … 3" is longer than "1 2 3" and harder to click.
|
|
115
|
+
if (prev && n - prev > 1) out.push(n - prev === 2 ? prev + 1 : null);
|
|
116
|
+
// No duplicate check needed: `sorted` comes from a Set, and the only other push is
|
|
117
|
+
// `prev + 1` in the gap-of-two branch, where n === prev + 2.
|
|
118
|
+
out.push(n);
|
|
119
|
+
prev = n;
|
|
120
|
+
}
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
module.exports = { paginate, DEFAULT_SIZES };
|