moment-range
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -127,4 +127,21 @@ var DateRange, INTERVALS; | ||
} | ||
/** | ||
* Merge date ranges if they intersect. | ||
* | ||
* @param {!DateRange} other A date range to add to this one | ||
* | ||
* @return {!DateRange|null} | ||
* | ||
*/ | ||
}; | ||
DateRange.prototype.add = function(other) { | ||
if (this.overlaps(other)) { | ||
return new DateRange(moment.min(this.start, other.start), moment.max(this.end, other.end)); | ||
} else { | ||
return null; | ||
} | ||
}; | ||
/** | ||
@@ -131,0 +148,0 @@ * Subtract one range from another. |
@@ -1,1 +0,1 @@ | ||
var DateRange,INTERVALS;INTERVALS={year:!0,month:!0,week:!0,day:!0,hour:!0,minute:!0,second:!0},DateRange=function(){function a(a,b){this.start=moment(a),this.end=moment(b)}return a.prototype.contains=function(b){return b instanceof a?this.start<=b.start&&this.end>=b.end:this.start<=b&&b<=this.end},a.prototype._by_string=function(a,b){var c,d;for(c=moment(this.start),d=[];this.contains(c);)b.call(this,c.clone()),d.push(c.add(1,a));return d},a.prototype._by_range=function(a,b){var c,d,e,f;if(d=Math.floor(this/a),1/0===d)return this;for(f=[],c=e=0;d>=0?d>=e:e>=d;c=d>=0?++e:--e)f.push(b.call(this,moment(this.start.valueOf()+a.valueOf()*c)));return f},a.prototype.overlaps=function(a){return null!==this.intersect(a)},a.prototype.intersect=function(b){var c,d,e,f,g,h,i,j;return this.start<=(d=b.start)&&d<(c=this.end)&&c<b.end?new a(b.start,this.end):b.start<(f=this.start)&&f<(e=b.end)&&e<=this.end?new a(this.start,b.end):b.start<(h=this.start)&&h<(g=this.end)&&g<b.end?this:this.start<=(j=b.start)&&j<(i=b.end)&&i<=this.end?b:null},a.prototype.subtract=function(b){var c,d,e,f,g,h,i,j;return null===this.intersect(b)?[this]:b.start<=(d=this.start)&&d<(c=this.end)&&c<=b.end?[]:b.start<=(f=this.start)&&f<(e=b.end)&&e<this.end?[new a(b.end,this.end)]:this.start<(h=b.start)&&h<(g=this.end)&&g<=b.end?[new a(this.start,b.start)]:this.start<(j=b.start)&&j<(i=b.end)&&i<this.end?[new a(this.start,b.start),new a(b.end,this.end)]:void 0},a.prototype.by=function(a,b){return"string"==typeof a?this._by_string(a,b):this._by_range(a,b),this},a.prototype.valueOf=function(){return this.end-this.start},a.prototype.toDate=function(){return[this.start.toDate(),this.end.toDate()]},a.prototype.isSame=function(a){return this.start.isSame(a.start)&&this.end.isSame(a.end)},a.prototype.diff=function(a){return null==a&&(a=void 0),this.end.diff(this.start,a)},a}(),moment.fn.range=function(a,b){return a in INTERVALS?new DateRange(moment(this).startOf(a),moment(this).endOf(a)):new DateRange(a,b)},moment.range=function(a,b){return new DateRange(a,b)},moment.fn.within=function(a){return a.contains(this._d)}; | ||
var DateRange,INTERVALS;INTERVALS={year:!0,month:!0,week:!0,day:!0,hour:!0,minute:!0,second:!0},DateRange=function(){function a(a,b){this.start=moment(a),this.end=moment(b)}return a.prototype.contains=function(b){return b instanceof a?this.start<=b.start&&this.end>=b.end:this.start<=b&&b<=this.end},a.prototype._by_string=function(a,b){var c,d;for(c=moment(this.start),d=[];this.contains(c);)b.call(this,c.clone()),d.push(c.add(1,a));return d},a.prototype._by_range=function(a,b){var c,d,e,f;if(d=Math.floor(this/a),1/0===d)return this;for(f=[],c=e=0;d>=0?d>=e:e>=d;c=d>=0?++e:--e)f.push(b.call(this,moment(this.start.valueOf()+a.valueOf()*c)));return f},a.prototype.overlaps=function(a){return null!==this.intersect(a)},a.prototype.intersect=function(b){var c,d,e,f,g,h,i,j;return this.start<=(d=b.start)&&d<(c=this.end)&&c<b.end?new a(b.start,this.end):b.start<(f=this.start)&&f<(e=b.end)&&e<=this.end?new a(this.start,b.end):b.start<(h=this.start)&&h<(g=this.end)&&g<b.end?this:this.start<=(j=b.start)&&j<(i=b.end)&&i<=this.end?b:null},a.prototype.add=function(b){return this.overlaps(b)?new a(moment.min(this.start,b.start),moment.max(this.end,b.end)):null},a.prototype.subtract=function(b){var c,d,e,f,g,h,i,j;return null===this.intersect(b)?[this]:b.start<=(d=this.start)&&d<(c=this.end)&&c<=b.end?[]:b.start<=(f=this.start)&&f<(e=b.end)&&e<this.end?[new a(b.end,this.end)]:this.start<(h=b.start)&&h<(g=this.end)&&g<=b.end?[new a(this.start,b.start)]:this.start<(j=b.start)&&j<(i=b.end)&&i<this.end?[new a(this.start,b.start),new a(b.end,this.end)]:void 0},a.prototype.by=function(a,b){return"string"==typeof a?this._by_string(a,b):this._by_range(a,b),this},a.prototype.valueOf=function(){return this.end-this.start},a.prototype.toDate=function(){return[this.start.toDate(),this.end.toDate()]},a.prototype.isSame=function(a){return this.start.isSame(a.start)&&this.end.isSame(a.end)},a.prototype.diff=function(a){return null==a&&(a=void 0),this.end.diff(this.start,a)},a}(),moment.fn.range=function(a,b){return a in INTERVALS?new DateRange(moment(this).startOf(a),moment(this).endOf(a)):new DateRange(a,b)},moment.range=function(a,b){return new DateRange(a,b)},moment.fn.within=function(a){return a.contains(this._d)}; |
@@ -138,4 +138,21 @@ (function(root, factory) { | ||
} | ||
/** | ||
* Merge date ranges if they intersect. | ||
* | ||
* @param {!DateRange} other A date range to add to this one | ||
* | ||
* @return {!DateRange|null} | ||
* | ||
*/ | ||
}; | ||
DateRange.prototype.add = function(other) { | ||
if (this.overlaps(other)) { | ||
return new DateRange(moment.min(this.start, other.start), moment.max(this.end, other.end)); | ||
} else { | ||
return null; | ||
} | ||
}; | ||
/** | ||
@@ -142,0 +159,0 @@ * Subtract one range from another. |
@@ -1,1 +0,1 @@ | ||
!function(a,b){"object"==typeof exports?module.exports=b(require("moment")):"function"==typeof define&&define.amd?define(["moment"],b):a.moment=b(a.moment)}(this,function(a){var b,c;return c={year:!0,month:!0,week:!0,day:!0,hour:!0,minute:!0,second:!0},b=function(){function b(b,c){this.start=a(b),this.end=a(c)}return b.prototype.contains=function(a){return a instanceof b?this.start<=a.start&&this.end>=a.end:this.start<=a&&a<=this.end},b.prototype._by_string=function(b,c){var d,e;for(d=a(this.start),e=[];this.contains(d);)c.call(this,d.clone()),e.push(d.add(1,b));return e},b.prototype._by_range=function(b,c){var d,e,f,g;if(e=Math.floor(this/b),1/0===e)return this;for(g=[],d=f=0;e>=0?e>=f:f>=e;d=e>=0?++f:--f)g.push(c.call(this,a(this.start.valueOf()+b.valueOf()*d)));return g},b.prototype.overlaps=function(a){return null!==this.intersect(a)},b.prototype.intersect=function(a){var c,d,e,f,g,h,i,j;return this.start<=(d=a.start)&&d<(c=this.end)&&c<a.end?new b(a.start,this.end):a.start<(f=this.start)&&f<(e=a.end)&&e<=this.end?new b(this.start,a.end):a.start<(h=this.start)&&h<(g=this.end)&&g<a.end?this:this.start<=(j=a.start)&&j<(i=a.end)&&i<=this.end?a:null},b.prototype.subtract=function(a){var c,d,e,f,g,h,i,j;return null===this.intersect(a)?[this]:a.start<=(d=this.start)&&d<(c=this.end)&&c<=a.end?[]:a.start<=(f=this.start)&&f<(e=a.end)&&e<this.end?[new b(a.end,this.end)]:this.start<(h=a.start)&&h<(g=this.end)&&g<=a.end?[new b(this.start,a.start)]:this.start<(j=a.start)&&j<(i=a.end)&&i<this.end?[new b(this.start,a.start),new b(a.end,this.end)]:void 0},b.prototype.by=function(a,b){return"string"==typeof a?this._by_string(a,b):this._by_range(a,b),this},b.prototype.valueOf=function(){return this.end-this.start},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 null==a&&(a=void 0),this.end.diff(this.start,a)},b}(),a.fn.range=function(d,e){return d in c?new b(a(this).startOf(d),a(this).endOf(d)):new b(d,e)},a.range=function(a,c){return new b(a,c)},a.fn.within=function(a){return a.contains(this._d)},a}); | ||
!function(a,b){"object"==typeof exports?module.exports=b(require("moment")):"function"==typeof define&&define.amd?define(["moment"],b):a.moment=b(a.moment)}(this,function(a){var b,c;return c={year:!0,month:!0,week:!0,day:!0,hour:!0,minute:!0,second:!0},b=function(){function b(b,c){this.start=a(b),this.end=a(c)}return b.prototype.contains=function(a){return a instanceof b?this.start<=a.start&&this.end>=a.end:this.start<=a&&a<=this.end},b.prototype._by_string=function(b,c){var d,e;for(d=a(this.start),e=[];this.contains(d);)c.call(this,d.clone()),e.push(d.add(1,b));return e},b.prototype._by_range=function(b,c){var d,e,f,g;if(e=Math.floor(this/b),1/0===e)return this;for(g=[],d=f=0;e>=0?e>=f:f>=e;d=e>=0?++f:--f)g.push(c.call(this,a(this.start.valueOf()+b.valueOf()*d)));return g},b.prototype.overlaps=function(a){return null!==this.intersect(a)},b.prototype.intersect=function(a){var c,d,e,f,g,h,i,j;return this.start<=(d=a.start)&&d<(c=this.end)&&c<a.end?new b(a.start,this.end):a.start<(f=this.start)&&f<(e=a.end)&&e<=this.end?new b(this.start,a.end):a.start<(h=this.start)&&h<(g=this.end)&&g<a.end?this:this.start<=(j=a.start)&&j<(i=a.end)&&i<=this.end?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,d,e,f,g,h,i,j;return null===this.intersect(a)?[this]:a.start<=(d=this.start)&&d<(c=this.end)&&c<=a.end?[]:a.start<=(f=this.start)&&f<(e=a.end)&&e<this.end?[new b(a.end,this.end)]:this.start<(h=a.start)&&h<(g=this.end)&&g<=a.end?[new b(this.start,a.start)]:this.start<(j=a.start)&&j<(i=a.end)&&i<this.end?[new b(this.start,a.start),new b(a.end,this.end)]:void 0},b.prototype.by=function(a,b){return"string"==typeof a?this._by_string(a,b):this._by_range(a,b),this},b.prototype.valueOf=function(){return this.end-this.start},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 null==a&&(a=void 0),this.end.diff(this.start,a)},b}(),a.fn.range=function(d,e){return d in c?new b(a(this).startOf(d),a(this).endOf(d)):new b(d,e)},a.range=function(a,c){return new b(a,c)},a.fn.within=function(a){return a.contains(this._d)},a}); |
@@ -24,3 +24,3 @@ { | ||
}, | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"engines": { | ||
@@ -43,4 +43,4 @@ "node": "*" | ||
"grunt-umd": "~1.3.0", | ||
"mocha": "1.3.0", | ||
"should": "0.6.3" | ||
"mocha": "^2.1.0", | ||
"should": "^4.6.5" | ||
}, | ||
@@ -47,0 +47,0 @@ "repository": { |
@@ -28,3 +28,3 @@ moment-range | ||
### Contains / Within / Overlaps / Intersect / Subtract | ||
### Contains / Within / Overlaps / Intersect / Add / Subtract | ||
@@ -37,3 +37,3 @@ Check to see if your range contains a date/moment: | ||
var lol = new Date(2012, 4, 15); | ||
var wat = new Date(2012, 2, 27); | ||
var wat = new Date(2012, 4, 27); | ||
var range = moment().range(start, end); | ||
@@ -69,2 +69,11 @@ var range2 = moment().range(lol, wat); | ||
Add/combine/merge overlapping ranges. | ||
``` javascript | ||
range.add(range2); // [moment().range(start, wat)] | ||
var range3 = moment.range(new Date(2012, 3, 1), new Date(2012, 3, 15); | ||
range.add(range3); // [null] | ||
``` | ||
Subtracting one range from another. | ||
@@ -179,3 +188,5 @@ | ||
Thanks to the fine people at [cdnjs][cdnjs], you can link to moment-range from the [cdnjs servers][cdnjs-moment-range]. | ||
### NPM | ||
@@ -231,4 +242,6 @@ | ||
[cdnjs]: https://github.com/cdnjs/cdnjs | ||
[cdnjs-moment-range]: https://cdnjs.com/libraries/moment-range | ||
[moment]: http://momentjs.com/ | ||
[node]: http://nodejs.org/ | ||
[unlicense]: http://unlicense.org/ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
50750
650
243