Skip to content

Commit 5c7647d

Browse files
committed
Replaces asc and desc with 1 and -1 in queries with sorting.
1 parent 3fd94c1 commit 5c7647d

File tree

5 files changed

+25
-30
lines changed

5 files changed

+25
-30
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ app.get('/', (req, res) => {
3535
* Result (req.query):
3636
* {
3737
* fields: { name: 1, age: 1 },
38-
* sort: { created_at: 'asc' }
38+
* sort: { created_at: 1 }
3939
* filters: {},
4040
* pagination: {
4141
* skip: 10,
@@ -61,7 +61,7 @@ app.use(qs({
6161
},
6262
default: {
6363
fields: {name: 1 , age: 1, number: 1, _id: 0},
64-
sort: { created_at: 'desc' },
64+
sort: { created_at: -1 },
6565
filters: {},
6666
pagination: {
6767
page: 1,
@@ -75,7 +75,7 @@ app.use(qs({
7575
* Result (req.query):
7676
* {
7777
* fields: { name: 1, age: 1},
78-
* sort: { created_at: 'desc' }
78+
* sort: { created_at: -1 }
7979
* filters: {age: 30},
8080
* pagination: {
8181
* limit: 100,
@@ -127,7 +127,7 @@ console.log(qs.parseFields(query, {}, { use_page: true }))
127127
* Result:
128128
* {
129129
* fields: { name: 1, age: 1 },
130-
* sort: { created_at: 'asc' },
130+
* sort: { created_at: 1 },
131131
* filters: {},
132132
* pagination: { limit: 10, page: 1 },
133133
* original: '?fields=name,age&page=1&limit=10&sort=created_at'
@@ -166,9 +166,9 @@ console.log(qs.parseSort(query))
166166
/**
167167
* Result:
168168
* {
169-
* name: 'asc',
170-
* age: 'desc',
171-
* created_at: 'asc'
169+
* name: 1,
170+
* age: -1,
171+
* created_at: 1
172172
* }
173173
*/
174174
```

lib/mapper/ordination.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ function processQuery(query) {
1919
query = query.replace(/([^\w\s,-])|(\s{1,})/gi, '')
2020
query.split(',').forEach(function (elem) {
2121
elem = elem.trim()
22-
if (elem[0] === '-') result[elem.substr(1)] = 'desc'
23-
else result[elem] = 'asc'
22+
if (elem[0] === '-') result[elem.substr(1)] = -1
23+
else result[elem] = 1
2424
})
2525
return result
2626
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe('queryFilter()', function () {
7878

7979
context('when query contains ordination param', function () {
8080
it('should return req.query with set ordination params', function () {
81-
const expect_sort = {name: 'asc', age: 'desc'}
81+
const expect_sort = {name: 1, age: -1}
8282

8383
const options = JSON.parse(JSON.stringify(default_options))
8484
options.default.sort = expect_sort
@@ -88,6 +88,7 @@ describe('queryFilter()', function () {
8888
return request(app)
8989
.get(query)
9090
.then(res => {
91+
console.table(res.body)
9192
validate(res.body, options)
9293
})
9394
})

test/unit/index.spec.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,8 @@ describe('QueryString: Parsers', function () {
172172

173173
it('should return parsing query classification merged with custom classification', function () {
174174
const query = '?sort=name,-age,created_at'
175-
const result = index.parser(query, {sort: {_id: 'desc'}})
176-
verifySort(result.sort)
177-
expect(result.sort).to.have.property('_id', 'desc')
175+
const result = index.parser(query, {sort: {_id: -1}})
176+
expect(result.sort).to.have.property('_id', -1)
178177
})
179178

180179
it('should return parse query pagination', function () {
@@ -218,9 +217,9 @@ function verifySort(result) {
218217
expect(result).to.have.property('name')
219218
expect(result).to.have.property('age')
220219
expect(result).to.have.property('created_at')
221-
expect(result.name).to.eql('asc')
222-
expect(result.age).to.eql('desc')
223-
expect(result.created_at).to.eql('asc')
220+
expect(result.name).to.eql(1)
221+
expect(result.age).to.eql(-1)
222+
expect(result.created_at).to.eql(1)
224223
}
225224

226225
function verifyPage(result) {

test/unit/ordination.spec.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,24 @@ describe('QueryString: Ordination', function () {
5050

5151
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: 1}}}
5454
const result = ordination.sort({}, custom_options)
55-
expect(result).is.not.null
56-
expect(result).to.have.property('created_at')
57-
expect(result.created_at).to.eql('asc')
55+
expect(result.created_at).to.eql(1)
5856
})
5957

6058
it('should return a JSON with custom parameters and those of the query', function () {
61-
const custom_options = {default: {sort: {created_at: 'asc'}}}
59+
const custom_options = {default: {sort: {created_at: 1}}}
6260
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')
61+
expect(result.created_at).to.eql(-1)
62+
expect(result.age).to.eql(-1)
63+
expect(result.name).to.eql(1)
6664
})
6765
})
6866
})
6967

7068
function verify(result) {
71-
expect(result).to.have.property('name')
72-
expect(result).to.have.property('age')
73-
expect(result).to.have.property('created_at')
74-
expect(result.name).to.eql('desc')
75-
expect(result.age).to.eql('asc')
76-
expect(result.created_at).to.eql('asc')
69+
expect(result.name).to.eql(-1)
70+
expect(result.age).to.eql(1)
71+
expect(result.created_at).to.eql(1)
7772

7873
}

0 commit comments

Comments
 (0)