-
Notifications
You must be signed in to change notification settings - Fork 927
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add date parse utility + tests for better reusability
- Loading branch information
1 parent
083c282
commit 0d8ebc0
Showing
6 changed files
with
48 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
const dayjs = require('dayjs'); | ||
|
||
/** | ||
* Return date range object for mongo find query | ||
* @param {Object} query Koa querystring object from ctx.request | ||
* @return {Object} Mongo compatible date range query object | ||
*/ | ||
|
||
module.exports = (q) => { | ||
let start; | ||
let end; | ||
// See if date is unix, if so, add miliseconds | ||
if (/^[0-9]*$/.test(q.start && (q.final || q.end))) { | ||
start = dayjs(q.start * 1000); | ||
end = dayjs(q.final * 1000 || q.end * 1000); | ||
} else { | ||
start = dayjs(q.start); | ||
end = dayjs(q.final || q.end); | ||
} | ||
|
||
return { $gte: start.toISOString(), $lte: end.toISOString() }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
const dateParse = require('../../src/utilities/date_range'); | ||
|
||
test('It should return the correct date comparison object', () => { | ||
const query = { | ||
start: '2017-06-22', | ||
final: '2017-06-25', | ||
}; | ||
const response = dateParse(query); | ||
expect(response).toEqual({ $gte: '2017-06-22T05:00:00.000Z', $lte: '2017-06-25T05:00:00.000Z' }); | ||
}); |