forked from c-trimm/moment-recur
-
Notifications
You must be signed in to change notification settings - Fork 1
/
moment-recur.min.js
1 lines (1 loc) · 7.68 KB
/
moment-recur.min.js
1
!function(a,b){"object"==typeof exports?module.exports=b(require("moment")):"function"==typeof define&&define.amd?define("moment-recur",["moment"],b):a.moment=b(a.moment)}(this,function(a){var b;if(b="undefined"!=typeof module&&null!==module&&null!=module.exports,"undefined"==typeof a)throw Error("Can't find moment");var c=function(){function a(a,b){for(var c in a)if(a.hasOwnProperty(c)&&parseInt(c,10)<=0)throw Error("Intervals must be greater than zero");return{measure:b.toLowerCase(),units:a}}function b(a,b,c,d){var e=null;e=d.isBefore(c)?c.diff(d,a,!0):d.diff(c,a,!0),"days"==a&&(e=parseInt(e));for(var f in b)if(b.hasOwnProperty(f)&&(f=parseInt(f,10),e%f===0))return!0;return!1}return{create:a,match:b}}(),d=function(){function b(a,b,c){c.forEach(function(c){if(a>c||c>b)throw Error("Value should be in range "+a+" to "+b)})}function c(b,c){var d,e,f,g={};for(d in b)b.hasOwnProperty(d)&&(e=parseInt(d,10),isNaN(e)&&(e=d),f=a().set(c,e).get(c),g[f]=b[d]);return g}function d(a,d){var e=[];"daysOfWeek"===d&&(a=c(a,"days")),"monthsOfYear"===d&&(a=c(a,"months"));for(var f in a)hasOwnProperty.call(a,f)&&e.push(f);return b(g[d].low,g[d].high,e),{measure:d,units:a}}function e(a,b,c){var d=f[a],e=c[d]();if(b[e])return!0;if("date"===d&&e==c.add(1,"months").date(0).format("D")&&31>e)for(;31>=e;){if(b[e])return!0;e++}return!1}var f={daysOfMonth:"date",daysOfWeek:"day",weeksOfMonth:"monthWeek",weeksOfMonthByDay:"monthWeekByDay",weeksOfYear:"weeks",monthsOfYear:"months"},g={daysOfMonth:{low:1,high:31},daysOfWeek:{low:0,high:6},weeksOfMonth:{low:0,high:4},weeksOfMonthByDay:{low:0,high:4},weeksOfYear:{low:0,high:52},monthsOfYear:{low:0,high:11}};return{create:d,match:e}}(),e=function(){function b(){var a,b=l[this.measure];if(!(this instanceof n))throw Error("Private method trigger() was called directly or not called as instance of Recur!");if("undefined"==typeof this.units||null===this.units||!this.measure)return this;if("calendar"!==b&&"interval"!==b)throw Error("Invlid measure provided: "+this.measure);if("interval"===b){if(!this.start)throw Error("Must have a start date set to set an interval!");a=c.create(this.units,this.measure)}if("calendar"===b&&(a=d.create(this.units,this.measure)),this.units=null,this.measure=null,"weeksOfMonthByDay"===a.measure&&!this.hasRule("daysOfWeek"))throw Error("weeksOfMonthByDay must be combined with daysOfWeek");for(var e=0;e<this.rules.length;e++)this.rules[e].measure===a.measure&&this.rules.splice(e,1);return this.rules.push(a),this}function e(a,b,c){var d,e,f=[];if(!(this instanceof n))throw Error("Private method trigger() was called directly or not called as instance of Recur!");if(!this.start&&!this.from)throw Error("Cannot get occurances without start or from date.");if("all"===c&&!this.end)throw Error("Cannot get all occurances without an end date.");if(this.end&&this.start>this.end)throw Error("Start date cannot be later than end date.");if("all"!==c&&!(a>0))return f;for(d=(this.from||this.start).clone(),"all"===c&&this.matches(d,!1)&&(e=b?d.format(b):d.clone(),f.push(e));f.length<(null===a?f.length+1:a)&&("next"===c||"all"===c?d.add(1,"day"):d.subtract(1,"day"),this.matches(d,"all"===c?!1:!0)&&(e=b?d.format(b):d.clone(),f.push(e)),!("all"===c&&d>=this.end)););return f}function f(a,b,c){return a&&c.isBefore(a)?!1:b&&c.isAfter(b)?!1:!0}function g(a){var b={};if("[object Array]"==Object.prototype.toString.call(a))a.forEach(function(a){b[a]=!0});else if(a===Object(a))b=a;else{if("[object Number]"!=Object.prototype.toString.call(a)&&"[object String]"!=Object.prototype.toString.call(a))throw Error("Provide an array, object, string or number when passing units!");b[a]=!0}return b}function h(b,c){for(var d=0,e=b.length;e>d;d++)if(a(b[d]).isSame(c))return!0;return!1}function i(a){switch(a){case"day":return"days";case"week":return"weeks";case"month":return"months";case"year":return"years";case"dayOfWeek":return"daysOfWeek";case"dayOfMonth":return"daysOfMonth";case"weekOfMonth":return"weeksOfMonth";case"weekOfMonthByDay":return"weeksOfMonthByDay";case"weekOfYear":return"weeksOfYear";case"monthOfYear":return"monthsOfYear";default:return a}}function j(a,b,e){var f,g,h,i;for(f=0,g=a.length;g>f;f++)if(h=a[f],i=l[h.measure],"interval"===i){if(!c.match(h.measure,h.units,e,b))return!1}else{if("calendar"!==i)return!1;if(!d.match(h.measure,h.units,b))return!1}return!0}function k(a){return function(b){return this.every.call(this,b,a),this}}var l={days:"interval",weeks:"interval",months:"interval",years:"interval",daysOfWeek:"calendar",daysOfMonth:"calendar",weeksOfMonth:"calendar",weeksOfMonthByDay:"calendar",weeksOfYear:"calendar",monthsOfYear:"calendar"},m={days:"day",weeks:"week",months:"month",years:"year",daysOfWeek:"dayOfWeek",daysOfMonth:"dayOfMonth",weeksOfMonth:"weekOfMonth",weeksOfMonthByDay:"weekOfMonthByDay",weeksOfYear:"weekOfYear",monthsOfYear:"monthOfYear"},n=function(b){b.start&&(this.start=a(b.start).dateOnly()),b.end&&(this.end=a(b.end).dateOnly()),this.rules=b.rules||[];var c=b.exceptions||[];this.exceptions=[];for(var d=0;d<c.length;d++)this.except(c[d]);return this.units=null,this.measure=null,this.from=null,this};n.prototype.startDate=function(b){return null===b?(this.start=null,this):b?(this.start=a(b).dateOnly(),this):this.start},n.prototype.endDate=function(b){return null===b?(this.end=null,this):b?(this.end=a(b).dateOnly(),this):this.end},n.prototype.fromDate=function(b){return null===b?(this.from=null,this):b?(this.from=a(b).dateOnly(),this):this.from},n.prototype.save=function(){var b={};this.start&&a(this.start).isValid()&&(b.start=this.start.format("L")),this.end&&a(this.end).isValid()&&(b.end=this.end.format("L")),b.exceptions=[];for(var c=0,d=this.exceptions.length;d>c;c++)b.exceptions.push(this.exceptions[c].format("L"));return b.rules=this.rules,b},n.prototype.repeats=function(){return this.rules.length>0?!0:!1},n.prototype.every=function(a,c){return"undefined"!=typeof a&&null!==a&&(this.units=g(a)),"undefined"!=typeof c&&null!==c&&(this.measure=i(c)),b.call(this)},n.prototype.except=function(b){return b=a(b).dateOnly(),this.exceptions.push(b),this},n.prototype.forget=function(b){var c,d,e=a(b);if(e.isValid()){for(e=e.dateOnly(),c=0,d=this.exceptions.length;d>c;c++)if(e.isSame(this.exceptions[c]))return this.exceptions.splice(c,1),this;return this}for(c=0,d=this.rules.length;d>c;c++)this.rules[c].measure===i(b)&&this.rules.splice(c,1)},n.prototype.hasRule=function(a){var b,c;for(b=0,c=this.rules.length;c>b;b++)if(this.rules[b].measure===i(a))return!0;return!1},n.prototype.matches=function(b,c){var d=a(b).dateOnly();if(!d.isValid())throw Error("Invalid date supplied to match method: "+b);return c||f(this.start,this.end,d)?h(this.exceptions,d)?!1:j(this.rules,d,this.start)?!0:!1:!1},n.prototype.next=function(a,b){return e.call(this,a,b,"next")},n.prototype.previous=function(a,b){return e.call(this,a,b,"previous")},n.prototype.all=function(a){return e.call(this,null,a,"all")};for(var o in m)l.hasOwnProperty(o)&&(n.prototype[o]=n.prototype[m[o]]=k(o));return n}();return a.recur=function(b,c){return new e(b!==Object(b)||a.isMoment(b)?{start:b,end:c}:b)},a.fn.recur=function(b,c){return b!==Object(b)||a.isMoment(b)?(c||(c=b,b=void 0),b||(b=this),new e({start:b,end:c,moment:this})):("undefined"==typeof b.start&&(b.start=this),new e(b))},a.fn.monthWeek=function(){var a=this.clone().startOf("month").startOf("week"),b=this.clone().startOf("week");return b.diff(a,"weeks")},a.fn.monthWeekByDay=function(){var a,b,c,d;return a=this.clone(),b=this.clone().startOf("month").startOf("week"),c=this.clone().startOf("week"),d=c.diff(b,"weeks"),a.subtract(d,"weeks").month()===this.clone().month()?d:d-1},a.fn.dateOnly=function(){return this.tz&&"function"==typeof a.tz?a.tz(this.format("YYYY/MM/DD"),"UTC"):this.hours(0).minutes(0).seconds(0).milliseconds(0).add(this.utcOffset(),"minute").utcOffset(0)},a});