Skip to content
Browse files

supporting specific increments in dates

  • Loading branch information...
1 parent 502358f commit bab0b96d1fc8db59d335921d715a6260f228564f @aaronpowell committed
Showing with 77 additions and 0 deletions.
  1. +15 −0 lib/tbd.js
  2. +62 −0 tests/until-sequential-spec.js
View
15 lib/tbd.js
@@ -161,9 +161,24 @@
return function () {
start = new Date(start.getFullYear(), start.getMonth(), start.getDate(), start.getHours(), start.getMinutes(), start.getSeconds(), start.getMilliseconds());
switch(dateType) {
+ case 'y':
+ start.setFullYear(start.getFullYear() + inc++);
+ break;
+ case 'M':
+ start.setMonth(start.getMonth() + inc++);
+ break;
case 'd':
start.setDate(start.getDate() + inc++);
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:
throw 'The value ' + dateType + ' is not an understood date part';
}
View
62 tests/until-sequential-spec.js
@@ -46,4 +46,66 @@ describe('tbd-util-sequantial', function() {
expect(data[0].foo).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.
Something went wrong with that request. Please try again.