add BYDAY property to recurrence events

This commit is contained in:
jmuia
2014-08-20 14:46:26 -04:00
parent 75369c0184
commit 98b22dd60a
5 changed files with 41 additions and 4 deletions
+1
View File
@@ -29,6 +29,7 @@ The `rrule` object has the following properties:
- `until` : A date string representing the date on which to end repitition. Must be friendly to `Date()`
- `count` : Alternative to until. Repeat the event `count` times. Must be an integer
- `interval` : The interval of `freq` to recur at. For example, if `freq` is `WEEKLY` and `interval` is `2`, the event will repeat every 2 weeks. Must be an integer.
- `byday` : Which days of the week the event is to occur. An array containing any of `SU`, `MO`, `TU`, `WE`, `TH`, `FR`, `SA`.
The four properties described above are not exhaustive of recurrence rule capabilities. If extra functionality is required, you can set the `rrule.rule` property to a full recurrence rule string. In this case, none of the four properties described above are necessary. See [this page](http://www.kanzaki.com/docs/ical/rrule.html) for a description of recurrence rules.
+2 -2
View File
File diff suppressed because one or more lines are too long
+26
View File
@@ -16,6 +16,7 @@ var ics = function() {
'VERSION:2.0'
].join(SEPARATOR);
var calendarEnd = SEPARATOR + 'END:VCALENDAR';
var BYDAY_VALUES = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'];
return {
/**
@@ -77,6 +78,27 @@ var ics = function() {
throw "Recurrence rule 'count' must be an integer";
}
}
if (typeof rrule.byday !== 'undefined') {
if ( (Object.prototype.toString.call(rrule.byday) !== '[object Array]') ) {
throw "Recurrence rule 'byday' must be an array";
}
if (rrule.byday.length > 7) {
throw "Recurrence rule 'byday' array must not be longer than the 7 days in a week";
}
// Filter any possible repeats
rrule.byday = rrule.byday.filter(function(elem, pos) {
return rrule.byday.indexOf(elem) == pos;
});
for (var d in rrule.byday) {
if (BYDAY_VALUES.indexOf(rrule.byday[d]) < 0) {
throw "Recurrence rule 'byday' values must include only the following: 'SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'";
}
}
}
}
}
@@ -129,6 +151,10 @@ var ics = function() {
if (rrule.count) {
rruleString += ';COUNT=' + rrule.count;
}
if (rrule.byday && rrule.byday.length > 0) {
rruleString += ';BYDAY=' + rrule.byday.join(',');
}
}
}
Vendored
+2 -2
View File
@@ -1,2 +1,2 @@
/*! ics.js Mon Aug 18 2014 20:11:18 */
var ics=function(){"use strict";if(navigator.userAgent.indexOf("MSIE")>-1&&-1==navigator.userAgent.indexOf("MSIE 10"))return void console.log("Unsupported Browser");var a=-1!==navigator.appVersion.indexOf("Win")?"\r\n":"\n",b=[],c=["BEGIN:VCALENDAR","VERSION:2.0"].join(a),d=a+"END:VCALENDAR";return{events:function(){return b},calendar:function(){return c+a+b.join(a)+d},addEvent:function(c,d,e,f,g,h){if("undefined"==typeof c||"undefined"==typeof d||"undefined"==typeof e||"undefined"==typeof f||"undefined"==typeof g)return!1;if(h&&!h.rule){if("YEARLY"!==h.freq&&"MONTHLY"!==h.freq&&"WEEKLY"!==h.freq&&"DAILY"!==h.freq)throw"Recurrence rule frequency must be provided and be one of the following: 'YEARLY', 'MONTHLY', 'WEEKLY', or 'DAILY'";if(h.until&&isNaN(Date.parse(h.until)))throw"Recurrence rule 'until' must be a valid date string";if(h.interval&&isNaN(parseInt(h.interval)))throw"Recurrence rule 'interval' must be an integer";if(h.count&&isNaN(parseInt(h.count)))throw"Recurrence rule 'count' must be an integer"}var i=new Date(f),j=new Date(g),k=("0000"+i.getFullYear().toString()).slice(-4),l=("00"+(i.getMonth()+1).toString()).slice(-2),m=("00"+i.getDate().toString()).slice(-2),n=("00"+i.getHours().toString()).slice(-2),o=("00"+i.getMinutes().toString()).slice(-2),p=("00"+i.getMinutes().toString()).slice(-2),q=("0000"+j.getFullYear().toString()).slice(-4),r=("00"+(j.getMonth()+1).toString()).slice(-2),s=("00"+j.getDate().toString()).slice(-2),t=("00"+j.getHours().toString()).slice(-2),u=("00"+j.getMinutes().toString()).slice(-2),v=("00"+j.getMinutes().toString()).slice(-2),w="",x="";o+p+u+v!==0&&(w="T"+n+o+p,x="T"+t+u+v);var y,z=k+l+m+w,A=q+r+s+x;if(h)if(h.rule)y=h.rule;else{if(y="RRULE:FREQ="+h.freq,h.until){var B=new Date(Date.parse(h.until)).toISOString();y+=";UNTIL="+B.substring(0,B.length-13).replace(/[-]/g,"")+"000000Z"}h.interval&&(y+=";INTERVAL="+h.interval),h.count&&(y+=";COUNT="+h.count)}var C=["BEGIN:VEVENT","CLASS:PUBLIC","DESCRIPTION:"+d,"DTSTART;VALUE=DATE:"+z,"DTEND;VALUE=DATE:"+A,"LOCATION:"+e,"SUMMARY;LANGUAGE=en-us:"+c,"TRANSP:TRANSPARENT","END:VEVENT"];return y&&C.splice(4,0,y),C=C.join(a),b.push(C),C},download:function(e,f){if(b.length<1)return!1;f="undefined"!=typeof f?f:".ics",e="undefined"!=typeof e?e:"calendar";var g,h=c+a+b.join(a)+d;if(-1===navigator.userAgent.indexOf("MSIE 10"))g=new Blob([h]);else{var i=new BlobBuilder;i.append(h),g=i.getBlob("text/x-vCalendar;charset="+document.characterSet)}return saveAs(g,e+f),h}}};
/*! ics.js Wed Aug 20 2014 14:44:48 */
var ics=function(){"use strict";if(navigator.userAgent.indexOf("MSIE")>-1&&-1==navigator.userAgent.indexOf("MSIE 10"))return void console.log("Unsupported Browser");var a=-1!==navigator.appVersion.indexOf("Win")?"\r\n":"\n",b=[],c=["BEGIN:VCALENDAR","VERSION:2.0"].join(a),d=a+"END:VCALENDAR",e=["SU","MO","TU","WE","TH","FR","SA"];return{events:function(){return b},calendar:function(){return c+a+b.join(a)+d},addEvent:function(c,d,f,g,h,i){if("undefined"==typeof c||"undefined"==typeof d||"undefined"==typeof f||"undefined"==typeof g||"undefined"==typeof h)return!1;if(i&&!i.rule){if("YEARLY"!==i.freq&&"MONTHLY"!==i.freq&&"WEEKLY"!==i.freq&&"DAILY"!==i.freq)throw"Recurrence rule frequency must be provided and be one of the following: 'YEARLY', 'MONTHLY', 'WEEKLY', or 'DAILY'";if(i.until&&isNaN(Date.parse(i.until)))throw"Recurrence rule 'until' must be a valid date string";if(i.interval&&isNaN(parseInt(i.interval)))throw"Recurrence rule 'interval' must be an integer";if(i.count&&isNaN(parseInt(i.count)))throw"Recurrence rule 'count' must be an integer";if("undefined"!=typeof i.byday){if("[object Array]"!==Object.prototype.toString.call(i.byday))throw"Recurrence rule 'byday' must be an array";if(i.byday.length>7)throw"Recurrence rule 'byday' array must not be longer than the 7 days in a week";i.byday=i.byday.filter(function(a,b){return i.byday.indexOf(a)==b});for(var j in i.byday)if(e.indexOf(i.byday[j])<0)throw"Recurrence rule 'byday' values must include only the following: 'SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'"}}var k=new Date(g),l=new Date(h),m=("0000"+k.getFullYear().toString()).slice(-4),n=("00"+(k.getMonth()+1).toString()).slice(-2),o=("00"+k.getDate().toString()).slice(-2),p=("00"+k.getHours().toString()).slice(-2),q=("00"+k.getMinutes().toString()).slice(-2),r=("00"+k.getMinutes().toString()).slice(-2),s=("0000"+l.getFullYear().toString()).slice(-4),t=("00"+(l.getMonth()+1).toString()).slice(-2),u=("00"+l.getDate().toString()).slice(-2),v=("00"+l.getHours().toString()).slice(-2),w=("00"+l.getMinutes().toString()).slice(-2),x=("00"+l.getMinutes().toString()).slice(-2),y="",z="";q+r+w+x!==0&&(y="T"+p+q+r,z="T"+v+w+x);var A,B=m+n+o+y,C=s+t+u+z;if(i)if(i.rule)A=i.rule;else{if(A="RRULE:FREQ="+i.freq,i.until){var D=new Date(Date.parse(i.until)).toISOString();A+=";UNTIL="+D.substring(0,D.length-13).replace(/[-]/g,"")+"000000Z"}i.interval&&(A+=";INTERVAL="+i.interval),i.count&&(A+=";COUNT="+i.count),i.byday&&i.byday.length>0&&(A+=";BYDAY="+i.byday.join(","))}var E=["BEGIN:VEVENT","CLASS:PUBLIC","DESCRIPTION:"+d,"DTSTART;VALUE=DATE:"+B,"DTEND;VALUE=DATE:"+C,"LOCATION:"+f,"SUMMARY;LANGUAGE=en-us:"+c,"TRANSP:TRANSPARENT","END:VEVENT"];return A&&E.splice(4,0,A),E=E.join(a),b.push(E),E},download:function(e,f){if(b.length<1)return!1;f="undefined"!=typeof f?f:".ics",e="undefined"!=typeof e?e:"calendar";var g,h=c+a+b.join(a)+d;if(-1===navigator.userAgent.indexOf("MSIE 10"))g=new Blob([h]);else{var i=new BlobBuilder;i.append(h),g=i.getBlob("text/x-vCalendar;charset="+document.characterSet)}return saveAs(g,e+f),h}}};
+10
View File
@@ -52,6 +52,16 @@
cal.addEvent('Soccer Practice', 'Practice kicking the ball in the net! YAYY!!', 'Soccer field', '08/18/2014', '09/18/2014', {freq: 'WEEKLY', interval: 2, count: 10});
assert.strictEqual(cal.events()[4], 'BEGIN:VEVENT\nCLASS:PUBLIC\nDESCRIPTION:Practice kicking the ball in the net! YAYY!!\nDTSTART;VALUE=DATE:20140818T000000\nRRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=10\nDTEND;VALUE=DATE:20140918T000000\nLOCATION:Soccer field\nSUMMARY;LANGUAGE=en-us:Soccer Practice\nTRANSP:TRANSPARENT\nEND:VEVENT');
});
it('should add recurring events using interval and until and byday', function () {
cal.addEvent('Soccer Practice', 'Practice kicking the ball in the net! YAYY!!', 'Soccer field', '08/18/2014', '09/18/2014', {freq: 'WEEKLY', interval: 1, until: '08/18/2014', byday: ['MO','WE','FR']});
assert.strictEqual(cal.events()[5], 'BEGIN:VEVENT\nCLASS:PUBLIC\nDESCRIPTION:Practice kicking the ball in the net! YAYY!!\nDTSTART;VALUE=DATE:20140818T000000\nRRULE:FREQ=WEEKLY;UNTIL=20140818T000000Z;INTERVAL=1;BYDAY=MO,WE,FR\nDTEND;VALUE=DATE:20140918T000000\nLOCATION:Soccer field\nSUMMARY;LANGUAGE=en-us:Soccer Practice\nTRANSP:TRANSPARENT\nEND:VEVENT');
});
it('should add recurring events using interval and until and ignore duplicates in byday array', function () {
cal.addEvent('Soccer Practice', 'Practice kicking the ball in the net! YAYY!!', 'Soccer field', '08/18/2014', '09/18/2014', {freq: 'WEEKLY', interval: 1, until: '08/18/2014', byday: ['MO', 'WE', 'MO']});
assert.strictEqual(cal.events()[6], 'BEGIN:VEVENT\nCLASS:PUBLIC\nDESCRIPTION:Practice kicking the ball in the net! YAYY!!\nDTSTART;VALUE=DATE:20140818T000000\nRRULE:FREQ=WEEKLY;UNTIL=20140818T000000Z;INTERVAL=1;BYDAY=MO,WE\nDTEND;VALUE=DATE:20140918T000000\nLOCATION:Soccer field\nSUMMARY;LANGUAGE=en-us:Soccer Practice\nTRANSP:TRANSPARENT\nEND:VEVENT');
});
});
});
})();