diff --git a/lib/ical/timezone_service.js b/lib/ical/timezone_service.js index 06df6da7..06370013 100644 --- a/lib/ical/timezone_service.js +++ b/lib/ical/timezone_service.js @@ -69,24 +69,39 @@ const TimezoneService = { /** * Registers a timezone object or component. * + * @param {Component|Timezone} timezone + * The initialized zone or vtimezone. + * * @param {String=} name * The name of the timezone. Defaults to the component's TZID if not * passed. - * @param {Component|Timezone} timezone - * The initialized zone or vtimezone. */ - register: function(name, timezone) { + register: function(timezone, name) { if (zones === null) { this.reset(); } - if (name instanceof Component) { - if (name.name === 'vtimezone') { - timezone = new Timezone(name); + // This avoids a breaking change by the change of argument order + // TODO remove in v3 + if (typeof timezone === "string" && name instanceof Timezone) { + [timezone, name] = [name, timezone]; + } + + if (!name) { + if (timezone instanceof Timezone) { name = timezone.tzid; + } else { + if (timezone.name === 'vtimezone') { + timezone = new Timezone(timezone); + name = timezone.tzid; + } } } + if (!timezone.tzid && !name) { + throw new TypeError("neither a timezone nor a name was passed"); + } + if (timezone instanceof Timezone) { zones[name] = timezone; } else { diff --git a/test/timezone_service_test.js b/test/timezone_service_test.js index 8b2adc2c..23625f8a 100644 --- a/test/timezone_service_test.js +++ b/test/timezone_service_test.js @@ -79,7 +79,7 @@ suite('timezone_service', function() { assert.throws(function() { let comp = new ICAL.Component('vtoaster'); subject.register(comp); - }, "timezone must be ICAL.Timezone"); + }, "neither a timezone nor a name was passed"); }); test('override', function() {