Skip to content

Commit

Permalink
supporting specific increments in dates
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronpowell committed Dec 4, 2011
1 parent 502358f commit bab0b96
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/tbd.js
Expand Up @@ -161,9 +161,24 @@
return function () { return function () {
start = new Date(start.getFullYear(), start.getMonth(), start.getDate(), start.getHours(), start.getMinutes(), start.getSeconds(), start.getMilliseconds()); start = new Date(start.getFullYear(), start.getMonth(), start.getDate(), start.getHours(), start.getMinutes(), start.getSeconds(), start.getMilliseconds());
switch(dateType) { switch(dateType) {
case 'y':
start.setFullYear(start.getFullYear() + inc++);
break;
case 'M':
start.setMonth(start.getMonth() + inc++);
break;
case 'd': case 'd':
start.setDate(start.getDate() + inc++); start.setDate(start.getDate() + inc++);
break; break;
case 'h':
start.setHours(start.getHours() + inc++);
break;
case 'm':
start.setMinutes(start.getMinutes() + inc++);
break;
case 's':
start.setSeconds(start.getSeconds() + inc++);
break;
default: default:
throw 'The value ' + dateType + ' is not an understood date part'; throw 'The value ' + dateType + ' is not an understood date part';
} }
Expand Down
62 changes: 62 additions & 0 deletions tests/until-sequential-spec.js
Expand Up @@ -46,4 +46,66 @@ describe('tbd-util-sequantial', function() {
expect(data[0].foo).toEqual(start); expect(data[0].foo).toEqual(start);
expect(data[1].foo).not.toEqual(start); expect(data[1].foo).not.toEqual(start);
}); });

it('should increment by year when specified', function () {
var start = new Date(),
data = tbd.from({})
.prop('foo').use(tbd.utils.sequential(start, 'y'))
.make(2);

expect(data[0].foo.getFullYear()).toEqual(start.getFullYear());
expect(data[1].foo.getFullYear()).toEqual(start.getFullYear() + 1);
});

it('should increment by month when specified', function () {
var start = new Date(),
month = start.getMonth() == 11 ? 0 : start.getMonth + 1,
data = tbd.from({})
.prop('foo').use(tbd.utils.sequential(start, 'M'))
.make(2);

expect(data[0].foo.getMonth()).toEqual(start.getMonth());
expect(data[1].foo.getMonth()).toEqual(month);
});

it('should increment by day when specified', function () {
var start = new Date(),
data = tbd.from({})
.prop('foo').use(tbd.utils.sequential(start, 'd'))
.make(2);

expect(data[0].foo.getDate()).toEqual(start.getDate());
expect(data[1].foo.getDate()).toEqual(start.getDate() + 1);
});

it('should increment by hour when specified', function () {
var start = new Date(),
hours = start.getHours() === 23 ? 0 : start.getHours() + 1,
data = tbd.from({})
.prop('foo').use(tbd.utils.sequential(start, 'h'))
.make(2);

expect(data[0].foo.getHours()).toEqual(start.getHours());
expect(data[1].foo.getHours()).toEqual(hours);
});

it('should increment by minutes when specified', function () {
var start = new Date(),
data = tbd.from({})
.prop('foo').use(tbd.utils.sequential(start, 'm'))
.make(2);

expect(data[0].foo.getMinutes()).toEqual(start.getMinutes());
expect(data[1].foo.getMinutes()).toEqual(start.getMinutes() + 1);
});

it('should increment by seconds when specified', function () {
var start = new Date(),
data = tbd.from({})
.prop('foo').use(tbd.utils.sequential(start, 's'))
.make(2);

expect(data[0].foo.getSeconds()).toEqual(start.getSeconds());
expect(data[1].foo.getSeconds()).toEqual(start.getSeconds() + 1);
});
}); });

0 comments on commit bab0b96

Please sign in to comment.