Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support exdates and multiple events with the same UID #44

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.idea
139 changes: 89 additions & 50 deletions ical.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var UUID = require('node-uuid');

(function(name, definition) {

/****************
Expand Down Expand Up @@ -51,71 +53,110 @@

return curr
}
}
};

var addTZ = function(dt, name, params){
var addTZ = function(dateObj, params){
var p = parseParams(params);

if (params && p){
dt[name].tz = p.TZID
if (params && p && dateObj){
dateObj.tz = p.TZID
}

return dt
}


var dateParam = function(name){
return function(val, params, curr){
return dateObj;
};

// Store as string - worst case scenario
storeParam(name)(val, undefined, curr)
/**
* Convert a date string from ICS format into a native Date object
* @param {string} val - The ICS string to be parsed
* @param {array} params
* @param {object} curr - The current Object that we're building
* @return {object} The Javascript date object
*/
function parseDate(val, params, curr) {
var objToReturn = val;

if (params && params[0] === "VALUE=DATE") {
// Just Date
if (params && params[0] === "VALUE=DATE") {
// Just Date

var comps = /^(\d{4})(\d{2})(\d{2})$/.exec(val);
if (comps !== null) {
// No TZ info - assume same timezone as this computer
curr[name] = new Date(
comps[1],
var comps = /^(\d{4})(\d{2})(\d{2})$/.exec(val);
if (comps !== null) {
// No TZ info - assume same timezone as this computer
objToReturn = new Date(
comps[1],
parseInt(comps[2], 10)-1,
comps[3]
);
comps[3]
);

return addTZ(curr, name, params);
}
return addTZ(objToReturn, params);
}
}


//typical RFC date-time format
var comps = /^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(Z)?$/.exec(val);
if (comps !== null) {
if (comps[7] == 'Z'){ // GMT
curr[name] = new Date(Date.UTC(
parseInt(comps[1], 10),
//typical RFC date-time format
var comps = /^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(Z)?$/.exec(val);
if (comps !== null) {
if (comps[7] == 'Z'){ // GMT
return new Date(Date.UTC(
parseInt(comps[1], 10),
parseInt(comps[2], 10)-1,
parseInt(comps[3], 10),
parseInt(comps[4], 10),
parseInt(comps[5], 10),
parseInt(comps[6], 10 )
));
// TODO add tz
} else {
curr[name] = new Date(
parseInt(comps[1], 10),
parseInt(comps[3], 10),
parseInt(comps[4], 10),
parseInt(comps[5], 10),
parseInt(comps[6], 10 )
));
// TODO add tz
} else {
return new Date(
parseInt(comps[1], 10),
parseInt(comps[2], 10)-1,
parseInt(comps[3], 10),
parseInt(comps[4], 10),
parseInt(comps[5], 10),
parseInt(comps[6], 10)
);
}
parseInt(comps[3], 10),
parseInt(comps[4], 10),
parseInt(comps[5], 10),
parseInt(comps[6], 10)
);
}
}

return addTZ(curr, name, params)
// date format (no time)
comps = /^(\d{4})(\d{2})(\d{2})$/.exec(val);
if (comps !== null) {
// No TZ info - assume same timezone as this computer
objToReturn = new Date(
comps[1],
parseInt(comps[2], 10)-1,
comps[3]
);

return addTZ(objToReturn, params);
}
}

var dateParam = function(name){
return function(val, params, curr){
var dateObj = parseDate(val, params, curr);
dateObj = addTZ(dateObj, params);
if (dateObj) {
curr[name] = dateObj;
} else {
// Store as string - worst case scenario
storeParam(name)(val, undefined, curr);
}
return curr;
}
};

var dateParamArray = function(name) {
return function(date, params, curr) {
// initialize
curr[name] = curr[name] || [];
// load date
var dateObj = parseDate(date, params, curr);
dateObj = addTZ(dateObj, params);
curr[name].push(dateObj);
return curr;
}

};

var geoParam = function(name){
return function(val, params, curr){
Expand Down Expand Up @@ -193,11 +234,7 @@
}

var par = stack.pop()

if (curr.uid)
par[curr.uid] = curr
else
par[Math.random()*100000] = curr // Randomly assign ID : TODO - use true GUID
par[UUID.v4()] = curr;

return par
}
Expand All @@ -216,6 +253,8 @@
, 'COMPLETED': dateParam('completed')
, 'CATEGORIES': categoriesParam('categories')
, 'FREEBUSY': freebusyParam('freebusy')
, 'EXDATE': dateParamArray('exdate')
, 'RECURRENCE-ID': storeParam('recurrenceId')
},


Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
"url": "git://github.com/peterbraden/ical.js.git"
},
"dependencies": {
"request": "2.40.0",
"rrule": "2.0.0"
"node-uuid": "^1.4.1",
"rrule": "2.1.0",
"request": "2.40.0"
},
"devDependencies": {
"vows": "0.7.0",
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ vows.describe('node-ical').addBatch({
}
, 'todo item [email protected]' : {
topic : function(items){
return items['[email protected]']
return _.filter(items,function(obj) { { return obj.uid == '[email protected]'; } })[0];
}
, 'is a VTODO' : function(topic){
assert.equal(topic.type, 'VTODO')
Expand Down