Skip to content

Commit 9190365

Browse files
committed
Merge branch 'hotfix/2.1.4'
2 parents d5d63ed + ecd20ec commit 9190365

File tree

8 files changed

+74
-33
lines changed

8 files changed

+74
-33
lines changed

lib/mapper/filters.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ function normalizeDate(date, isDateStart) {
229229
}
230230

231231
function validate_filters(_values, _default) {
232-
return Object.assign(_values, _default)
232+
return {
233+
..._default, ..._values
234+
}
233235
}
234236

235237
exports = module.exports = {

lib/mapper/ordination.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ function processQuery(query) {
2727

2828

2929
function validate_sort(_values, _default) {
30-
return Object.assign(_values, _default)
30+
return {
31+
..._default, ..._values
32+
}
3133
}
3234

3335
exports = module.exports = {

lib/mapper/pagination.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
function pagination(query, options) {
22
const result = {}
3-
43
result.limit = options.default.pagination.limit
54

65
if (query.limit) {
@@ -26,9 +25,7 @@ function processQuery(query) {
2625
if (query instanceof Array) {
2726
query = query[0]
2827
}
29-
query = query.replace(/([^\d\s])|(\s{1,})/gi, '')
30-
if (parseInt(query)) return (parseInt(query))
31-
return undefined
28+
return parseInt(query.replace(/([^\d\s])|(\s{1,})/gi, ''))
3229
}
3330

3431
exports = module.exports = {

test/integration/index.custom.config.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('queryFilter()', function () {
8383
})
8484
})
8585

86-
context('when use pagination with page and query page is not a number', function () {
86+
context('when use pagination with page and query page and limit is not a number', function () {
8787
it('should return pagination param with default limit', function () {
8888

8989
const expect_pagination = {
@@ -94,7 +94,7 @@ describe('queryFilter()', function () {
9494
const options = JSON.parse(JSON.stringify(custom_options))
9595
options.default.pagination = expect_pagination
9696

97-
const query = '?limit=teen'
97+
const query = '?page=current&limit=teen'
9898

9999
return request(app)
100100
.get(query)

test/unit/fields.spec.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,39 @@ describe('QueryString: Fields', function () {
55

66
context('when query fields are a simple string', function () {
77
it('should return a JSON with field params', function (done) {
8-
const query = { fields: 'name,age,created_at' }
8+
const query = {fields: 'name,age,created_at'}
99
verify(fields.fields(query, default_options))
1010
done()
1111
})
1212
})
1313

1414
context('when query fields are an array of strings', function () {
1515
it('should return a JSON with field params', function (done) {
16-
const query = { fields: ['name,age', 'created_at'] }
16+
const query = {fields: ['name,age', 'created_at']}
1717
verify(fields.fields(query, default_options))
1818
done()
1919
})
2020
})
2121

2222
context('when there are blank spaces between query fields', function () {
2323
it('should return a JSON with field params, ignoring the blank space', function (done) {
24-
const query = { fields: ' name , name, age , created_at' }
24+
const query = {fields: ' name , name, age , created_at'}
2525
verify(fields.fields(query, default_options))
2626
done()
2727
})
2828
})
2929

3030
context('when there are null fields in query fields', function () {
3131
it('should return a JSON with field params, ignoring the null fields', function (done) {
32-
const query = { fields: ',,name,,,age,,,,,created_at,,' }
32+
const query = {fields: ',,name,,,age,,,,,created_at,,'}
3333
verify(fields.fields(query, default_options))
3434
done()
3535
})
3636
})
3737

3838
context('when there are special characters in query fields', function () {
3939
it('should return a JSON with field params, ignoring the special characteres', function (done) {
40-
const query = { fields: ' ,,, ^ & * ( ´) @!n@a"m "e,$%ag" e",created _a t ' }
40+
const query = {fields: ' ,,, ^ & * ( ´) @!n@a"m "e,$%ag" e",created _a t '}
4141
verify(fields.fields(query, default_options))
4242
done()
4343

@@ -52,17 +52,21 @@ describe('QueryString: Fields', function () {
5252
})
5353
})
5454

55-
context('when use custom params without query', function () {
55+
context('when use custom params', function () {
5656
it('should return a JSON with custom params', function () {
57-
const custom_options = { default: { fields: { name: 1, age: 1, _id: 0 } } }
57+
const custom_options = {default: {fields: {name: 1, age: 1, _id: 0}}}
5858
const result = fields.fields({}, custom_options)
59-
expect(result).to.have.property('name')
60-
expect(result).to.have.property('age')
61-
expect(result).to.have.property('_id')
6259
expect(result.name).to.eql(custom_options.default.fields.name)
6360
expect(result.age).to.eql(custom_options.default.fields.age)
6461
expect(result._id).to.eql(custom_options.default.fields._id)
62+
})
6563

64+
it('should return a JSON with custom params and those of the query', function () {
65+
const custom_options = {default: {fields: {name: 1, _id: 1}}}
66+
const result = fields.fields({fields: 'age'}, custom_options)
67+
expect(result.name).to.eql(custom_options.default.fields.name)
68+
expect(result._id).to.eql(custom_options.default.fields._id)
69+
expect(result.age).to.eql(1)
6670
})
6771
})
6872
})

test/unit/filters.spec.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,24 @@ describe('QueryString: Filters', function () {
3131
})
3232
})
3333

34-
context('when use the default options without query', function () {
34+
context('when use the default options', function () {
3535
it('should return a JSON with default filters params', function (done) {
3636
const result = filter.filters({}, default_options)
3737
expect(result).to.eql(default_options.default.filters)
3838
done()
3939
})
40+
41+
it('should return a JSON with default filters params and those of the query', function (done) {
42+
const options = {
43+
...default_options, ...{
44+
default: {filters: {age: '19', name: 'John'}}
45+
}
46+
}
47+
const result = filter.filters({age: '21'}, options)
48+
expect(result.name).to.eql(options.default.filters.name)
49+
expect(result.age).to.eql(21)
50+
done()
51+
})
4052
})
4153
})
4254

@@ -45,4 +57,4 @@ function verify(result) {
4557
expect(result).to.have.property('age')
4658
expect(result.name).to.eql('lucas')
4759
expect(result.age).to.eql(30)
48-
}
60+
}

test/unit/ordination.spec.js

+18-11
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,42 @@ const expect = require('chai').expect
22
const ordination = require('../../lib/mapper/ordination')
33

44
describe('QueryString: Ordination', function () {
5-
5+
66
context('when ordination query is a simple string', function () {
77
it('should return a JSON with order params', function (done) {
8-
verify(ordination.sort({ sort: '-name,age,created_at' }, default_options))
8+
verify(ordination.sort({sort: '-name,age,created_at'}, default_options))
99
done()
1010
})
1111
})
1212

1313
context('when ordination query is an array of strings', function () {
1414
it('should return a JSON with order params', function (done) {
15-
verify(ordination.sort({ sort: ['-name,age', 'created_at'] }, default_options))
15+
verify(ordination.sort({sort: ['-name,age', 'created_at']}, default_options))
1616
done()
1717
})
1818
})
1919

2020
context('when there are blank spaces between ordination query', function () {
2121
it('should return a JSON with order params, ignoring the blank space', function (done) {
22-
verify(ordination.sort({ sort: '-na m e, age, cr eat ed_at' }, default_options))
22+
verify(ordination.sort({sort: '-na m e, age, cr eat ed_at'}, default_options))
2323
done()
2424
})
2525
})
2626

2727
context('when there are null fields in ordination query', function () {
2828
it('should return a JSON with order params, ignoring the null fields', function (done) {
29-
verify(ordination.sort({ sort: ',,,,,-name,,,,age,,,created_at,,,,,,,' }, default_options))
29+
verify(ordination.sort({sort: ',,,,,-name,,,,age,,,created_at,,,,,,,'}, default_options))
3030
done()
3131
})
3232
})
3333

3434
context('when there are special characters in ordination query', function () {
3535
it('should return a JSON with order params, ignoring the special characteres', function (done) {
36-
verify(ordination.sort({ sort: '-$%n@am#$e??,!!ag%e,c***r$@$eated_at' }, default_options))
36+
verify(ordination.sort({sort: '-$%n@am#$e??,!!ag%e,c***r$@$eated_at'}, default_options))
3737
done()
3838
})
3939

40-
})
40+
})
4141

4242
context('when use the default options without query', function () {
4343
it('should return a JSON with default ordination params', function (done) {
@@ -48,16 +48,23 @@ describe('QueryString: Ordination', function () {
4848
})
4949
})
5050

51-
context('when use custom params without query', function () {
51+
context('when use custom params', function () {
5252
it('should return a JSON with custom params', function () {
53-
const custom_options = { default: { sort: { created_at: 'asc' }}}
53+
const custom_options = {default: {sort: {created_at: 'asc'}}}
5454
const result = ordination.sort({}, custom_options)
5555
expect(result).is.not.null
5656
expect(result).to.have.property('created_at')
5757
expect(result.created_at).to.eql('asc')
5858
})
59-
})
6059

60+
it('should return a JSON with custom parameters and those of the query', function () {
61+
const custom_options = {default: {sort: {created_at: 'asc'}}}
62+
const result = ordination.sort({sort: '-created_at,-age,name'}, custom_options)
63+
expect(result.created_at).to.eql('desc')
64+
expect(result.age).to.eql('desc')
65+
expect(result.name).to.eql('asc')
66+
})
67+
})
6168
})
6269

6370
function verify(result) {
@@ -68,4 +75,4 @@ function verify(result) {
6875
expect(result.age).to.eql('asc')
6976
expect(result.created_at).to.eql('asc')
7077

71-
}
78+
}

test/unit/pagination.spec.js

+19-2
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)