Merge pull request #6 from ccowan/master

Adding calculateIndices() Method
This commit is contained in:
Chris Cowan 2014-02-24 15:03:02 -08:00
commit 2ef005851e
2 changed files with 8 additions and 17 deletions

View file

@ -2,13 +2,17 @@ define(function (require) {
return function (start, end, interval, pattern) {
if (end.isBefore(start)) {
throw new Error('Start must begin before end');
throw new Error('Start must begin before end.');
}
if (!~['hour','day','week','year'].indexOf(interval)) {
throw new Error('Interval must be hour, day, week, or year.');
}
if (!pattern) {
throw new Error('Pattern can not be empty.');
}
var data = [];
while(start.isBefore(end)) {
start.add(interval, '1');

View file

@ -5,19 +5,18 @@ define(function (require) {
describe('calculateIndices()', function () {
describe('error checking', function() {
it('should throw an error if start is > end', function () {
expect(function () { calculateIndices(moment().add('day', 1), moment()); }).to.throwError();
});
it('should throw an error if interval is not [ hour, day, week, year ]', function () {
expect(function () { calculateIndices(moment().subtract('day', 1), moment(), 'century' ); }).to.throwError();
});
it('should throw an error if pattern is not set', function () {
expect(function () { calculateIndices(moment().subtract('day', 1), moment(), 'hour' ); }).to.throwError();
});
});
describe('hourly interval', function() {
beforeEach(function () {
var date = '2014-01-15 04:30:10';
this.start = moment(date).subtract('hours', 4);
@ -31,16 +30,13 @@ define(function (require) {
'logstash-2014.01.15.04'
]
});
it('should return a set of hourly indices', function () {
expect(calculateIndices(this.start, this.end, this.interval, this.pattern))
.to.eql(this.fixture);
});
});
describe('daily interval', function() {
beforeEach(function () {
var date = '2014-01-15 04:30:10';
this.start = moment(date).subtract('days', 4);
@ -54,16 +50,13 @@ define(function (require) {
'logstash-2014.01.15'
]
});
it('should return a set of daily indices', function () {
expect(calculateIndices(this.start, this.end, this.interval, this.pattern))
.to.eql(this.fixture);
});
});
describe('weekly interval', function() {
beforeEach(function () {
var date = '2014-01-15 04:30:10';
this.start = moment(date).subtract('week', 4);
@ -77,16 +70,13 @@ define(function (require) {
'logstash-2014.01.15'
]
});
it('should return a set of daily indices', function () {
expect(calculateIndices(this.start, this.end, this.interval, this.pattern))
.to.eql(this.fixture);
});
});
describe('yearly interval', function() {
beforeEach(function () {
var date = '2014-01-15 04:30:10';
this.start = moment(date).subtract('years', 4);
@ -100,15 +90,12 @@ define(function (require) {
'logstash-2014.01.15'
]
});
it('should return a set of yearly indices', function () {
expect(calculateIndices(this.start, this.end, this.interval, this.pattern))
.to.eql(this.fixture);
});
});
});
});