diff --git a/.mocharc.yml b/.mocharc.yml new file mode 100644 index 0000000..2770979 --- /dev/null +++ b/.mocharc.yml @@ -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" diff --git a/.travis.yml b/.travis.yml index e9285fa..231e3da 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/README.md b/README.md index 19dffba..86a0894 100644 --- a/README.md +++ b/README.md @@ -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, @@ -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, @@ -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, @@ -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' @@ -166,9 +166,9 @@ console.log(qs.parseSort(query)) /** * Result: * { -* name: 'asc', -* age: 'desc', -* created_at: 'asc' +* name: 1, +* age: -1, +* created_at: 1 * } */ ``` diff --git a/lib/mapper/ordination.js b/lib/mapper/ordination.js index 5062db3..68dbd15 100644 --- a/lib/mapper/ordination.js +++ b/lib/mapper/ordination.js @@ -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 } diff --git a/package.json b/package.json index 4ec8a63..585c782 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } } diff --git a/test/integration/index.default.config.spec.js b/test/integration/index.default.config.spec.js index 9c03a75..e5bfdaa 100644 --- a/test/integration/index.default.config.spec.js +++ b/test/integration/index.default.config.spec.js @@ -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 @@ -88,6 +88,7 @@ describe('queryFilter()', function () { return request(app) .get(query) .then(res => { + console.table(res.body) validate(res.body, options) }) }) diff --git a/test/mocha.opts b/test/mocha.opts deleted file mode 100644 index bd159c8..0000000 --- a/test/mocha.opts +++ /dev/null @@ -1,6 +0,0 @@ ---reporter spec ---require test/global.js ---recursive ---slow 5000 ---timeout 5000 ---exit \ No newline at end of file diff --git a/test/unit/index.spec.js b/test/unit/index.spec.js index 50db78e..8e39e5d 100644 --- a/test/unit/index.spec.js +++ b/test/unit/index.spec.js @@ -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 () { @@ -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) { diff --git a/test/unit/ordination.spec.js b/test/unit/ordination.spec.js index e2f51df..2092932 100644 --- a/test/unit/ordination.spec.js +++ b/test/unit/ordination.spec.js @@ -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) }