Skip to content

Commit

Permalink
Added object params for ".by" method.
Browse files Browse the repository at this point in the history
Previously, the ".by" method could only add in increments of 1. By utilizing objects and updating the case selection, we're now able to change the increments to something customized.

fixed bug in conditional and wrote a test

not for dist, fails one test

documented

rebuilt
  • Loading branch information
Faris Chebib committed Oct 2, 2015
1 parent 2e20232 commit 1c09c08
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ var acc = [];

range1.by('days', function(moment) {
// Do something with `moment`
acc.push(moment)
});
```
You can also use moment.js’ object notation to specify custom intervals:
``` javascript
var custom_interval = {minutes: 30}; // every half hour

range1.by(custom_interval, function(moment) {
// Do something with `moment`
acc.push(moment);
});
```
Expand Down
19 changes: 18 additions & 1 deletion dist/moment-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ DateRange.prototype.subtract = function(other) {
* @param {(!DateRange|String)} range Date range to be used for iteration or
* shorthand string (shorthands:
* http://momentjs.com/docs/#/manipulating/add/)
* Can also use Object Literals for different intervals.
* Eg, {minutes: 30}
* @param {!DateRange~by} hollaback Callback
* @param {!boolean} exclusive Indicate that the end of the range should not
* be included in the iter.
Expand All @@ -212,9 +214,12 @@ DateRange.prototype.by = function(range, hollaback, exclusive) {
if (typeof range === 'string') {
_byString.call(this, range, hollaback, exclusive);
}
else {
else if ((range instanceof DateRange) || (typeof range === 'number')) {
_byRange.call(this, range, hollaback, exclusive);
}
else if (typeof range === 'object') {
_byObject.call(this, range, hollaback, exclusive);
}
return this;
};

Expand All @@ -239,6 +244,18 @@ function _byString(interval, hollaback, exclusive) {
}
}

/**
* @private
*/
function _byObject(interval, hollaback, exclusive) {
var current = moment(this.start);

while (this.contains(current, exclusive)) {
hollaback.call(this, current.clone());
current.add(interval);
}
}

/**
* @private
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/moment-range.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion lib/moment-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ DateRange.prototype.subtract = function(other) {
* @param {(!DateRange|String)} range Date range to be used for iteration or
* shorthand string (shorthands:
* http://momentjs.com/docs/#/manipulating/add/)
* Can also use Object Literals for different intervals.
* Eg, {minutes: 30}
* @param {!DateRange~by} hollaback Callback
* @param {!boolean} exclusive Indicate that the end of the range should not
* be included in the iter.
Expand All @@ -196,9 +198,12 @@ DateRange.prototype.by = function(range, hollaback, exclusive) {
if (typeof range === 'string') {
_byString.call(this, range, hollaback, exclusive);
}
else {
else if ((range instanceof DateRange) || (typeof range === 'number')) {
_byRange.call(this, range, hollaback, exclusive);
}
else if (typeof range === 'object') {
_byObject.call(this, range, hollaback, exclusive);
}
return this;
};

Expand All @@ -223,6 +228,18 @@ function _byString(interval, hollaback, exclusive) {
}
}

/**
* @private
*/
function _byObject(interval, hollaback, exclusive) {
var current = moment(this.start);

while (this.contains(current, exclusive)) {
hollaback.call(this, current.clone());
current.add(interval);
}
}

/**
* @private
*/
Expand Down
18 changes: 18 additions & 0 deletions test/moment-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,24 @@ describe('DateRange', function() {
acc[4].utc().date().should.eql(5);
});

it('should iterate correctly by object literal', function(){
var acc = [];
var d1 = new Date(Date.UTC(2012, 1, 1));
var d2 = new Date(Date.UTC(2012, 1, 8));
var dr1 = moment.range(d1, d2);
var dr2 = {days:2};

dr1.by(dr2, function(m){
acc.push(m);
});

acc.length.should.eql(4);
acc[0].utc().date().should.eql(1);
acc[1].utc().date().should.eql(3);
acc[2].utc().date().should.eql(5);
acc[3].utc().date().should.eql(7);
});

it('should iterate correctly by year over a Date-constructed range when leap years are involved', function() {
var acc = [];
var d1 = new Date(Date.UTC(2011, 1, 1));
Expand Down

0 comments on commit 1c09c08

Please sign in to comment.