Commit 40466215 authored by Sebastian Pekarek's avatar Sebastian Pekarek
Browse files

ICalAlarm.repeat()

parent 19c1193f
......@@ -61,7 +61,7 @@ module.exports.duration = function duration(seconds) {
}
// SECONDS
if(string > 0) {
if(seconds > 0) {
string += seconds + 'S';
}
else if(!string) {
......
......@@ -28,6 +28,7 @@ var ICalAlarm = function(_data, event) {
description: null
};
/**
* Set/Get the alarm type
*
......@@ -76,6 +77,7 @@ var ICalAlarm = function(_data, event) {
throw '`trigger` is not correct, must be either typeof `Number` or `Date`!';
};
/**
* Set/Get seconds after event to trigger alarm
*
......@@ -101,8 +103,46 @@ var ICalAlarm = function(_data, event) {
*/
this.triggerBefore = this.trigger;
this.repeat = function() {
/**
* 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;
};
......@@ -118,7 +158,7 @@ var ICalAlarm = function(_data, event) {
/*
REPEAT:4
DURATION:PT15M
ATTACH;FMTTYPE=audio/basic:ftp://host.com/pub/sounds/bell-01.aud
......@@ -144,13 +184,29 @@ var ICalAlarm = function(_data, event) {
g += 'TRIGGER:' + tools.duration(data.trigger) + '\n';
}
// REPEAT
if(data.repeat && !data.interval) {
throw 'No value for `interval` in ICalAlarm given, but required for `repeat`!';
}
if(data.repeat) {
g += 'REPEAT:' + data.repeat + '\n';
}
// INTERVAL
if(data.interval && !data.repeat) {
throw 'No value for `repeat` in ICalAlarm given, but required for `interval`!';
}
if(data.interval) {
g += 'DURATION:' + tools.duration(data.interval) + '\n';
}
g += 'END:VALARM\n';
return g;
};
for(i in _data) {
if(_data.hasOwnProperty(i) && ['type', 'trigger', 'triggerBefore', 'triggerAfter', 'repeat'].indexOf(i) > -1) {
if(_data.hasOwnProperty(i) && ['type', 'trigger', 'triggerBefore', 'triggerAfter', 'repeat', 'interval'].indexOf(i) > -1) {
this[i](_data[i]);
}
}
......
......@@ -1441,5 +1441,79 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
assert.ok(cal.toString().indexOf('TRIGGER;VALUE=DATE-TIME:20150201T133845Z') > -1);
});
});
describe('repeat()', function() {
it('setter should return this', function() {
var a = ical().createEvent().createAlarm();
assert.deepEqual(a, a.repeat(4));
});
it('getter should return value', function() {
var e = ical().createEvent().createAlarm().repeat(100);
assert.equal(e.repeat(), 100);
});
it('should throw error if repeat not allowed', function() {
var a = ical().createEvent().createAlarm();
assert.throws(function() {
a.repeat(Infinity);
}, /`repeat`/);
assert.throws(function() {
a.repeat('hi');
}, /`repeat`/);
assert.throws(function() {
a.repeat(true);
}, /`repeat`/);
});
it('should change something', function() {
var cal = ical(),
event = cal.createEvent({
start: new Date(),
end: new Date(new Date().getTime() + 3600000),
summary: 'Example Event'
});
event.createAlarm({type: 'display', trigger: 300, repeat: 42, interval: 60});
assert.ok(cal.toString().indexOf('REPEAT:42') > -1);
});
});
describe('interval()', function() {
it('setter should return this', function() {
var a = ical().createEvent().createAlarm();
assert.deepEqual(a, a.interval(60));
});
it('getter should return value', function() {
var e = ical().createEvent().createAlarm().interval(30);
assert.equal(e.interval(), 30);
});
it('should throw error if repeat not allowed', function() {
var a = ical().createEvent().createAlarm();
assert.throws(function() {
a.interval(Infinity);
}, /`interval`/);
assert.throws(function() {
a.interval('hi');
}, /`interval`/);
assert.throws(function() {
a.interval(true);
}, /`interval`/);
});
it('should change something', function() {
var cal = ical(),
event = cal.createEvent({
start: new Date(),
end: new Date(new Date().getTime() + 3600000),
summary: 'Example Event'
});
event.createAlarm({type: 'display', trigger: 300, repeat: 42, interval: 90});
assert.ok(cal.toString().indexOf('DURATION:PT1M30S') > -1);
});
});
});
});
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment