Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added object params for ".by" method. #94

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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