Skip to content

Commit

Permalink
Merge branch 'hotfix/2.1.10'
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffsampaio committed Dec 3, 2021
2 parents 40f5f31 + 4b482fa commit a3a2c71
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 107 deletions.
13 changes: 9 additions & 4 deletions lib/mapper/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,15 @@ function buildRegEx(value) {

function addAccentsRegex(string) {
return string.replace(/a/g, '[a,á,à,ä,â,ã]')
.replace(/e/g, '[e,é,ë,ê]')
.replace(/i/g, '[i,í,ï]')
.replace(/o/g, '[o,ó,ö,ò,ô]')
.replace(/u/g, '[u,ü,ú,ù]')
.replace(/A/g, '[A,Á,À,Ä,Â,Ã]')
.replace(/e/g, '[e,é,ë,ê]')
.replace(/E/g, '[E,É,Ë,Ê]')
.replace(/i/g, '[i,í,ï]')
.replace(/I/g, '[I,Í,Ï]')
.replace(/o/g, '[o,ó,ö,ò,ô]')
.replace(/O/g, '[O,Ó,Ö,Ò,Ô]')
.replace(/u/g, '[u,ü,ú,ù]')
.replace(/U/g, '[U,Ü,Ú,Ù]')
}

function parseDate(query, options) {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "query-strings-parser",
"version": "2.1.9",
"version": "2.1.10",
"description": "Middleware to transform query strings in a format that is recognized by the MongoDB, MySQL and other databases...",
"license": "MIT",
"main": "index.js",
Expand Down Expand Up @@ -47,8 +47,8 @@
"devDependencies": {
"chai": "^4.3.4",
"express": "^4.17.1",
"mocha": "^9.0.3",
"mocha": "^9.1.3",
"nyc": "^15.1.0",
"supertest": "^6.1.4"
"supertest": "^6.1.6"
}
}
94 changes: 47 additions & 47 deletions test/integration/index.default.config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('queryFilter()', function () {

context('when query contains pagination param with string limit as skip', function () {
it('should return req.query with set pagination params', function () {
const expect_pagination = {limit: Number.MAX_SAFE_INTEGER, skip: 2}
const expect_pagination = { limit: Number.MAX_SAFE_INTEGER, skip: 2 }

const options = JSON.parse(JSON.stringify(default_options))
options.default.pagination.limit = expect_pagination.limit
Expand All @@ -60,7 +60,7 @@ describe('queryFilter()', function () {

context('when query contains pagination param with string skip', function () {
it('should return req.query with set pagination params', function () {
const expect_pagination = {limit: 10, skip: 0}
const expect_pagination = { limit: 10, skip: 0 }

const options = JSON.parse(JSON.stringify(default_options))
options.default.pagination.limit = expect_pagination.limit
Expand All @@ -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: 1, age: -1}
const expect_sort = { name: 1, age: -1 }

const options = JSON.parse(JSON.stringify(default_options))
options.default.sort = expect_sort
Expand All @@ -95,7 +95,7 @@ describe('queryFilter()', function () {

context('when query contains fields param', function () {
it('should return req.query with set field params', function () {
const expect_fields = {name: 1, age: 1}
const expect_fields = { name: 1, age: 1 }

const options = JSON.parse(JSON.stringify(default_options))
options.default.fields = expect_fields
Expand All @@ -112,7 +112,7 @@ describe('queryFilter()', function () {

context('when query contains simple filters param', function () {
it('should return req.query with set field params', function () {
const expect_filters = {name: 'lucas', age: 30}
const expect_filters = { name: 'lucas', age: 30 }

const options = JSON.parse(JSON.stringify(default_options))
options.default.filters = expect_filters
Expand All @@ -138,17 +138,17 @@ describe('queryFilter()', function () {
name: { '$options': 'i', '$regex': 'd[o,ó,ö,ò,ô][u,ü,ú,ù]gl[a,á,à,ä,â,ã]s$' }
},
{
name: { '$options': 'i', '$regex': 'j[o,ó,ö,ò,ô]rg[e,é,ë,ê]' }
name: { '$options': 'i', '$regex': 'J[O,Ó,Ö,Ò,Ô]RG[E,É,Ë,Ê]' }
}],
'school.name': 'UEPB',
'timestamp': '2018-12-05T00:00:00',
'$or': [{job: 'Developer'}, {job: 'Engineer'}]
'$or': [{ job: 'Developer' }, { job: 'Engineer' }]
}

const options = JSON.parse(JSON.stringify(default_options))
options.default.filters = expect_filters

const query = '?name=lucas****&name=*****douglas&name=*****jorge*****&.school.name.=UEPB'
const query = '?name=lucas****&name=*****douglas&name=*****JORGE*****&.school.name.=UEPB'
.concat('&timestamp=2018-12-05&job=Developer,Engineer')

return request(app)
Expand All @@ -163,9 +163,9 @@ describe('queryFilter()', function () {
it('should return req.query with set field params', function () {
const expect_filters = {
name: 'lucas',
age: {$gt: 30},
timestamp: {$gt: '2018-12-05T00:00:00'},
created_at: {$lte: '2018-12-06T00:00:00'},
age: { $gt: 30 },
timestamp: { $gt: '2018-12-05T00:00:00' },
created_at: { $lte: '2018-12-06T00:00:00' },
sleep_hour: '22:40'
}

Expand All @@ -185,10 +185,10 @@ describe('queryFilter()', function () {
it('should return req.query with set field params (with $ne filter)', function () {
const expect_filters = {
name: 'lucas',
age: {$gt: 30},
type: {$ne: 'admin'},
timestamp: {$gt: '2018-12-05T00:00:00'},
created_at: {$lte: '2018-12-06T00:00:00'},
age: { $gt: 30 },
type: { $ne: 'admin' },
timestamp: { $gt: '2018-12-05T00:00:00' },
created_at: { $lte: '2018-12-06T00:00:00' },
sleep_hour: '22:40'
}

Expand All @@ -210,8 +210,8 @@ describe('queryFilter()', function () {
it('should return req.query with set start_at params as today', function () {
const expect_filters = {
$and: [
{created_at: {$lt: normalizeDate(dateToString(new Date()), false)}},
{created_at: {$gte: normalizeDate(dateToString(new Date()), true)}}
{ created_at: { $lt: normalizeDate(dateToString(new Date()), false) } },
{ created_at: { $gte: normalizeDate(dateToString(new Date()), true) } }
]
}

Expand All @@ -230,8 +230,8 @@ describe('queryFilter()', function () {
it('should return req.query with set end_at params as today', function () {
const expect_filters = {
$and: [
{created_at: {$lt: normalizeDate(dateToString(new Date()), false)}},
{created_at: {$gte: '2019-02-05T00:00:00'}}
{ created_at: { $lt: normalizeDate(dateToString(new Date()), false) } },
{ created_at: { $gte: '2019-02-05T00:00:00' } }
]
}

Expand All @@ -250,8 +250,8 @@ describe('queryFilter()', function () {
it('should return req.query with set start_at params as date', function () {
const expect_filters = {
$and: [
{created_at: {$lt: normalizeDate(dateToString(new Date()), false)}},
{created_at: {$gte: '2018-12-05T00:00:01'}}
{ created_at: { $lt: normalizeDate(dateToString(new Date()), false) } },
{ created_at: { $gte: '2018-12-05T00:00:01' } }
]
}

Expand All @@ -271,8 +271,8 @@ describe('queryFilter()', function () {
it('should return req.query with set start_at params as dateTime', function () {
const expect_filters = {
$and: [
{created_at: {$lt: normalizeDate(dateToString(new Date()), false)}},
{created_at: {$gte: '2018-12-05T00:00:00'}}
{ created_at: { $lt: normalizeDate(dateToString(new Date()), false) } },
{ created_at: { $gte: '2018-12-05T00:00:00' } }
]
}

Expand All @@ -292,8 +292,8 @@ describe('queryFilter()', function () {

const expect_filters = {
$and: [
{created_at: {$lt: '2018-12-11T00:00:00'}},
{created_at: {$gte: '2018-12-01T00:00:00'}}]
{ created_at: { $lt: '2018-12-11T00:00:00' } },
{ created_at: { $gte: '2018-12-01T00:00:00' } }]
}


Expand All @@ -313,8 +313,8 @@ describe('queryFilter()', function () {

const expect_filters = {
$and: [
{created_at: {$lt: '2018-12-11T03:02:01'}},
{created_at: {$gte: '2018-12-01T01:02:03'}}]
{ created_at: { $lt: '2018-12-11T03:02:01' } },
{ created_at: { $gte: '2018-12-01T01:02:03' } }]
}

const options = JSON.parse(JSON.stringify(default_options))
Expand All @@ -332,8 +332,8 @@ describe('queryFilter()', function () {
it('should return req.query with period as day and start_at param', function () {
const expect_filters = {
$and: [
{created_at: {$lt: '2019-01-26T23:59:59'}},
{created_at: {$gte: '2019-01-24T00:00:00'}}
{ created_at: { $lt: '2019-01-26T23:59:59' } },
{ created_at: { $gte: '2019-01-24T00:00:00' } }
]
}

Expand All @@ -352,8 +352,8 @@ describe('queryFilter()', function () {
it('should return req.query with period as day and end_at param', function () {
const expect_filters = {
$and: [
{created_at: {$lt: '2019-01-26T00:00:00'}},
{created_at: {$gte: '2019-01-24T00:00:00'}}
{ created_at: { $lt: '2019-01-26T00:00:00' } },
{ created_at: { $gte: '2019-01-24T00:00:00' } }
]
}

Expand All @@ -372,8 +372,8 @@ describe('queryFilter()', function () {
it('should return req.query with period as week and start_at param', function () {
const expect_filters = {
$and: [
{created_at: {$lt: '2019-01-26T23:59:59'}},
{created_at: {$gte: '2019-01-19T00:00:00'}}
{ created_at: { $lt: '2019-01-26T23:59:59' } },
{ created_at: { $gte: '2019-01-19T00:00:00' } }
]
}

Expand All @@ -392,8 +392,8 @@ describe('queryFilter()', function () {
it('should return req.query with period as week and end_at param', function () {
const expect_filters = {
$and: [
{created_at: {$lt: '2019-01-26T23:59:59'}},
{created_at: {$gte: '2019-01-19T00:00:00'}}
{ created_at: { $lt: '2019-01-26T23:59:59' } },
{ created_at: { $gte: '2019-01-19T00:00:00' } }
]
}

Expand All @@ -412,8 +412,8 @@ describe('queryFilter()', function () {
it('should return req.query with period as month and start_at param', function () {
const expect_filters = {
$and: [
{created_at: {$lt: '2019-02-24T23:59:59'}},
{created_at: {$gte: '2019-01-24T00:00:00'}}
{ created_at: { $lt: '2019-02-24T23:59:59' } },
{ created_at: { $gte: '2019-01-24T00:00:00' } }
]
}

Expand All @@ -432,8 +432,8 @@ describe('queryFilter()', function () {
it('should return req.query with period as month and end_at param', function () {
const expect_filters = {
$and: [
{created_at: {$lt: '2019-01-24T23:59:59'}},
{created_at: {$gte: '2018-12-24T00:00:00'}}
{ created_at: { $lt: '2019-01-24T23:59:59' } },
{ created_at: { $gte: '2018-12-24T00:00:00' } }
]
}

Expand All @@ -452,8 +452,8 @@ describe('queryFilter()', function () {
it('should return req.query with period as year and start_at param', function () {
const expect_filters = {
$and: [
{created_at: {$lt: '2019-02-24T23:59:59'}},
{created_at: {$gte: '2018-02-24T00:00:00'}}
{ created_at: { $lt: '2019-02-24T23:59:59' } },
{ created_at: { $gte: '2018-02-24T00:00:00' } }
]
}

Expand All @@ -472,8 +472,8 @@ describe('queryFilter()', function () {
it('should return req.query with period as year and end_at param', function () {
const expect_filters = {
$and: [
{created_at: {$lt: '2019-02-24T23:59:59'}},
{created_at: {$gte: '2018-02-24T00:00:00'}}
{ created_at: { $lt: '2019-02-24T23:59:59' } },
{ created_at: { $gte: '2018-02-24T00:00:00' } }
]
}

Expand All @@ -492,8 +492,8 @@ describe('queryFilter()', function () {
it('should return req.query with today start_at for invalid period', function () {
const expect_filters = {
$and: [
{created_at: {$lt: normalizeDate(dateToString(new Date()), false)}},
{created_at: {$gte: normalizeDate(dateToString(new Date()), true)}}
{ created_at: { $lt: normalizeDate(dateToString(new Date()), false) } },
{ created_at: { $gte: normalizeDate(dateToString(new Date()), true) } }
]
}

Expand All @@ -512,8 +512,8 @@ describe('queryFilter()', function () {
it('should return req.query with today end_at for invalid period', function () {
const expect_filters = {
$and: [
{created_at: {$lt: normalizeDate(dateToString(new Date()), false)}},
{created_at: {$gte: '2018-12-05T00:00:01'}}
{ created_at: { $lt: normalizeDate(dateToString(new Date()), false) } },
{ created_at: { $gte: '2018-12-05T00:00:01' } }
]
}

Expand Down
16 changes: 8 additions & 8 deletions test/unit/fields.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@ describe('QueryString: Fields', function () {

context('when query fields are a simple string', function () {
it('should return a JSON with field params', function (done) {
const query = {fields: 'name,age,created_at'}
const query = { fields: 'name,age,created_at' }
verify(fields.fields(query, default_options))
done()
})
})

context('when query fields are an array of strings', function () {
it('should return a JSON with field params', function (done) {
const query = {fields: ['name,age', 'created_at']}
const query = { fields: ['name,age', 'created_at'] }
verify(fields.fields(query, default_options))
done()
})
})

context('when there are blank spaces between query fields', function () {
it('should return a JSON with field params, ignoring the blank space', function (done) {
const query = {fields: ' name , name, age , created_at'}
const query = { fields: ' name , name, age , created_at' }
verify(fields.fields(query, default_options))
done()
})
})

context('when there are null fields in query fields', function () {
it('should return a JSON with field params, ignoring the null fields', function (done) {
const query = {fields: ',,name,,,age,,,,,created_at,,'}
const query = { fields: ',,name,,,age,,,,,created_at,,' }
verify(fields.fields(query, default_options))
done()
})
})

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

Expand All @@ -54,16 +54,16 @@ describe('QueryString: Fields', function () {

context('when use custom params', function () {
it('should return a JSON with custom params', function () {
const custom_options = {default: {fields: {name: 1, age: 1, _id: 0}}}
const custom_options = { default: { fields: { name: 1, age: 1, _id: 0 } } }
const result = fields.fields({}, custom_options)
expect(result.name).to.eql(custom_options.default.fields.name)
expect(result.age).to.eql(custom_options.default.fields.age)
expect(result._id).to.eql(custom_options.default.fields._id)
})

it('should return a JSON with custom params and those of the query', function () {
const custom_options = {default: {fields: {name: 1, _id: 1}}}
const result = fields.fields({fields: 'age'}, custom_options)
const custom_options = { default: { fields: { name: 1, _id: 1 } } }
const result = fields.fields({ fields: 'age' }, custom_options)
expect(result.name).to.eql(custom_options.default.fields.name)
expect(result._id).to.eql(custom_options.default.fields._id)
expect(result.age).to.eql(1)
Expand Down
Loading

0 comments on commit a3a2c71

Please sign in to comment.