moment-range
Advanced tools
Comparing version 2.1.0 to 2.2.0
{ | ||
"name": "moment-range", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"main": "./dist/moment-range.js", | ||
@@ -5,0 +5,0 @@ "ignore": [ |
@@ -199,2 +199,21 @@ (function (root, factory) { | ||
/** | ||
* Build a n array of dates. | ||
* | ||
* @param {(!DateRange|String)} range Date range to be used for iteration or | ||
* shorthand string (shorthands: | ||
* http://momentjs.com/docs/#/manipulating/add/) | ||
* @param {!boolean} exclusive Indicate that the end of the range should not | ||
* be included in the iter. | ||
* | ||
* @return {!Array} | ||
*/ | ||
DateRange.prototype.toArray = function(by, exclusive) { | ||
var acc = []; | ||
this.by(by, function(unit) { | ||
acc.push(unit); | ||
}, exclusive); | ||
return acc; | ||
}; | ||
/** | ||
* Iterate over the date range by a given date range, executing a function | ||
@@ -201,0 +220,0 @@ * for each sub-range. |
@@ -1,1 +0,1 @@ | ||
!function(a,b){"function"==typeof define&&define.amd?define(["moment"],function(c){return a.DateRange=b(c)}):"object"==typeof exports?module.exports=b(require("moment")):a.DateRange=b(moment)}(this,function(a){function b(b,c){var d,e=b,f=c;(1===arguments.length||void 0===c)&&("object"==typeof b&&2===b.length?(e=b[0],f=b[1]):"string"==typeof b&&(d=b.split("/"),e=d[0],f=d[1])),this.start=a(null===e?-864e13:e),this.end=a(null===f?864e13:f)}function c(b,c,d){for(var e=a(this.start);this.contains(e,d);)c.call(this,e.clone()),e.add(1,b)}function d(b,c,d){var e=this/b,f=Math.floor(e);if(f!==1/0){f===e&&d&&f--;for(var g=0;f>=g;g++)c.call(this,a(this.start.valueOf()+b.valueOf()*g))}}var e={year:!0,month:!0,week:!0,day:!0,hour:!0,minute:!0,second:!0};return b.prototype.constructor=b,b.prototype.clone=function(){return a().range(this.start,this.end)},b.prototype.contains=function(a,c){var d=this.start,e=this.end;return a instanceof b?d<=a.start&&(e>a.end||e.isSame(a.end)&&!c):a>=d&&(e>a||e.isSame(a)&&!c)},b.prototype.overlaps=function(a){return null!==this.intersect(a)},b.prototype.intersect=function(a){var c=this.start,d=this.end;return c<=a.start&&a.start<d&&d<a.end?new b(a.start,d):a.start<c&&c<a.end&&a.end<=d?new b(c,a.end):a.start<c&&d>=c&&d<a.end?this:c<=a.start&&a.start<=a.end&&a.end<=d?a:null},b.prototype.add=function(c){return this.overlaps(c)?new b(a.min(this.start,c.start),a.max(this.end,c.end)):null},b.prototype.subtract=function(a){var c=this.start,d=this.end;return null===this.intersect(a)?[this]:a.start<=c&&d>c&&d<=a.end?[]:a.start<=c&&c<a.end&&a.end<d?[new b(a.end,d)]:c<a.start&&a.start<d&&d<=a.end?[new b(c,a.start)]:c<a.start&&a.start<a.end&&a.end<d?[new b(c,a.start),new b(a.end,d)]:c<a.start&&a.start<d&&a.end<d?[new b(c,a.start),new b(a.start,d)]:void 0},b.prototype.by=function(a,b,e){return"string"==typeof a?c.call(this,a,b,e):d.call(this,a,b,e),this},b.prototype.toString=function(){return this.start.format()+"/"+this.end.format()},b.prototype.valueOf=function(){return this.end-this.start},b.prototype.center=function(){var b=this.start+this.diff()/2;return a(b)},b.prototype.toDate=function(){return[this.start.toDate(),this.end.toDate()]},b.prototype.isSame=function(a){return this.start.isSame(a.start)&&this.end.isSame(a.end)},b.prototype.diff=function(a){return this.end.diff(this.start,a)},a.range=function(c,d){return c in e?new b(a(this).startOf(c),a(this).endOf(c)):new b(c,d)},a.range.constructor=b,a.fn.range=a.range,a.fn.within=function(a){return a.contains(this._d)},b}); | ||
!function(a,b){"function"==typeof define&&define.amd?define(["moment"],function(c){return a.DateRange=b(c)}):"object"==typeof exports?module.exports=b(require("moment")):a.DateRange=b(moment)}(this,function(a){function b(b,c){var d,e=b,f=c;(1===arguments.length||void 0===c)&&("object"==typeof b&&2===b.length?(e=b[0],f=b[1]):"string"==typeof b&&(d=b.split("/"),e=d[0],f=d[1])),this.start=a(null===e?-864e13:e),this.end=a(null===f?864e13:f)}function c(b,c,d){for(var e=a(this.start);this.contains(e,d);)c.call(this,e.clone()),e.add(1,b)}function d(b,c,d){var e=this/b,f=Math.floor(e);if(f!==1/0){f===e&&d&&f--;for(var g=0;f>=g;g++)c.call(this,a(this.start.valueOf()+b.valueOf()*g))}}var e={year:!0,month:!0,week:!0,day:!0,hour:!0,minute:!0,second:!0};return b.prototype.constructor=b,b.prototype.clone=function(){return a().range(this.start,this.end)},b.prototype.contains=function(a,c){var d=this.start,e=this.end;return a instanceof b?d<=a.start&&(e>a.end||e.isSame(a.end)&&!c):a>=d&&(e>a||e.isSame(a)&&!c)},b.prototype.overlaps=function(a){return null!==this.intersect(a)},b.prototype.intersect=function(a){var c=this.start,d=this.end;return c<=a.start&&a.start<d&&d<a.end?new b(a.start,d):a.start<c&&c<a.end&&a.end<=d?new b(c,a.end):a.start<c&&d>=c&&d<a.end?this:c<=a.start&&a.start<=a.end&&a.end<=d?a:null},b.prototype.add=function(c){return this.overlaps(c)?new b(a.min(this.start,c.start),a.max(this.end,c.end)):null},b.prototype.subtract=function(a){var c=this.start,d=this.end;return null===this.intersect(a)?[this]:a.start<=c&&d>c&&d<=a.end?[]:a.start<=c&&c<a.end&&a.end<d?[new b(a.end,d)]:c<a.start&&a.start<d&&d<=a.end?[new b(c,a.start)]:c<a.start&&a.start<a.end&&a.end<d?[new b(c,a.start),new b(a.end,d)]:c<a.start&&a.start<d&&a.end<d?[new b(c,a.start),new b(a.start,d)]:void 0},b.prototype.toArray=function(a,b){var c=[];return this.by(a,function(a){c.push(a)},b),c},b.prototype.by=function(a,b,e){return"string"==typeof a?c.call(this,a,b,e):d.call(this,a,b,e),this},b.prototype.toString=function(){return this.start.format()+"/"+this.end.format()},b.prototype.valueOf=function(){return this.end-this.start},b.prototype.center=function(){var b=this.start+this.diff()/2;return a(b)},b.prototype.toDate=function(){return[this.start.toDate(),this.end.toDate()]},b.prototype.isSame=function(a){return this.start.isSame(a.start)&&this.end.isSame(a.end)},b.prototype.diff=function(a){return this.end.diff(this.start,a)},a.range=function(c,d){return c in e?new b(a(this).startOf(c),a(this).endOf(c)):new b(c,d)},a.range.constructor=b,a.fn.range=a.range,a.fn.within=function(a){return a.contains(this._d)},b}); |
@@ -183,2 +183,21 @@ //----------------------------------------------------------------------------- | ||
/** | ||
* Build a n array of dates. | ||
* | ||
* @param {(!DateRange|String)} range Date range to be used for iteration or | ||
* shorthand string (shorthands: | ||
* http://momentjs.com/docs/#/manipulating/add/) | ||
* @param {!boolean} exclusive Indicate that the end of the range should not | ||
* be included in the iter. | ||
* | ||
* @return {!Array} | ||
*/ | ||
DateRange.prototype.toArray = function(by, exclusive) { | ||
var acc = []; | ||
this.by(by, function(unit) { | ||
acc.push(unit); | ||
}, exclusive); | ||
return acc; | ||
}; | ||
/** | ||
* Iterate over the date range by a given date range, executing a function | ||
@@ -185,0 +204,0 @@ * for each sub-range. |
@@ -24,3 +24,3 @@ { | ||
}, | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"engines": { | ||
@@ -27,0 +27,0 @@ "node": "*" |
@@ -18,2 +18,3 @@ # moment-range | ||
- [Conversion](#conversion) | ||
- [`toArray`](#toarray) | ||
- [`toDate`](#todate) | ||
@@ -237,2 +238,14 @@ - [`toString`](#tostring) | ||
#### `toArray` | ||
Converts the `DateRange` to an `Array` of `Date` objects. | ||
``` javascript | ||
var start = new Date(2011, 2, 5); | ||
var end = new Date(2011, 5, 5); | ||
var dr = moment.range(start, end); | ||
dr.toArray('days'); // [new Date(2011, 2, 5), new Date(2011, 3, 5), new Date(2011, 4, 5), new Date(2011, 5, 5)] | ||
``` | ||
#### `toDate` | ||
@@ -239,0 +252,0 @@ |
@@ -321,2 +321,35 @@ /* jshint node: true, maxlen: 200, -W030 */ | ||
describe('#toArray()', function() { | ||
it('should return array by range', function() { | ||
var d1 = new Date(Date.UTC(2012, 2, 1)); | ||
var d2 = new Date(Date.UTC(2012, 2, 5)); | ||
var dr1 = moment.range(d1, d2); | ||
var dr2 = 1000 * 60 * 60 * 24; | ||
var acc = dr1.toArray(dr2); | ||
acc.length.should.eql(5); | ||
acc[0].utc().date().should.eql(1); | ||
acc[1].utc().date().should.eql(2); | ||
acc[2].utc().date().should.eql(3); | ||
acc[3].utc().date().should.eql(4); | ||
acc[4].utc().date().should.eql(5); | ||
}); | ||
it('should return array by shorthand string with exclusive', function() { | ||
var d1 = new Date(Date.UTC(2012, 2, 1)); | ||
var d2 = new Date(Date.UTC(2012, 2, 5)); | ||
var dr1 = moment.range(d1, d2); | ||
var dr2 = 'days'; | ||
var acc = dr1.toArray(dr2, true); | ||
acc.length.should.eql(4); | ||
acc[0].utc().date().should.eql(1); | ||
acc[1].utc().date().should.eql(2); | ||
acc[2].utc().date().should.eql(3); | ||
acc[3].utc().date().should.eql(4); | ||
}); | ||
}); | ||
describe('#contains()', function() { | ||
@@ -323,0 +356,0 @@ it('should work with Date objects', function() { |
60068
326044
1345
387