UNPKG

moment-recur-ts

Version:

A momentjs plugin for matching and generating recurring dates.

2 lines 15.5 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("moment")):"function"==typeof define&&define.amd?define("moment-recur-ts",["moment"],t):"object"==typeof exports?exports["moment-recur-ts"]=t(require("moment")):e["moment-recur-ts"]=t(e.moment)}("undefined"!=typeof self?self:this,function(e){return function(e){function t(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,t),i.l=!0,i.exports}var r={};return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:n})},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=1)}([function(t,r){t.exports=e},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=r(0),i=r(2);n.fn.monthWeek=function(e){if(void 0===e){var t=this.clone().startOf("month").startOf("week");return this.clone().startOf("week").diff(t,"weeks")}var r=e-this.monthWeek();return this.clone().add(r,"weeks")},n.fn.monthWeekByDay=function(e){if(void 0===e)return Math.floor((this.date()-1)/7);var t=e-this.monthWeekByDay();return this.clone().add(t,"weeks")},n.fn.dateOnly=function(){return this.isValid()?n.utc(this.format("YYYY-MM-DD")):this},n.recur=function(e,t){if("object"==typeof e&&!n.isMoment(e)){var r=e;return new i.Recur(r)}return new i.Recur({start:e,end:t})},n.fn.recur=function(e,t){if(e===Object(e)&&!n.isMoment(e)){var r=e;return void 0===r.start&&(r.start=this),new i.Recur(r)}return t||(t=e,e=void 0),e||(e=this),new i.Recur({start:e,end:t})}},function(e,t,r){"use strict";var n=this&&this.__generator||function(e,t){function r(e){return function(t){return n([e,t])}}function n(r){if(i)throw new TypeError("Generator is already executing.");for(;u;)try{if(i=1,o&&(s=o[2&r[0]?"return":r[0]?"throw":"next"])&&!(s=s.call(o,r[1])).done)return s;switch(o=0,s&&(r=[0,s.value]),r[0]){case 0:case 1:s=r;break;case 4:return u.label++,{value:r[1],done:!1};case 5:u.label++,o=r[1],r=[0];continue;case 7:r=u.ops.pop(),u.trys.pop();continue;default:if(s=u.trys,!(s=s.length>0&&s[s.length-1])&&(6===r[0]||2===r[0])){u=0;continue}if(3===r[0]&&(!s||r[1]>s[0]&&r[1]<s[3])){u.label=r[1];break}if(6===r[0]&&u.label<s[1]){u.label=s[1],s=r;break}if(s&&u.label<s[2]){u.label=s[2],u.ops.push(r);break}s[2]&&u.ops.pop(),u.trys.pop();continue}r=t.call(e,u)}catch(e){r=[6,e],o=0}finally{i=s=0}if(5&r[0])throw r[1];return{value:r[0]?r[1]:void 0,done:!0}}var i,o,s,a,u={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]};return a={next:r(0),throw:r(1),return:r(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a},i=this&&this.__values||function(e){var t="function"==typeof Symbol&&e[Symbol.iterator],r=0;return t?t.call(e):{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}}};Object.defineProperty(t,"__esModule",{value:!0});var o=r(0),s=r(3),a=function(){function e(e){var t=this;this.reversed=!1,this.maximumYears=1e3,e.start&&(this.start=o(e.start).dateOnly()),e.end&&(this.end=o(e.end).dateOnly()),this.rules=(e.rules||[]).map(function(e){return s.Rule.factory(e.units,e.measure,t.start)});var r=e.exceptions||[];return this.exceptions=r.map(function(e){return o(e).dateOnly()}),this.units=null,this.measure=null,this.from=null,this.addMeasureFunctions(),this}return e.prototype.startDate=function(e){if(null===e)return this.start=null,this;if(e)return this.start=o(e).dateOnly(),this;if(!this.start)throw new Error("No start date defined for recurrence.");return this.start},e.prototype.endDate=function(e){if(null===e)return this.end=null,this;if(e)return this.end=o(e).dateOnly(),this;if(!this.end)throw new Error("No end date defined for recurrence.");return this.end},e.prototype.fromDate=function(e){if(null===e)return this.from=null,this;if(e)return this.from=o(e).dateOnly(),this;if(!this.from)throw new Error("No from date defined for recurrence.");return this.from},e.prototype.maxYears=function(e){return e?(this.maximumYears=e,this):this.maximumYears},e.prototype.save=function(){var e={};return this.start&&o(this.start).isValid()&&(e.start=this.start.format("YYYY-MM-DD")),this.end&&o(this.end).isValid()&&(e.end=this.end.format("YYYY-MM-DD")),e.exceptions=this.exceptions.map(function(e){return e.format("YYYY-MM-DD")}),e.rules=this.rules,e},e.prototype.repeats=function(){return this.rules.length>0},e.prototype.every=function(e,t){if(null!=e&&(this.units=e),null!=t&&(this.measure=t),!this.measure)return this;var r=s.Rule.factory(this.units,this.measure,this.start);if("weeksOfMonthByDay"===r.measure&&!this.hasRule("daysOfWeek"))throw new Error("weeksOfMonthByDay must be combined with daysOfWeek");return this.units=null,this.measure=null,this.rules=this.rules.filter(function(e){return e.measure!==r.measure}),this.rules.push(r),this},e.prototype.except=function(e){return e=o(e).dateOnly(),this.exceptions.push(e),this},e.prototype.forget=function(e,t){if(!e)throw new Error("Invalid input for recurrence forget: "+e);try{var r=s.Rule.normalizeMeasure(e);return this.rules=this.rules.filter(function(e){return e.measure!==r}),this}catch(r){var n=o(e,t);if(n.isValid())return n=n.dateOnly(),this.exceptions=this.exceptions.filter(function(e){return!n.isSame(e)}),this;throw new Error("Invalid input for recurrence forget: "+e)}},e.prototype.hasRule=function(e){return-1!==this.rules.findIndex(function(t){return t.measure===s.Rule.normalizeMeasure(e)})},e.prototype.matches=function(e,t){var r=o(e).dateOnly();if(!r.isValid())throw Error("Invalid date supplied to match method: "+e);return!(!t&&!this.inRange(r))&&(!this.isException(r)&&!!this.matchAllRules(r))},e.prototype[Symbol.iterator]=function(){var e,t;return n(this,function(r){switch(r.label){case 0:if(!(e=this.from||this.start)||!e.isValid())throw Error("Cannot get occurrences without start or from date.");if(this.end&&e>this.end)throw Error("Start date cannot be later than end date.");return t=e.clone(),this.matchAllRules(t)?[4,t.clone()]:[3,2];case 1:r.sent(),r.label=2;case 2:try{t=this.reversed?this.findPreviousMatch(t):this.findNextMatch(t)}catch(e){if(e instanceof RangeError)return[2,void 0];throw e}return this.end&&t.isAfter(this.end)?[3,5]:this.isException(t)?[3,4]:[4,t.clone()];case 3:r.sent(),r.label=4;case 4:return[3,2];case 5:return[2]}})},e.prototype.reverse=function(){return this.reversed=!this.reversed,this},e.prototype.all=function(e){if(!this.end||!this.end.isValid())throw Error("Cannot get all occurrences without an end date.");this.reversed=!1;var t=[];try{for(var r=i(this),n=r.next();!n.done;n=r.next()){var o=n.value;t.push(e?o.format(e):o)}}catch(e){s={error:e}}finally{try{n&&!n.done&&(a=r.return)&&a.call(r)}finally{if(s)throw s.error}}return t;var s,a},e.prototype.next=function(e,t){if(!e)return[];var r=[],n=0;this.reversed=!1;try{for(var o=i(this),s=o.next();!s.done;s=o.next()){var a=s.value;if(this.start&&a.isSame(this.start)||(r.push(t?a.format(t):a),n++),n>=e)break}}catch(e){u={error:e}}finally{try{s&&!s.done&&(f=o.return)&&f.call(o)}finally{if(u)throw u.error}}return r;var u,f},e.prototype.previous=function(e,t){if(!e)return[];var r=[],n=0;this.reversed=!0;try{for(var o=i(this),s=o.next();!s.done;s=o.next()){var a=s.value;if(this.start&&a.isSame(this.start)||(r.push(t?a.format(t):a),n++),n>=e)break}}catch(e){u={error:e}}finally{try{s&&!s.done&&(f=o.return)&&f.call(o)}finally{if(u)throw u.error}}return r;var u,f},e.prototype.addMeasureFunctions=function(){var t=this;try{for(var r=i(Object.keys(s.Rule.MeasureSingleToPlural)),n=r.next();!n.done;n=r.next()){var o=n.value;!function(r){var n=s.Rule.MeasureSingleToPlural[r];e.prototype[r]=function(e){return t.every(e,n),t},e.prototype[n]=function(e){return t.every(e,n),t}}(o)}}catch(e){a={error:e}}finally{try{n&&!n.done&&(u=r.return)&&u.call(r)}finally{if(a)throw a.error}}var a,u},e.prototype.inRange=function(e){return(!this.start||!e.isBefore(this.start))&&(!this.end||!e.isAfter(this.end))},e.prototype.isException=function(e){try{for(var t=i(this.exceptions),r=t.next();!r.done;r=t.next()){var n=r.value;if(o(n).isSame(e))return!0}}catch(e){s={error:e}}finally{try{r&&!r.done&&(a=t.return)&&a.call(t)}finally{if(s)throw s.error}}return!1;var s,a},e.prototype.matchAllRules=function(e){try{for(var t=i(this.rules),r=t.next();!r.done;r=t.next()){if(!r.value.match(e))return!1}}catch(e){n={error:e}}finally{try{r&&!r.done&&(o=t.return)&&o.call(t)}finally{if(n)throw n.error}}return!0;var n,o},e.prototype.findNextMatch=function(e){for(var t=e.clone().add(1,"day"),r=e.clone().add(this.maximumYears,"years"),n=!1;!n;){t.subtract(1,"day");var i=this.rules.map(function(e){return e.next(t,r)});i.sort(function(e,t){return e.diff(t)}),t=i[i.length-1],n=t.isSame(i[0])}return t},e.prototype.findPreviousMatch=function(e){for(var t=e.clone().subtract(1,"day"),r=e.clone().subtract(this.maximumYears,"years"),n=!1;!n;){t.add(1,"day");var i=this.rules.map(function(e){return e.previous(t,r)});i.sort(function(e,t){return t.diff(e)}),t=i[i.length-1],n=t.isSame(i[0])}return t},e}();t.Recur=a},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=r(4),i=r(5);!function(e){function t(e,t,s){var a=o(t);switch(a){case"days":case"weeks":case"months":case"years":return new i.Interval(r(e),a,s);case"daysOfWeek":case"daysOfMonth":case"weeksOfMonth":case"weeksOfMonthByDay":case"weeksOfYear":case"monthsOfYear":return new n.Calendar(r(e),a)}}function r(e){if(null==e)throw new Error("Units not defined for recurrence rule.");if(Array.isArray(e))return e;if("object"==typeof e)return Object.keys(e);if("number"==typeof e)return[e];if("string"==typeof e)return[e];throw new Error("Provide an array, object, string or number when passing units!")}function o(t){if("string"==typeof t){if(e.MeasureSingleToPlural[t])return e.MeasureSingleToPlural[t];for(var r in e.MeasureSingleToPlural)if(e.MeasureSingleToPlural[r]===t)return t}throw new Error("Invalid Measure for recurrence: "+t)}e.MeasureSingleToPlural={day:"days",week:"weeks",month:"months",year:"years",dayOfWeek:"daysOfWeek",dayOfMonth:"daysOfMonth",weekOfMonth:"weeksOfMonth",weekOfMonthByDay:"weeksOfMonthByDay",weekOfYear:"weeksOfYear",monthOfYear:"monthsOfYear"},e.factory=t,e.normalizeMeasure=o}(t.Rule||(t.Rule={}))},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=r(0),i=function(){function e(t,r){this.measure=r,this.units=this.normalizeUnits(t),this.range=e.ranges[this.measure].range,this.period=e.ranges[this.measure].period}return e.prototype.match=function(e){var t=this.periodUnit(e);return-1!==this.units.indexOf(t)||-1===this.units[0]&&t===this.periodUnit(n(e).endOf(this.range))},e.prototype.next=function(e,t){var r=e.clone();if(!this.isLastDayOfPeriod(r)){var i=n(r).add(1,"day");if(this.match(i))return i}for(;;){var o=this.nextPeriod(r);if(o)return o;if(r=this.incrementRange(r,1),this.match(r))return r;if(r.isSameOrAfter(t))throw new RangeError("Recurrence Year limit exceeded.")}},e.prototype.previous=function(e,t){var r=e.clone();if(!this.isFirstDayOfPeriod(r)){var i=n(r).subtract(1,"day");if(this.match(i))return i}for(;;){var o=this.previousPeriod(r);if(o)return o;if(r=this.decrementRange(r,1),this.match(r))return r;if(r.isSameOrBefore(t))throw new RangeError("Recurrence Year limit exceeded.")}},e.prototype.normalizeUnits=function(t){var r=this,i=e.ranges[this.measure].low,o=e.ranges[this.measure].high;return t.map(function(e){if("last"===e&&(e=-1),"number"!=typeof e&&(e="daysOfWeek"===r.measure?n().set("days",e).get("days"):"monthsOfYear"===r.measure?n().set("months",e).get("months"):+e),!Number.isInteger(e))throw new TypeError("Invalid calendar unit in recurrence: "+e);if((e<i||e>o)&&-1!==e)throw new RangeError("Value should be in range "+i+" to "+o);return e}).sort(function(e,t){return e-t})},e.prototype.periodUnit=function(e,t){switch(this.measure){case"daysOfWeek":return e.day(t);case"daysOfMonth":return e.date(t);case"weeksOfMonth":return e.monthWeek(t);case"weeksOfMonthByDay":return e.monthWeekByDay(t);case"weeksOfYear":return e.week(t);case"monthsOfYear":return e.month(t)}},e.prototype.nextPeriod=function(e){var t=this,r=this.periodUnit(e),n=this.units.map(function(r){return-1===r?t.periodUnit(e.clone().endOf(t.range)):r}).find(function(e){return e>r});return void 0!==n?this.periodUnit(e.clone(),n).startOf(this.period):"weeksOfYear"===this.measure&&-1!==this.units.indexOf(1)?e.clone().endOf("year").startOf("week"):void 0},e.prototype.previousPeriod=function(e){var t=this,r=this.periodUnit(e);"weeksOfYear"===this.measure&&11===e.month()&&1===e.week()&&(r=53);var n=this.units.map(function(r){return-1===r?t.periodUnit(e.clone().endOf(t.range)):r}).reverse().find(function(e){return e<r});return void 0!==n?("weeksOfYear"===this.measure&&53===r&&e.week(0),this.periodUnit(e.clone(),n).endOf(this.period)):"weeksOfYear"===this.measure&&this.units.some(function(e){return 52>=e&&e<=53})?e.clone().startOf("year").endOf("week"):void 0},e.prototype.incrementRange=function(e,t){return e.add(t,this.range).startOf(this.range)},e.prototype.decrementRange=function(e,t){return e.subtract(t,this.range).endOf(this.range)},e.prototype.isLastDayOfPeriod=function(e){return"weeksOfMonthByDay"===this.measure?e.monthWeekByDay()!==n(e).add(1,"day").monthWeekByDay():"day"===this.period||e.isSame(n(e).endOf(this.period))},e.prototype.isFirstDayOfPeriod=function(e){return"weeksOfMonthByDay"===this.measure?e.monthWeekByDay()!==n(e).subtract(1,"day").monthWeekByDay():"day"===this.period||e.isSame(n(e).startOf(this.period))},e.ranges={daysOfWeek:{period:"day",range:"week",low:0,high:6},daysOfMonth:{period:"day",range:"month",low:1,high:31},weeksOfMonth:{period:"week",range:"month",low:0,high:4},weeksOfMonthByDay:{period:"week",range:"month",low:0,high:4},weeksOfYear:{period:"week",range:"year",low:1,high:53},monthsOfYear:{period:"month",range:"year",low:0,high:11}},e}();t.Calendar=i},function(e,t,r){"use strict";var n=this&&this.__values||function(e){var t="function"==typeof Symbol&&e[Symbol.iterator],r=0;return t?t.call(e):{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}}};Object.defineProperty(t,"__esModule",{value:!0});var i=function(){function e(e,t,r){if(!r)throw new Error("Must have a start date set to set an interval!");this.start=r.clone(),this.measure=t,this.units=this.normalizeUnits(e)}return e.prototype.match=function(e){var t="days"!==this.measure,r=Math.abs(this.start.diff(e,this.measure,t));try{for(var i=n(this.units),o=i.next();!o.done;o=i.next()){if(r%o.value==0)return!0}}catch(e){s={error:e}}finally{try{o&&!o.done&&(a=i.return)&&a.call(i)}finally{if(s)throw s.error}}return!1;var s,a},e.prototype.next=function(e){var t=e.diff(this.start,this.measure),r=this.units.map(function(e){return(Math.floor(t/e)+1)*e});return r.sort(function(e,t){return e-t}),this.start.clone().add(r[0],this.measure)},e.prototype.previous=function(e){var t=this.start.diff(e,this.measure),r=this.units.map(function(e){return(Math.floor(t/e)+1)*e});return r.sort(function(e,t){return t-e}),this.start.clone().subtract(r[0],this.measure)},e.prototype.normalizeUnits=function(e){return e.map(function(e){if((e=+e)<=0)throw new Error("Intervals must be greater than zero.");if(!Number.isInteger(e))throw new Error("Intervals must be integers.");return e}).sort(function(e,t){return e-t})},e}();t.Interval=i}])}); //# sourceMappingURL=moment-recur-ts.min.js.map