Skip to content

Commit

Permalink
Merge branch 'hotfix/2.1.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasrafael committed Jun 1, 2020
2 parents 015bcb7 + 5c7647d commit 4589201
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 46 deletions.
7 changes: 7 additions & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
exit: true # could be expressed as "no-exit: true"
recursive: true
reporter: spec
require:
- 'test/global.js'
slow: 5000
timeout: 5000 # same as "no-timeout: true" or "timeout: 0"
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ node_js:
- "10"
- "11"
- "12"
- "13"
- "14"
sudo: false
cache:
directories:
- node_modules
before_script:
- npm install && npm install coveralls mocha-lcov-reporter --save-dev
script:
Expand Down
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
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "query-strings-parser",
"version": "2.1.4",
"version": "2.1.5",
"description": "Middleware to transform query strings in a format that is recognized by the MongoDB, MySQL and other databases...",
"license": "MIT",
"main": "index.js",
"scripts": {
"test": "nyc --clean --all --reporter=text-summary mocha --opts test/mocha.opts test/**/*.spec.js",
"test:unit": "nyc --clean --all --reporter=text-summary mocha --opts test/mocha.opts test/unit/*.spec.js",
"test:integration": "nyc --clean --all --reporter=text-summary mocha --opts test/mocha.opts test/integration/*.spec.js",
"test:cov": "nyc --clean --all --reporter=html --reporter=text mocha --opts test/mocha.opts test/**/*.spec.js"
"test": "nyc --clean --all --reporter=text-summary mocha test/**/*.spec.js",
"test:unit": "nyc --clean --all --reporter=text-summary mocha test/unit/*.spec.js",
"test:integration": "nyc --clean --all --reporter=text-summary mocha test/integration/*.spec.js",
"test:cov": "nyc --clean --all --reporter=html --reporter=text mocha test/**/*.spec.js"
},
"directories": {
"lib": "lib",
Expand Down Expand Up @@ -44,8 +44,8 @@
"devDependencies": {
"chai": "^4.2.0",
"express": "^4.17.1",
"mocha": "^6.2.1",
"nyc": "^14.1.1",
"mocha": "^7.2.0",
"nyc": "^15.1.0",
"supertest": "^4.0.2"
}
}
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
6 changes: 0 additions & 6 deletions test/mocha.opts

This file was deleted.

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 4589201

Please sign in to comment.