@rareelements/lydia
Version:
RFC5545 implementation
98 lines • 3.57 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CalendarParser = void 0;
const fs = __importStar(require("fs"));
const readline = __importStar(require("readline"));
const Helper_1 = require("./utils/Helper");
const PropertyType_1 = require("./model/PropertyType");
class CalendarParser {
static parse(lines) {
let currentComponent;
const components = [];
const properties = [];
const calendar = {
components,
properties
};
for (const line of lines) {
if (Helper_1.SpecialStrings.CALENDAR_START === line || Helper_1.SpecialStrings.CALENDAR_END === line) {
continue;
}
if (line.startsWith(Helper_1.SpecialStrings.BEGIN)) {
const name = line.substr(Helper_1.SpecialStrings.BEGIN.length + 1);
currentComponent = {
name,
properties: []
};
components.push(currentComponent);
}
else if (line.startsWith(Helper_1.SpecialStrings.END) && line !== PropertyType_1.PropertyType.X_WR_TIMEZONE) {
currentComponent = undefined;
}
else {
const property = Helper_1.Helper.parseLine(line);
if (currentComponent)
currentComponent.properties.push(property);
else
properties.push(property);
}
}
return calendar;
}
static async parseContent(data) {
const temp = data.split(/\r?\n/);
const lines = [];
for (const line of temp) {
unwrap(lines)(line);
}
const calendar = CalendarParser.parse(lines);
return calendar;
}
static async parseFile(filename) {
return new Promise((resolve, reject) => {
try {
const rl = readline.createInterface({ input: fs.createReadStream(filename) });
const lines = [];
rl.on('line', unwrap(lines));
rl.on('close', () => {
const calendar = CalendarParser.parse(lines);
resolve(calendar);
});
}
catch (err) {
reject(err);
}
});
}
}
exports.CalendarParser = CalendarParser;
function unwrap(lines) {
return (line) => {
if (line.startsWith(' ') || line.startsWith('\t')) {
line = line.substr(1);
line = lines.pop() + line;
}
if (line) // ignore empty lines
lines.push(line);
};
}
//# sourceMappingURL=CalendarParser.js.map