Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
177 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"browser": false, | ||
"node": true, | ||
"camelcase": true, | ||
"eqeqeq": false, | ||
"indent": 2, | ||
"quotmark": "single", | ||
"newcap": true, | ||
"trailing": true, | ||
"undef": true, | ||
"unused": true, | ||
"latedef": true, | ||
"eqnull": true, | ||
"globals": { | ||
"describe" : true, | ||
"xdescribe" : true, | ||
"it" : true, | ||
"xit" : true, | ||
"expect" : true, | ||
"assert" : true, | ||
"beforeEach": true, | ||
"before" : true, | ||
"afterEach" : true, | ||
"after" : true, | ||
"chai" : true, | ||
"mocha" : true, | ||
"sinon" : true | ||
} | ||
} |
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,3 @@ | ||
language: node_js | ||
node_js: | ||
- 0.10 |
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,42 @@ | ||
'use strict'; | ||
|
||
var toString = {}.toString; | ||
function isDate(d) { | ||
return toString.call(d) === '[object Date]'; | ||
} | ||
|
||
/** | ||
* Creates an array of dates | ||
* | ||
* @param {Date} start | ||
* @param {Date} [end] | ||
* @param {Number} [step] | ||
* | ||
* @return {Array} | ||
*/ | ||
function range(start, end, step) { | ||
end = end || new Date(); | ||
step = step || range.defaultStep; | ||
|
||
if (!isDate(start) || !isDate(end)) throw new TypeError(); | ||
|
||
var length = Math.ceil((end - start) / step); | ||
var out = new Array(length); | ||
out[0] = start; | ||
|
||
for (var i = +start + step, j = 1; i < +end; i += step, j += 1) { | ||
out[j] = new Date(i); | ||
} | ||
out[length] = end; | ||
return out; | ||
} | ||
|
||
range.MS = 1; | ||
range.SEC = 1e3; | ||
range.MIN = 60e3; | ||
range.HOUR = range.MIN * 60; | ||
range.DAY = range.HOUR * 24; | ||
|
||
range.defaultStep = range.DAY; | ||
|
||
module.exports = range; |
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,71 @@ | ||
/* jshint expr:true */ | ||
/* jshint unused:false */ | ||
'use strict'; | ||
|
||
var range = require('./'); | ||
var should = require('should'); | ||
|
||
function setTime(d, v) { | ||
v = v || 0; | ||
['Hours', 'Minutes', 'Seconds', 'Milliseconds'].forEach(function (m) { | ||
d['set' + m](v); | ||
}); | ||
return d; | ||
} | ||
|
||
describe('range', function () { | ||
var d1 = new Date(); | ||
setTime(d1); | ||
var d2 = new Date(d1 - (range.DAY * 2)); // - 2 days | ||
|
||
it('should throw an error if start or end is not a date', function() { | ||
|
||
var errs = []; | ||
|
||
try { | ||
range(d2, 'foo'); | ||
} catch(e) { | ||
errs.push(e); | ||
} | ||
|
||
try { | ||
range('foo', d1); | ||
} catch(e) { | ||
errs.push(e); | ||
} | ||
|
||
errs.length.should.equal(2); | ||
errs.every(function(v) { return v instanceof TypeError; }).should.be.true; | ||
}); | ||
|
||
before(function() { | ||
this.dr = range(new Date(d1 - (range.DAY * 3)), d1); | ||
}); | ||
|
||
it('should return an array', function() { | ||
this.dr.should.be.instanceof(Array); | ||
}); | ||
|
||
it('should create an array of length 4', function() { | ||
this.dr.length.should.equal(4); | ||
}); | ||
|
||
it('should step by 1 day', function() { | ||
this.dr.slice(1).every(function (d, i) { | ||
return d - this.dr[i] === range.DAY; | ||
}, this).should.be.true; | ||
}); | ||
|
||
it('should default to new date for end', function() { | ||
var dr = range(d2); | ||
dr.length.should.equal(4); | ||
}); | ||
|
||
it('should work with multiple step types', function() { | ||
['MS', 'SEC', 'MIN', 'HOUR', 'DAY'].forEach(function(m) { | ||
var d = new Date(d1 - (range[m] * 5)); | ||
var dr = range(d, d1, range[m]); | ||
dr.length.should.equal(6); | ||
}); | ||
}); | ||
}); |