vcard4
Version:
An RFC 6350 compliant JavaScript library for generating and parsing version 4.0 vCards. Can also generate RFC 6351 compliant XML vCards and RFC 7095 compliant jCards. TypeScript type declarations are provided.
64 lines (51 loc) • 1.57 kB
JavaScript
import { BaseValue } from "./BaseValue.js";
import { MissingArgument } from "../errors/index.js";
export class TextType extends BaseValue {
static type = "TEXT";
static identifier = "TextType";
#textValue;
#validate(textValue) {
if (typeof textValue === "undefined" || textValue === "")
throw new MissingArgument("Value for TextType must be supplied");
else if (typeof textValue !== "string")
throw new TypeError("Only type string allowed for TextType value");
}
#cleanUp(textValue) {
return textValue
.replaceAll("\\", "\\\\")
.replaceAll(",", "\\,")
.replaceAll(":", "\\:")
.replaceAll(";", "\\;")
.replaceAll("\n", "\\n");
}
#cleanUpXML(textValue) {
return textValue
.replaceAll("&", "&")
.replaceAll(">", ">")
.replaceAll("<", "<")
.replaceAll('"', """)
.replaceAll("'", "'");
}
get value() {
return this.#cleanUp(this.#textValue);
}
get valueXML() {
return `<${this.constructor.type.toLowerCase()}>${this.#cleanUpXML(
this.#textValue
)}</${this.constructor.type.toLowerCase()}>`;
}
get valueJSON() {
return [this.constructor.type.toLowerCase(), this.#textValue];
}
get _unsafe_raw_value() {
return this.#textValue;
}
constructor(textValue) {
super();
this.#validate(textValue);
this.#textValue = textValue;
this.checkAbstractPropertiesAndMethods();
Object.freeze(this);
}
}
Object.freeze(TextType);