Skip to content

Commit 2df7e11

Browse files
author
Lucas Cosmo Rocha
committed
Update Filters methods
Adapting to date / datetime pattern Added normalization for dates that are not in datetime format.
1 parent bac5d47 commit 2df7e11

File tree

3 files changed

+75
-21
lines changed

3 files changed

+75
-21
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ declare namespace queryStringsParser {
1010
default?: IDefault
1111
use_page?: boolean
1212
client_db?: string
13+
date_field?: string
1314
}
1415

1516
export interface IDefault {

lib/mapper/filters.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ function processQuery(key, value) {
4242
}
4343

4444
function treatValue(value) {
45-
if (isDate(value) || isDateTime(value)) return new Date(value).toISOString()
45+
if (isDate(value)) return normalizeDate(value, true)
46+
else if (isDateTime(value)) return value
4647
else if (value.includes('*')) return buildRegEx(value)
4748
else if (value.includes(':')) return getCompareOperator(value)
48-
else if (value === 'now') return new Date().toISOString()
49+
else if (value === 'now') return normalizeDate (new Date().toDateString())
4950
else if (parseInt(value)) return parseInt(value)
5051
return value
5152
}
@@ -88,18 +89,18 @@ function parseDate(query, options) {
8889
if (query.period) {
8990
(query.date_end) ?
9091
result.push(
91-
processQuery(options.date_field, 'lt:'.concat(query.date_end)),
92-
processQuery(options.date_field, 'gte:'.concat(
93-
getDateStart(query.period, new Date(query.date_end))))) :
92+
processQuery(options.date_field, 'lt:'.concat(normalizeDate(query.date_end, false))),
93+
processQuery(options.date_field, 'gte:'.concat(normalizeDate(
94+
getDateStart(query.period, new Date(query.date_end))))), true) :
9495
result.push(
9596
processQuery(options.date_field, 'lt:now'),
9697
processQuery(options.date_field, 'gte:'.concat(
9798
getDateStart(query.period, new Date()))))
9899
return result
99100
}
100101

101-
if (query.date_start && isDate(query.date_start)) {
102-
if (query.date_end && isDate(query.date_end)) {
102+
if (query.date_start && (isDate(query.date_start) || isDateTime(query.date_start))) {
103+
if (query.date_end && (isDate(query.date_end) || isDateTime(query.date_end))) {
103104
result.push(
104105
processQuery(options.date_field, 'lt:'.concat(query.date_end)))
105106
} else {
@@ -142,6 +143,7 @@ function getDateStart(period, date_end) {
142143
function dateToString(date) {
143144
return `${date.getFullYear()}-${date.getMonth() + 1}-${formatDay(date.getDate())}`
144145
}
146+
145147
function formatDay(day) {
146148
return day < 10 ? '0'.concat(day) : day
147149
}
@@ -150,6 +152,16 @@ function onlyNumbers(value) {
150152
return parseInt(value.replace(/[^0-9]+/g, ''))
151153
}
152154

155+
function normalizeDate(date, isDateStart) {
156+
if (isDate(date) && isDateStart) {
157+
return date.replace(/\D/g, '').replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3T00:00:00')
158+
}
159+
else if (isDate(date) && !isDateStart) {
160+
return date.replace(/\D/g, '').replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3T23:59:59')
161+
}
162+
return date
163+
}
164+
153165
exports = module.exports = {
154166
filters: filters
155-
}
167+
}

test/integration/index.spec.js

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ describe('queryFilter()', function () {
146146
name: { '$options': 'i', '$regex': 'jorge' }
147147
}],
148148
'school.name': 'UEPB',
149-
'timestamp': '2018-12-05T00:00:00.000Z',
149+
'timestamp': '2018-12-05T00:00:00',
150150
'$or': [{ job: 'Developer' }, { job: 'Engineer' }]
151151
}
152152

@@ -174,8 +174,8 @@ describe('queryFilter()', function () {
174174
const expect_filters = {
175175
name: 'lucas',
176176
age: { $gt: 30 },
177-
timestamp: { $gt: '2018-12-05T00:00:00.000Z' },
178-
created_at: { $lte: '2018-12-06T00:00:00.000Z' },
177+
timestamp: { $gt: '2018-12-05T00:00:00' },
178+
created_at: { $lte: '2018-12-06T00:00:00' },
179179
sleep_hour: '22:40'
180180
}
181181

@@ -193,15 +193,14 @@ describe('queryFilter()', function () {
193193
options.default.filters = expect_filters
194194

195195
qs({})(req, res, function next() {
196-
console.log('req: ', req.query.filters);
197196
validate(req, options)
198197
})
199198
done()
200199
})
201200
})
202201

203202
context('when query contains date filters param', function () {
204-
it('should return req.query with set date_start params', function (done) {
203+
it('should return req.query with set date_start params as date', function (done) {
205204
const expect_filters = {
206205
$and: [
207206
{ created_at: { $lt: new Date().toISOString() } },
@@ -222,7 +221,28 @@ describe('queryFilter()', function () {
222221
done()
223222
})
224223

225-
it('should return req.query with set date_start and date_end params', function (done) {
224+
it('should return req.query with set date_start params as dateTime', function (done) {
225+
const expect_filters = {
226+
$and: [
227+
{ created_at: { $lt: new Date().toISOString() } },
228+
{ created_at: { $gte: '2018-12-05T00:00:00.000Z' } }
229+
]
230+
}
231+
232+
const query = { date_start: '2018-12-05T00:00:00'}
233+
const req = httpMocks.createRequest({ method: 'GET', url: '/', query: query })
234+
const res = httpMocks.createResponse()
235+
236+
const options = JSON.parse(JSON.stringify(default_options))
237+
options.default.filters = expect_filters
238+
239+
qs({})(req, res, function next() {
240+
validateFilterWithDate(req, options)
241+
})
242+
done()
243+
})
244+
245+
it('should return req.query with set date_start and date_end params as date', function (done) {
226246

227247
const expect_filters = {
228248
$and: [
@@ -243,15 +263,36 @@ describe('queryFilter()', function () {
243263
done()
244264
})
245265

266+
it('should return req.query with set date_start and date_end params as dateTime', function (done) {
267+
268+
const expect_filters = {
269+
$and: [
270+
{ created_at: { $lt: '2018-12-11T00:00:00.000Z' } },
271+
{ created_at: { $gte: '2018-12-01T00:00:00.000Z' } }]
272+
}
273+
274+
const query = { date_start: '2018-12-01T00:00:00', date_end: '2018-12-11T00:00:00' }
275+
const req = httpMocks.createRequest({ method: 'GET', url: '/', query: query })
276+
const res = httpMocks.createResponse()
277+
278+
const options = JSON.parse(JSON.stringify(default_options))
279+
options.default.filters = expect_filters
280+
281+
qs({})(req, res, function next() {
282+
validateFilterWithDate(req, options)
283+
})
284+
done()
285+
})
286+
246287
it('should return req.query with set period and date_end param', function (done) {
247288

248289
const expect_filters = {
249290
$and: [
250-
{ created_at: { $lt: new Date('2018-12-13').toISOString() } },
251-
{ created_at: { $gte: new Date('2018-11-12').toISOString() } }
291+
{ created_at: { $lt: new Date('2018-12-09').toISOString() } },
292+
{ created_at: { $gte: new Date('2018-11-10').toISOString() } }
252293
]
253294
}
254-
const query = { period: '1m', date_end: '2018-12-13' }
295+
const query = { period: '1m', date_end: '2018-12-09' }
255296
const req = httpMocks.createRequest({ method: 'GET', url: '/', query: query })
256297
const res = httpMocks.createResponse()
257298

@@ -265,7 +306,7 @@ describe('queryFilter()', function () {
265306
})
266307

267308
it('should return req.query with set period as day params', function (done) {
268-
const now = new Date();
309+
const now = new Date()
269310
const today = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1))
270311
const beforeToday = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate() - 9))
271312
const expect_filters = {
@@ -290,7 +331,7 @@ describe('queryFilter()', function () {
290331

291332
it('should return req.query with set period as week params', function (done) {
292333

293-
const now = new Date();
334+
const now = new Date()
294335
const today = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1))
295336
const beforeToday = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate() - 14))
296337
const expect_filters = {
@@ -314,7 +355,7 @@ describe('queryFilter()', function () {
314355
})
315356
it('should return req.query with set period as month params', function (done) {
316357

317-
const now = new Date();
358+
const now = new Date()
318359
const today = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1))
319360
const beforeToday = dateToString(new Date(now.getFullYear(), (now.getMonth() - 1), now.getDate()))
320361
const expect_filters = {
@@ -339,7 +380,7 @@ describe('queryFilter()', function () {
339380

340381
it('should return req.query with set period as year params', function (done) {
341382

342-
const now = new Date();
383+
const now = new Date()
343384
const today = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1))
344385
const beforeToday = dateToString(new Date(now.getFullYear() - 1, now.getMonth(), now.getDate()))
345386
const expect_filters = {

0 commit comments

Comments
 (0)