Skip to content

Commit

Permalink
Replaces asc and desc with 1 and -1 in queries with sorting.
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasrafael committed Jun 1, 2020
1 parent 3fd94c1 commit 5c7647d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 30 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ app.get('/', (req, res) => {
* Result (req.query):
* {
* fields: { name: 1, age: 1 },
* sort: { created_at: 'asc' }
* sort: { created_at: 1 }
* filters: {},
* pagination: {
* skip: 10,
Expand All @@ -61,7 +61,7 @@ app.use(qs({
},
default: {
fields: {name: 1 , age: 1, number: 1, _id: 0},
sort: { created_at: 'desc' },
sort: { created_at: -1 },
filters: {},
pagination: {
page: 1,
Expand All @@ -75,7 +75,7 @@ app.use(qs({
* Result (req.query):
* {
* fields: { name: 1, age: 1},
* sort: { created_at: 'desc' }
* sort: { created_at: -1 }
* filters: {age: 30},
* pagination: {
* limit: 100,
Expand Down Expand Up @@ -127,7 +127,7 @@ console.log(qs.parseFields(query, {}, { use_page: true }))
* Result:
* {
* fields: { name: 1, age: 1 },
* sort: { created_at: 'asc' },
* sort: { created_at: 1 },
* filters: {},
* pagination: { limit: 10, page: 1 },
* original: '?fields=name,age&page=1&limit=10&sort=created_at'
Expand Down Expand Up @@ -166,9 +166,9 @@ console.log(qs.parseSort(query))
/**
* Result:
* {
* name: 'asc',
* age: 'desc',
* created_at: 'asc'
* name: 1,
* age: -1,
* created_at: 1
* }
*/
```
Expand Down
4 changes: 2 additions & 2 deletions lib/mapper/ordination.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ function processQuery(query) {
query = query.replace(/([^\w\s,-])|(\s{1,})/gi, '')
query.split(',').forEach(function (elem) {
elem = elem.trim()
if (elem[0] === '-') result[elem.substr(1)] = 'desc'
else result[elem] = 'asc'
if (elem[0] === '-') result[elem.substr(1)] = -1
else result[elem] = 1
})
return result
}
Expand Down
3 changes: 2 additions & 1 deletion test/integration/index.default.config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('queryFilter()', function () {

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

const options = JSON.parse(JSON.stringify(default_options))
options.default.sort = expect_sort
Expand All @@ -88,6 +88,7 @@ describe('queryFilter()', function () {
return request(app)
.get(query)
.then(res => {
console.table(res.body)
validate(res.body, options)
})
})
Expand Down
11 changes: 5 additions & 6 deletions test/unit/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,8 @@ describe('QueryString: Parsers', function () {

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

it('should return parse query pagination', function () {
Expand Down Expand Up @@ -218,9 +217,9 @@ function verifySort(result) {
expect(result).to.have.property('name')
expect(result).to.have.property('age')
expect(result).to.have.property('created_at')
expect(result.name).to.eql('asc')
expect(result.age).to.eql('desc')
expect(result.created_at).to.eql('asc')
expect(result.name).to.eql(1)
expect(result.age).to.eql(-1)
expect(result.created_at).to.eql(1)
}

function verifyPage(result) {
Expand Down
23 changes: 9 additions & 14 deletions test/unit/ordination.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,24 @@ describe('QueryString: Ordination', function () {

context('when use custom params', function () {
it('should return a JSON with custom params', function () {
const custom_options = {default: {sort: {created_at: 'asc'}}}
const custom_options = {default: {sort: {created_at: 1}}}
const result = ordination.sort({}, custom_options)
expect(result).is.not.null
expect(result).to.have.property('created_at')
expect(result.created_at).to.eql('asc')
expect(result.created_at).to.eql(1)
})

it('should return a JSON with custom parameters and those of the query', function () {
const custom_options = {default: {sort: {created_at: 'asc'}}}
const custom_options = {default: {sort: {created_at: 1}}}
const result = ordination.sort({sort: '-created_at,-age,name'}, custom_options)
expect(result.created_at).to.eql('desc')
expect(result.age).to.eql('desc')
expect(result.name).to.eql('asc')
expect(result.created_at).to.eql(-1)
expect(result.age).to.eql(-1)
expect(result.name).to.eql(1)
})
})
})

function verify(result) {
expect(result).to.have.property('name')
expect(result).to.have.property('age')
expect(result).to.have.property('created_at')
expect(result.name).to.eql('desc')
expect(result.age).to.eql('asc')
expect(result.created_at).to.eql('asc')
expect(result.name).to.eql(-1)
expect(result.age).to.eql(1)
expect(result.created_at).to.eql(1)

}

0 comments on commit 5c7647d

Please sign in to comment.