import { Property } from "./property";
import { mapToJSON, mapToString, splitSafeLines, trim, removePattern, isEmptyString } from "./util";
Awesome ICS v0.1.0
http://bandraszyk.github.io/awesome-ico/
(c) 2015 Bandro
Awesome ICS may be freely distributed under the MIT license.
import { Property } from "./property";
import { mapToJSON, mapToString, splitSafeLines, trim, removePattern, isEmptyString } from "./util";
Basic element for building iCalendar objects. It contains type, child blocks and properties.
export class Block {
Initializes the instance with default values
constructor() {
this.clear();
}
Clears the Block
by setting default values
clear() {
this.properties = [];
this.blocks = [];
this.type = null;
return this;
}
Converts Block
to string
toString() {
let properties = "";
let blocks = "";
if (this.properties.length) { properties = `${this.properties.map(mapToString).join(Block.__format.newLine)}${Block.__format.newLine}`; }
if (this.blocks.length) { blocks = `${this.blocks.map(mapToString).join(Block.__format.newLine)}${Block.__format.newLine}`; }
return `${Block.__format.blockBegin}${this.type}${Block.__format.newLine}${properties}${blocks}${Block.__format.blockEnd}${this.type}`;
}
Converts Block
to JSON object
toJSON() {
return {
type : this.type,
properties : this.properties.map(mapToJSON),
blocks : this.blocks.map(mapToJSON)
}
}
Converts Block
from string, e.g.: ‘BEGIN:BlockType\nEND:BlockType’
convertFromString(string) {
string = Block.__format.prepareString(string);
if (isEmptyString(string)) { return this.clear(); }
let lines = splitSafeLines(string, Block.__format);
let blockBegin = trim(lines.shift() || "");
let blockEnd = trim(lines.pop() || "");
if (!Block.__format.regexBlockBegin.test(blockBegin)) {
throw new Error("[Block] [convertFromString()] Cannot load Block element, first line should match /^BEGIN:/i.");
}
if (!Block.__format.regexBlockEnd.test(blockEnd)) {
throw new Error("[Block] [convertFromString()] Cannot load Block elements, last line should match /^END:/i.");
}
if (removePattern(blockBegin, Block.__format.regexBlockBegin) !== removePattern(blockEnd, Block.__format.regexBlockEnd)) {
throw new Error("[Block] [convertFromString()] Cannot load Block elements, block doesn't have the end.");
}
this.type = removePattern(blockBegin, Block.__format.regexBlockBegin);
let block = [];
let blockCounter = 0;
lines.forEach(line => {
if (Block.__format.regexBlockBegin.test(line)) { blockCounter++; }
if (Block.__format.regexBlockEnd.test(line)) { blockCounter--; }
if (blockCounter === 0 && block.length > 0) {
block.push(line);
this.blocks.push(new Block().convertFromString(block.join(Block.__format.newLine)));
block = [];
return;
}
if (blockCounter > 0) { return block.push(line); }
this.properties.push(new Property().convertFromString(line));
});
return this;
}
Sets Block
‘s type that must be a string
setType(type) {
if (typeof type !== "string" && !(type instanceof String)) {
throw new Error("[Block] [setType()] The type must be an instance of `String`");
}
this.type = type;
return this;
}
Adds child block to Block
. The child block must be an instance of Block
addBlock(block) {
if (!(block instanceof Block)) {
throw new Error("[Block] [addBlock()] The block must be an instance of `Block`");
}
this.blocks.push(block);
return this;
}
Adds property to Block
. The property must be an instance of Property
addProperty(property) {
if (!(property instanceof Property)) {
throw new Error("[Block] [addProperty()] The property must be an instance of `Property`");
}
this.properties.push(property);
return this;
}
}
Block.__format = {
regexBlockBegin : /^BEGIN:/i,
regexBlockEnd : /^END:/i,
blockBegin : "BEGIN:",
blockEnd : "END:",
newLine : "\n",
multiLineBegin : " ",
prepareString : function(string) {
return string && string.replace(/\r/g, "").trim();
}
};