Skip to content

Commit 74e7bc1

Browse files
committed
Corrects error in pagination when defaults values are use
1 parent 7455cac commit 74e7bc1

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

lib/mapper/pagination.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ function pagination(query, options) {
44
result.limit = options.default.pagination.limit
55

66
if (query.limit) {
7-
result.limit = processQuery(query.limit) || options.default.pagination.limit
7+
result.limit = processQuery(query.limit)
88
}
99

1010
if (options.use_page) {
1111
result.page = options.default.pagination.page
1212
if (query.page) {
13-
result.page = processQuery(query.page) || options.default.pagination.page
13+
result.page = processQuery(query.page)
1414
}
1515
} else {
1616
result.skip = options.default.pagination.skip
1717
if (query.skip) {
18-
result.skip = processQuery(query.skip) || options.default.pagination.skip
18+
result.skip = processQuery(query.skip)
1919
}
2020
}
2121

@@ -26,9 +26,7 @@ function processQuery(query) {
2626
if (query instanceof Array) {
2727
query = query[0]
2828
}
29-
query = query.replace(/([^\d\s])|(\s{1,})/gi, '')
30-
if (parseInt(query)) return (parseInt(query))
31-
return undefined
29+
return parseInt(query.replace(/([^\d\s])|(\s{1,})/gi, ''))
3230
}
3331

3432
exports = module.exports = {

test/unit/pagination.spec.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ describe('QueryString: Pagination', function () {
164164
})
165165
})
166166

167-
context('when use custom options without query with limit and page params', function () {
167+
context('when use custom params', function () {
168168
it('should return a JSON with custom params', function (done) {
169169
const custom_options = {
170170
default: {
@@ -175,12 +175,29 @@ describe('QueryString: Pagination', function () {
175175
},
176176
use_page: true
177177
}
178-
const result = pagination.pagination({page: 'one'}, custom_options)
178+
const result = pagination.pagination({}, custom_options)
179179
verifyPage(result)
180180
expect(result.limit).to.eql(15)
181181
expect(result.page).to.eql(2)
182182
done()
183183
})
184+
185+
it('should return a JSON with custom parameters and those of the query', function (done) {
186+
const custom_options = {
187+
default: {
188+
pagination: {
189+
limit: 50,
190+
page: 5
191+
}
192+
},
193+
use_page: true
194+
}
195+
const result = pagination.pagination({page: '3', limit: '10'}, custom_options)
196+
verifyPage(result)
197+
expect(result.limit).to.eql(10)
198+
expect(result.page).to.eql(3)
199+
done()
200+
})
184201
})
185202
})
186203

0 commit comments

Comments
 (0)