Commit 6280432e authored by Sebastian's avatar Sebastian
Browse files

Merge branch 'develop'

parents 14261f20 b76ed63e
{
"maxerr": 50,
"smarttabs": true,
"nonbsp": false,
"quotmark": "single",
"node": true,
"mocha": true
"maxerr": 50,
"quotmark": "single",
"node": true,
"mocha": true
}
\ No newline at end of file
language: node_js
node_js:
- "4.1"
- "4.0"
- "0.12"
- "0.11"
- "0.10"
......
......@@ -160,18 +160,36 @@ cal.prodId('//My Company//My Product//EN');
#### name([_String_ name])
Use this method to set your feed's name. Is used to fill `X-WR-CALNAME` in your iCal file.
Use this method to set your feed's name. Is used to fill `NAME` and `X-WR-CALNAME` in your iCal file.
#### url([_String_ url])
Use this method to set your feed's URL.
```javascript
var cal = ical().url('https://example.com/calendar.ical');
```
#### timezone([_String_ timezone])
Use this method to set your feed's timezone. Is used to fill `X-WR-TIMEZONE` in your iCal.
Use this method to set your feed's timezone. Is used to fill `TIMEZONE-ID` and `X-WR-TIMEZONE` in your iCal.
```javascript
var cal = ical().timezone('Europe/Berlin');
```
#### ttl([_Number_ ttl])
Use this method to set your feed's time to live. Is used to fill `REFRESH-INTERVAL` and `X-PUBLISHED-TTL` in your iCal.
```javascript
var cal = ical().ttl(60 * 60 * 24);
```
#### createEvent([_Object_ options])
Creates a new [Event](#event) ([`ICalEvent`](#event)) and returns it. Use options to prefill the event's attributes.
......@@ -233,6 +251,9 @@ Return a shallow copy of the calendar's options for JSON stringification. Can be
```javascript
var cal = ical(),
json = JSON.stringify(cal);
// later
cal = ical(json);
```
#### length()
......@@ -260,7 +281,7 @@ Appointment date of beginning as Date object. This is required for all events!
#### end([_Date_ end])
Appointment date of end as Date object. This is also required for all events!
Appointment date of end as Date object.
#### timestamp([_Date_ stamp]) or stamp([_Date_ stamp])
......
'use strict';
var ical = require('../lib/'),
http = require('http'),
cal = ical({
domain: 'sebbo.net',
prodId: '//superman-industries.com//ical-generator//EN',
events: [
{
start: new Date(),
end: new Date(new Date().getTime() + 3600000),
summary: 'Example Event',
description: 'It works ;)',
url: 'http://sebbo.net/'
}
]
});
http = require('http'),
cal = ical({
domain: 'sebbo.net',
prodId: '//superman-industries.com//ical-generator//EN',
events: [
{
start: new Date(),
end: new Date(new Date().getTime() + 3600000),
summary: 'Example Event',
description: 'It works ;)',
url: 'http://sebbo.net/'
}
]
});
http.createServer(function(req, res) {
cal.serve(res);
cal.serve(res);
}).listen(3000, '127.0.0.1', function() {
console.log('Server running at http://127.0.0.1:3000/');
console.log('Server running at http://127.0.0.1:3000/');
});
\ No newline at end of file
var ical = require('../lib/'),
cal = ical({domain: 'localhost'}),
event;
cal = ical({domain: 'localhost'}),
event;
// overwrite domain
cal.domain('example.com');
event = cal.createEvent({
start: new Date(new Date().getTime() + 3600000),
end: new Date(new Date().getTime() + 7200000),
summary: 'Example Event',
description: 'It works ;)',
organizer: 'Organizer\'s Name <organizer@example.com>',
url: 'http://sebbo.net/'
start: new Date(new Date().getTime() + 3600000),
end: new Date(new Date().getTime() + 7200000),
summary: 'Example Event',
description: 'It works ;)',
organizer: 'Organizer\'s Name <organizer@example.com>',
url: 'http://sebbo.net/'
});
// update event's description
......
module.exports = function(grunt) {
'use strict';
'use strict';
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.initConfig({
clean: {
coverage: ['coverage/**']
},
jshint: {
options: {
jshintrc: true
},
lib: [
'lib/*.js',
'test/*.js'
]
},
watch: {
lib: {
files: ['lib/*.js', 'test/*.js'],
tasks: ['jshint:lib']
}
}
});
grunt.initConfig({
jshint: {
options: {
jshintrc: true
},
lib: [
'lib/*.js',
'test/*.js'
]
},
watch: {
lib: {
files: ['lib/*.js', 'test/*.js'],
tasks: ['jshint:lib']
}
}
});
grunt.registerTask('default', ['jshint:lib']);
grunt.registerTask('dev', ['jshint:lib', 'watch']);
grunt.registerTask('default', ['jshint:lib']);
grunt.registerTask('dev', ['jshint:lib', 'watch']);
};
\ No newline at end of file
......@@ -3,104 +3,110 @@
module.exports = {};
module.exports.formatDate = function formatDate(d, dateonly, floating) {
var s;
var s;
function pad(i) {
return (i < 10 ? '0' : '') + i;
}
function pad(i) {
return (i < 10 ? '0' : '') + i;
}
s = d.getUTCFullYear();
s += pad(d.getUTCMonth() + 1);
s += pad(d.getUTCDate());
s = d.getUTCFullYear();
s += pad(d.getUTCMonth() + 1);
s += pad(d.getUTCDate());
if(!dateonly) {
s += 'T';
s += pad(d.getUTCHours());
s += pad(d.getUTCMinutes());
s += pad(d.getUTCSeconds());
if(!dateonly) {
s += 'T';
s += pad(d.getUTCHours());
s += pad(d.getUTCMinutes());
s += pad(d.getUTCSeconds());
if(!floating) {
s += 'Z';
}
}
if(!floating) {
s += 'Z';
}
}
return s;
return s;
};
module.exports.escape = function escape(str) {
return str.replace(/[\\;,\n"]/g, function(match) {
if(match === '\n') {
return '\\n';
}
return '\\' + match;
});
return str.replace(/[\\;,"]/g, function(match) {
return '\\' + match;
}).replace(/(?:\r\n|\r|\n)/g, '\\n');
};
module.exports.duration = function duration(seconds) {
var string = '';
// < 0
if(seconds < 0) {
string = '-';
seconds *= -1;
}
string += 'PT';
// HOURS
if(seconds >= 3600) {
string += Math.floor(seconds / 3600) + 'H';
seconds %= 3600;
}
// MINUTES
if(seconds >= 60) {
string += Math.floor(seconds / 60) + 'M';
seconds %= 60;
}
// SECONDS
if(seconds > 0) {
string += seconds + 'S';
}
else if(string.length <= 2) {
string += '0S';
}
return string;
var string = '';
// < 0
if(seconds < 0) {
string = '-';
seconds *= -1;
}
string += 'P';
// DAYS
if(seconds >= 86400) {
string += Math.floor(seconds / 86400) + 'D';
seconds %= 86400;
}
if(!seconds && string.length > 1) {
return string;
}
string += 'T';
// HOURS
if(seconds >= 3600) {
string += Math.floor(seconds / 3600) + 'H';
seconds %= 3600;
}
// MINUTES
if(seconds >= 60) {
string += Math.floor(seconds / 60) + 'M';
seconds %= 60;
}
// SECONDS
if(seconds > 0) {
string += seconds + 'S';
}
else if(string.length <= 2) {
string += '0S';
}
return string;
};
module.exports.toJSON = function(object, attributes, options) {
var result = {};
options = options || {};
options.ignoreAttributes = options.ignoreAttributes || [];
options.hooks = options.hooks || {};
attributes.forEach(function(attribute) {
if(options.ignoreAttributes.indexOf(attribute) !== -1) {
return;
}
var value = object[attribute]();
if(options.hooks[attribute]) {
value = options.hooks[attribute](value);
}
if(!value) {
return;
}
result[attribute] = value;
if(Array.isArray(result[attribute])) {
console.log('ARRAY!');
var newObj = [];
result[attribute].forEach(function(object) {
newObj.push(object.toJSON());
});
result[attribute] = newObj;
}
});
return result;
var result = {};
options = options || {};
options.ignoreAttributes = options.ignoreAttributes || [];
options.hooks = options.hooks || {};
attributes.forEach(function(attribute) {
if(options.ignoreAttributes.indexOf(attribute) !== -1) {
return;
}
var value = object[attribute]();
if(options.hooks[attribute]) {
value = options.hooks[attribute](value);
}
if(!value) {
return;
}
result[attribute] = value;
if(Array.isArray(result[attribute])) {
var newObj = [];
result[attribute].forEach(function(object) {
newObj.push(object.toJSON());
});
result[attribute] = newObj;
}
});
return result;
};
\ No newline at end of file
......@@ -7,291 +7,291 @@
* @constructor ICalAlarm Alarm
*/
var ICalAlarm = function(_data, event) {
var attributes = ['type', 'trigger', 'triggerBefore', 'triggerAfter', 'repeat', 'interval', 'attach', 'description'],
vars,
i,
data;
if(!event) {
throw '`event` option required!';
}
vars = {
types: ['display', 'audio']
};
data = {
type: null,
trigger: null,
repeat: null,
repeatInterval: null,
attach: null,
description: null
};
/**
* Set/Get the alarm type
*
* @param type Type
* @since 0.2.1
* @returns {ICalAlarm|String}
*/
this.type = function(type) {
if(!type) {
return data.type;
}
if(vars.types.indexOf(type) === -1) {
throw '`type` is not correct, must be either `display` or `audio`!';
}
data.type = type;
return this;
};
/**
* Set/Get seconds before event to trigger alarm
*
* @param {Number|Date} trigger Seconds before alarm triggeres
* @since 0.2.1
* @returns {ICalAlarm|Number|Date}
*/
this.trigger = function(trigger) {
if(!trigger && data.trigger instanceof Date) {
return data.trigger;
}
if(!trigger) {
return -1 * data.trigger;
}
if(trigger instanceof Date) {
data.trigger = trigger;
return this;
}
if(typeof trigger === 'number' && isFinite(trigger)) {
data.trigger = -1 * trigger;
return this;
}
throw '`trigger` is not correct, must be either typeof `Number` or `Date`!';
};
/**
* Set/Get seconds after event to trigger alarm
*
* @param {Number|Date} trigger Seconds after alarm triggeres
* @since 0.2.1
* @returns {ICalAlarm|Number|Date}
*/
this.triggerAfter = function(trigger) {
if(!trigger) {
return data.trigger;
}
return this.trigger(typeof trigger === 'number' ? -1 * trigger : trigger);
};
/**
* Set/Get seconds before event to trigger alarm
*
* @param {Number|Date} trigger Seconds before alarm triggeres
* @since 0.2.1
* @alias trigger
* @returns {ICalAlarm|Number|Date}
*/
this.triggerBefore = this.trigger;
/**
* Set/Get Alarm Repetitions
*
* @param {Number} Number of repetitions
* @since 0.2.1
* @returns {ICalAlarm|Number}
*/
this.repeat = function(repeat) {
if(!repeat) {
return data.repeat;
}
if(typeof repeat !== 'number' || !isFinite(repeat)) {
throw '`repeat` is not correct, must be numeric!';
}
data.repeat = repeat;
return this;
};
/**
* Set/Get Repeat Interval
*
* @param {Number} Interval in seconds
* @since 0.2.1
* @returns {ICalAlarm|Number|Null}
*/
this.interval = function(interval) {
if(!interval) {
return data.interval;
}
if(typeof interval !== 'number' || !isFinite(interval)) {
throw '`interval` is not correct, must be numeric!';
}
data.interval = interval;
return this;
};
/**
* Set/Get Attachment
*
* @param {Object|String} File-URI or Object
* @since 0.2.1
* @returns {ICalAlarm|Object}
*/
this.attach = function(_attach) {
if(!_attach) {
return data.attach;
}
var attach = null;
if(typeof _attach === 'string') {
attach = {
uri: _attach,
mime: null
};
}
else if(typeof _attach === 'object') {
attach = {
uri: _attach.uri,
mime: _attach.mime || null
};
}
else {
throw '`attach` needs to be a valid formed string or an object. See https://github.com/sebbo2002/ical-generator#attachstringobject-attach';
}
if(!attach.uri) {
throw '`attach.uri` is empty!';
}
data.attach = {
uri: attach.uri,
mime: attach.mime
};
return this;
};
/**
* Set/Get the alarm description
*
* @param description Description
* @since 0.2.1
* @returns {ICalAlarm|String}
*/
this.description = function(description) {
if(!description) {
return data.description;
}
data.description = description;
return this;
};
/**
* Export calender as JSON Object to use it later…
*
* @since 0.2.4
* @returns Object Calendar
*/
this.toJSON = function() {
var tools = require('./_tools.js');
return tools.toJSON(this, attributes);
};
/**
* Export Event to iCal
*
* @since 0.2.0
* @returns {String}
*/
this.generate = function() {
var tools = require('./_tools.js'),