Skip to content

Commit

Permalink
Merge pull request #6 from ccowan/master
Browse files Browse the repository at this point in the history
Adding calculateIndices() Method
  • Loading branch information
simianhacker committed Feb 24, 2014
2 parents fa95593 + 5713805 commit 46b3e0e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/courier/calculateIndices.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
19 changes: 3 additions & 16 deletions test/unit/specs/calculateIndices.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
});

});


});

});

0 comments on commit 46b3e0e

Please sign in to comment.