@rareelements/lydia
Version:
RFC5545 implementation
90 lines • 3.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.cookComponent = exports.extractEntityName = exports.SpecialStrings = exports.Helper = exports.E_DELIMITER = exports.V_DELIMITER = exports.P_DELIMITER = void 0;
const CustomErrors_1 = require("../CustomErrors");
const CalendarUtils_1 = require("../CalendarUtils");
exports.P_DELIMITER = ';';
exports.V_DELIMITER = ':';
exports.E_DELIMITER = '=';
class Helper {
static parseLine(line) {
const params = [];
let nameEndIndex = 0;
let valueStartIndex = 0;
let paramNameStart = 0;
let paramNameEnd = 0;
let currentIndex = 0;
for (const c of line) {
if ((c === exports.V_DELIMITER || c === exports.P_DELIMITER) && nameEndIndex === 0) {
nameEndIndex = currentIndex;
}
if ((c === exports.P_DELIMITER || c === exports.V_DELIMITER) && paramNameStart > 0) {
params.push({
name: line.substring(paramNameStart, paramNameEnd).toUpperCase(),
value: line.substring(paramNameEnd + 1, currentIndex)
});
paramNameStart = 0;
}
// tslint:disable-next-line:prefer-switch
if (c === exports.P_DELIMITER) {
paramNameStart = currentIndex + 1;
}
else if (c === exports.E_DELIMITER) {
paramNameEnd = currentIndex;
}
else if (c === exports.V_DELIMITER) {
valueStartIndex = currentIndex + 1;
break;
}
currentIndex++;
}
const name = line.substr(0, nameEndIndex).toUpperCase();
const value = line.substring(valueStartIndex);
return {
name,
value,
params: params.length > 0 ? params : undefined
};
}
static unescapeText(value) {
return value
.replace(/\\[nN]/g, '\n')
.replace(/\\\\/g, '\\')
.replace(/\\\,/g, ',')
.replace(/\\\;/g, ';');
}
}
exports.Helper = Helper;
var SpecialStrings;
(function (SpecialStrings) {
SpecialStrings["VCALENDAR"] = "VCALENDAR";
SpecialStrings["BEGIN"] = "BEGIN";
SpecialStrings["END"] = "END";
SpecialStrings["VERSION"] = "VERSION";
SpecialStrings["PROVIDER"] = "PROVIDER";
SpecialStrings["CALENDAR_START"] = "BEGIN:VCALENDAR";
SpecialStrings["CALENDAR_END"] = "END:VCALENDAR";
})(SpecialStrings = exports.SpecialStrings || (exports.SpecialStrings = {}));
function extractEntityName(line) {
if (!line)
throw new CustomErrors_1.ValidationError('Empty line. Entity name cannot be extracted ');
let index = 0;
for (const c of line) {
if (c === ':' || c === ';') {
break;
}
index++;
}
if (line.length === index)
throw new CustomErrors_1.ValidationError('Missing delimiter. Cannot extract entity name ' + line);
const name = line.substr(0, index).toUpperCase();
if (name.trim() !== name)
throw new CustomErrors_1.ValidationError('Entity name must not contain spaces: ' + name);
return name;
}
exports.extractEntityName = extractEntityName;
function cookComponent(rawComponent, defaultTZ) {
return CalendarUtils_1.CalendarUtils.enrichComponent(rawComponent, defaultTZ);
}
exports.cookComponent = cookComponent;
//# sourceMappingURL=Helper.js.map