@rareelements/lydia
Version:
RFC5545 implementation
120 lines • 4.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CalendarUtils = void 0;
const SimpleTypes_1 = require("./model/SimpleTypes");
const PropertyType_1 = require("./model/PropertyType");
const CustomErrors_1 = require("./CustomErrors");
const DateUtils_1 = require("./utils/DateUtils");
const Component_1 = require("./model/Component");
class CalendarUtils {
static enrichComponent(rawComponent, defaultTZ) {
return new Component_1.Component(rawComponent, defaultTZ);
}
static enrichProperty(rawProperty, rawComponent, defaultTZ) {
const name = rawProperty.name;
if (name.startsWith(PropertyType_1.PropertyType.EXPERIMENTAL_PREFIX))
return handleExperimental(rawProperty);
switch (name) {
case PropertyType_1.PropertyType.STATUS:
return handleStatus(rawProperty);
case PropertyType_1.PropertyType.UID:
case PropertyType_1.PropertyType.DESCRIPTION:
case PropertyType_1.PropertyType.LOCATION:
case PropertyType_1.PropertyType.SUMMARY:
return handleText(rawProperty);
case PropertyType_1.PropertyType.TRANSP:
return handleTransparency(rawProperty);
case PropertyType_1.PropertyType.SEQUENCE:
return handleSequence(rawProperty);
case PropertyType_1.PropertyType.DTSTART:
case PropertyType_1.PropertyType.DTEND:
return handleDate(rawProperty, defaultTZ);
case PropertyType_1.PropertyType.DTSTAMP:
case PropertyType_1.PropertyType.CREATED:
case PropertyType_1.PropertyType.LAST_MODIFIED:
return handleTimestamp(rawProperty, defaultTZ);
case PropertyType_1.PropertyType.RRULE:
return handleRRule(rawProperty, rawComponent);
}
const unknown = {
type: PropertyType_1.PropertyType.UNSUPPORTED,
rawProperty,
value: rawProperty.value
};
return unknown;
}
}
exports.CalendarUtils = CalendarUtils;
function handleDate(rawProperty, defaultTZ) {
const property = DateUtils_1.DateUtils.enrichDTProperty(rawProperty, defaultTZ);
return property;
}
function handleTimestamp(rawProperty, defaultTZ) {
const property = DateUtils_1.DateUtils.entrichTSProperty(rawProperty, defaultTZ);
return property;
}
function handleStatus(rawProperty) {
const value = rawProperty.value;
if (!value || !Object.values(SimpleTypes_1.Status).includes(value))
throw new CustomErrors_1.ValidationError('Invalid Status: ' + value);
const rich = {
value: value,
type: PropertyType_1.PropertyType.STATUS
};
return rich;
}
function handleRRule(rawProperty, rawComponent) {
const rruleSet = DateUtils_1.DateUtils.createRuleSet(rawComponent);
if (!rruleSet) {
throw new CustomErrors_1.AssertionError('Empty rule set returned for non empty element');
}
const name = rawProperty.name;
const value = rawProperty.value;
const rich = {
name,
value,
type: PropertyType_1.PropertyType.RRULE,
rruleSet
};
return rich;
}
function handleText(rawProperty) {
const name = rawProperty.name;
const value = rawProperty.value;
const rich = {
value,
type: name
};
return rich;
}
function handleTransparency(rawProperty) {
const value = rawProperty.value;
if (!value || !Object.values(SimpleTypes_1.Transparency).includes(value))
throw new CustomErrors_1.ValidationError('Invalid Transparency: ' + value);
const rich = {
value: value,
type: PropertyType_1.PropertyType.TRANSP
};
return rich;
}
function handleSequence(rawProperty) {
const value = rawProperty.value;
if (!value)
throw new CustomErrors_1.ValidationError('Missing value for Sequence');
const parsed = parseInt(value, 10);
if (isNaN(parsed))
throw new CustomErrors_1.ValidationError('Invalid Sequence: ' + parsed);
const rich = {
value: parsed,
type: PropertyType_1.PropertyType.SEQUENCE
};
return rich;
}
function handleExperimental(rawProperty) {
return {
type: PropertyType_1.PropertyType.EXPERIMENTAL_PREFIX,
name: rawProperty.name,
rawProperty
};
}
//# sourceMappingURL=CalendarUtils.js.map