-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
index.js
125 lines (90 loc) · 3.62 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// express-paginate
// Copyright (c) 2014- Nick Baugh <[email protected]> (http://niftylettuce.com)
// MIT Licensed
// Node.js pagination middleware and view helpers.
// * Author: [@niftylettuce](https://twitter.com/#!/niftylettuce)
// * Source: <https://github.com/niftylettuce/express-paginate>
var qs = require('qs');
var url = require('url');
var assign = require('lodash.assign');
var clone = require('lodash.clone');
var isObject = require('lodash.isobject');
var util = require('util');
exports = module.exports;
exports.href = function href(req) {
return function(prev, params) {
var query = clone(req.query);
if (typeof prev === 'object') {
params = prev;
prev = false;
} else {
prev = (typeof prev === 'boolean') ? prev : false;
query.page = parseInt(query.page, 10);
query.page = prev ? query.page-= 1 : query.page += 1;
query.page = (query.page < 1) ? 1 : query.page;
}
// allow overriding querystring params
// (useful for sorting and filtering)
// another alias for `_.assign` is `_.extend`
if (isObject(params))
query = assign(query, params);
return url.parse(req.originalUrl).pathname + '?' + qs.stringify(query);
};
};
exports.hasNextPages = function hasNextPages(req) {
return function(pageCount) {
if (typeof pageCount !== 'number' || pageCount < 0)
throw new Error('express-paginate: `pageCount` is not a number >= 0');
return req.query.page < pageCount;
};
};
exports.getArrayPages = function(req) {
return function(limit, pageCount, currentPage) {
var maxPage = pageCount;
// limit default is 3
limit = limit || 3;
if (typeof limit !== 'number' || limit < 0)
throw new Error('express-paginate: `limit` is not a number >= 0');
if (typeof pageCount !== 'number' || pageCount < 0)
throw new Error('express-paginate: `pageCount` is not a number >= 0');
currentPage = parseInt(currentPage, 10);
if (Number.isNaN(currentPage) || currentPage < 0)
throw new Error('express-paginate: `currentPage` is not a number >= 0');
if (limit > 0) {
var end = Math.min(Math.max(currentPage + Math.floor(limit / 2), limit), pageCount);
var start = Math.max(1, (currentPage < (limit - 1)) ? 1 : (end - limit) + 1);
var pages = [];
for (var i = start; i <= end; i++) {
pages.push({
number: i,
url: exports.href(req)()
.replace('page=' + (currentPage + 1), 'page=' + i)
});
}
return pages;
}
}
}
exports.middleware = function middleware(limit, maxLimit) {
var _limit = (typeof limit === 'number') ? parseInt(limit, 10) : 10;
var _maxLimit = (typeof maxLimit === 'number') ? parseInt(maxLimit, 10) : 50;
return function _middleware(req, res, next) {
req.query.page = (typeof req.query.page === 'string') ? parseInt(req.query.page, 10) || 1 : 1;
req.query.limit = (typeof req.query.limit === 'string') ? parseInt(req.query.limit, 10) || 0 : _limit;
if (req.query.limit > _maxLimit)
req.query.limit = _maxLimit;
if (req.query.page < 1)
req.query.page = 1;
if (req.query.limit < 0)
req.query.limit = 0;
req.skip = req.offset = (req.query.page * req.query.limit) - req.query.limit;
res.locals.paginate = {};
res.locals.paginate.page = req.query.page;
res.locals.paginate.limit = req.query.limit;
res.locals.paginate.href = exports.href(req);
res.locals.paginate.hasPreviousPages = req.query.page > 1;
res.locals.paginate.hasNextPages = exports.hasNextPages(req);
res.locals.paginate.getArrayPages = exports.getArrayPages(req);
next();
};
};