Skip to content

Commit 063d239

Browse files
Initial release
0 parents  commit 063d239

File tree

8 files changed

+301
-0
lines changed

8 files changed

+301
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.DS_Store
3+
.jshintrc

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- "0.12"
4+
- "0.11"
5+
- "0.10"

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Matt Atkinson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
express-dates-middleware
2+
=========
3+
4+
[![Build Status](https://travis-ci.org/mattpker/express-dates-middleware.svg)](https://travis-ci.org/mattpker/express-dates-middleware) [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/mattpker/express-dates-middleware?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
5+
6+
Express middleware for providing a common start date and end date.
7+
8+
This module can be used as a middleware for express for providing common startDate and endDate parameters. This is helpful when creating an API that may need a start date or end date on some endpoints.
9+
10+
The following startDate and endDate parameters are returned depending on what parameters are provided:
11+
12+
* startDate & endDate: Provides a proper date object of the startDate & endDate.
13+
* No startDate & endDate: Provides a startDate of the beginning of today and an endDate of the ending of today.
14+
* endDate: Provides only that day, the startDate is the beginning of the endDate provided and the endDate is the end of that day.
15+
* startDate: Provides the startDate to the end of the current day as the endDate.
16+
17+
## Installation
18+
19+
npm install express-dates-middleware
20+
21+
## Usage
22+
23+
```
24+
var express = require('express');
25+
var app = express();
26+
var dates = require('express-dates-middleware');
27+
28+
app.use(dates);
29+
30+
app.get('/', function (req, res) {
31+
res.send({
32+
startDate: req.query.startDate,
33+
endDate: req.query.endDate
34+
});
35+
});
36+
37+
var server = app.listen(3000, function () {
38+
var port = server.address().port;
39+
console.log('Server listening on port:', port);
40+
});
41+
```
42+
43+
You can then go to your browser and test the different returns:
44+
45+
http://localhost:3000/
46+
47+
http://localhost:3000/?startDate=12/15/2015
48+
49+
http://localhost:3000/?endDate=12/15/2015
50+
51+
http://localhost:3000/?startDate=12/15/2015&endDate=12/25/2015
52+
53+
## Tests
54+
55+
npm test
56+
57+
## Contributing
58+
59+
In lieu of a formal styleguide, take care to maintain the existing coding style.
60+
Add unit tests for any new or changed functionality. Lint and test your code.
61+
62+
## Release History
63+
64+
* 1.0.0 Initial release

examples/example.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
var express = require('express');
4+
var app = express();
5+
var dates = require('../express-dates-middleware');
6+
7+
app.use(dates);
8+
9+
app.get('/', function (req, res) {
10+
res.send({
11+
startDate: req.query.startDate,
12+
endDate: req.query.endDate
13+
});
14+
});
15+
16+
var server = app.listen(3000, function () {
17+
var port = server.address().port;
18+
console.log('Server listening on port:', port);
19+
});

express-dates-middleware.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
3+
var moment = require('moment');
4+
5+
// Midleware for express to convert startDate and endDate request parameters
6+
module.exports = function(req, res, next) {
7+
8+
var startDate,
9+
endDate;
10+
11+
// Function to parse a string into an integer for use as a milisecond epoch
12+
var parseEpoch = function(date) {
13+
// If the date string is just numbers
14+
if (!isNaN(date)) {
15+
// If the length of the sting is then 13, assume it is a epoch based on seconds and not miliseconds
16+
if (date.length < 13) {
17+
// Change the sting into an integer
18+
date = parseInt(date);
19+
// Multiply the integer by 1000 to get the milisecond epoch and reurn the value
20+
return date * 1000;
21+
} else {
22+
// Return the string as an integer
23+
return parseInt(date);
24+
}
25+
} else {
26+
// Just return the sting as it is not an epoch
27+
return date;
28+
}
29+
};
30+
31+
// If we dont have a startDate and endDate, send the current day
32+
if (!req.query.startDate && !req.query.endDate) {
33+
req.query.startDate = new Date(moment().format('L') + ' 00:00:00');
34+
req.query.endDate = new Date(moment().format('L') + ' 23:59:59');
35+
}
36+
37+
// If we have only a start date, return from start date to current day
38+
else if (req.query.startDate && !req.query.endDate) {
39+
40+
startDate = parseEpoch(req.query.startDate);
41+
42+
req.query.startDate = new Date(startDate);
43+
req.query.endDate = new Date(moment().format('L') + ' 23:59:59');
44+
}
45+
46+
47+
// If we only have an end date, return only that day
48+
else if (!req.query.startDate && req.query.endDate) {
49+
50+
endDate = parseEpoch(req.query.endDate);
51+
52+
req.query.startDate = new Date(moment(new Date(endDate)).format('L') + ' 00:00:00');
53+
req.query.endDate = new Date(moment(new Date(endDate)).format('L') + ' 23:59:59');
54+
}
55+
56+
// If we have both a startDate and endDate, convert to Date objects
57+
else if (req.query.startDate && req.query.endDate) {
58+
59+
startDate = parseEpoch(req.query.startDate);
60+
endDate = parseEpoch(req.query.endDate);
61+
62+
req.query.startDate = new Date(startDate);
63+
req.query.endDate = new Date(endDate);
64+
}
65+
66+
// keep executing the router middleware
67+
next();
68+
69+
};

package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "express-dates-middleware",
3+
"version": "1.0.0",
4+
"description": "Express middleware for providing a common start date and end date",
5+
"main": "express-dates-middleware.js",
6+
"scripts": {
7+
"test": "mocha"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/mattpker/express-dates-middleware"
12+
},
13+
"keywords": [
14+
"express",
15+
"middleware",
16+
"dates",
17+
"date",
18+
"api"
19+
],
20+
"author": "Matt Atkinson <mattpker@gmail.com> (https://github.com/mattpker)",
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/mattpker/express-dates-middleware/issues"
24+
},
25+
"homepage": "https://github.com/mattpker/express-dates-middleware",
26+
"devDependencies": {
27+
"chai": "^3.0.0",
28+
"mocha": "^2.2.5"
29+
},
30+
"dependencies": {
31+
"moment": "^2.10.3"
32+
}
33+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'use strict';
2+
3+
var expect = require('chai').expect;
4+
var dates = require('../express-dates-middleware');
5+
var moment = require('moment');
6+
7+
describe('express-dates-middleware', function() {
8+
it('should return a start and end date for no provided dates', function(done) {
9+
var res = {};
10+
var req = {};
11+
req.query = {
12+
startDate: '',
13+
endDate: ''
14+
};
15+
16+
dates(req, res, function() {
17+
expect(req.query, 'should be an object').to.be.an('object');
18+
expect(req.query, 'should have a start date and end date').to.have.ownProperty('startDate', 'endDate');
19+
expect(req.query.startDate, 'start date should be correct').to.deep.equal(new Date(moment().format('L') + ' 00:00:00'));
20+
expect(req.query.endDate, 'end date should be correct').to.deep.equal(new Date(moment().format('L') + ' 23:59:59'));
21+
});
22+
23+
done();
24+
});
25+
26+
it('should return a start and end date for start date provided', function(done) {
27+
var startDate = new Date();
28+
startDate.setDate(startDate.getDate() - 1);
29+
var res = {};
30+
var req = {};
31+
req.query = {
32+
startDate: startDate.getTime(),
33+
endDate: ''
34+
};
35+
36+
dates(req, res, function() {
37+
expect(req.query, 'should be an object').to.be.an('object');
38+
expect(req.query, 'should have a start date and end date').to.have.ownProperty('startDate', 'endDate');
39+
expect(req.query.startDate, 'start date should be correct').to.deep.equal(startDate);
40+
expect(req.query.endDate, 'end date should be correct').to.deep.equal(new Date(moment().format('L') + ' 23:59:59'));
41+
});
42+
43+
done();
44+
});
45+
46+
it('should return a start and end date for end date provided', function(done) {
47+
var endDate = new Date();
48+
endDate.setDate(endDate.getDate() - 1);
49+
var res = {};
50+
var req = {};
51+
req.query = {
52+
startDate: '',
53+
endDate: endDate.getTime()
54+
};
55+
56+
dates(req, res, function() {
57+
expect(req.query, 'should be an object').to.be.an('object');
58+
expect(req.query, 'should have a start date and end date').to.have.ownProperty('startDate', 'endDate');
59+
expect(req.query.startDate, 'start date should be correct').to.deep.equal(new Date(moment(endDate).format('L') + ' 00:00:00'));
60+
expect(req.query.endDate, 'end date should be correct').to.deep.equal(new Date(moment(endDate).format('L') + ' 23:59:59'));
61+
});
62+
63+
done();
64+
});
65+
66+
it('should return a start and end date for both date provided', function(done) {
67+
var startDate = new Date();
68+
startDate.setDate(startDate.getDate() - 4);
69+
var endDate = new Date();
70+
endDate.setDate(endDate.getDate() - 1);
71+
var res = {};
72+
var req = {};
73+
req.query = {
74+
startDate: startDate.getTime(),
75+
endDate: endDate.getTime()
76+
};
77+
78+
dates(req, res, function() {
79+
expect(req.query, 'should be an object').to.be.an('object');
80+
expect(req.query, 'should have a start date and end date').to.have.ownProperty('startDate', 'endDate');
81+
expect(req.query.startDate, 'start date should be correct').to.deep.equal(startDate);
82+
expect(req.query.endDate, 'end date should be correct').to.deep.equal(endDate);
83+
});
84+
85+
done();
86+
});
87+
});

0 commit comments

Comments
 (0)