Spaces:
Running
Running
File size: 2,074 Bytes
fb4d8fe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | type ParsedVcard = {
name?: string;
phones: string[];
};
const ALLOWED_VCARD_KEYS = new Set(["FN", "N", "TEL"]);
export function parseVcard(vcard?: string): ParsedVcard {
if (!vcard) {
return { phones: [] };
}
const lines = vcard.split(/\r?\n/);
let nameFromN: string | undefined;
let nameFromFn: string | undefined;
const phones: string[] = [];
for (const rawLine of lines) {
const line = rawLine.trim();
if (!line) {
continue;
}
const colonIndex = line.indexOf(":");
if (colonIndex === -1) {
continue;
}
const key = line.slice(0, colonIndex).toUpperCase();
const rawValue = line.slice(colonIndex + 1).trim();
if (!rawValue) {
continue;
}
const baseKey = normalizeVcardKey(key);
if (!baseKey || !ALLOWED_VCARD_KEYS.has(baseKey)) {
continue;
}
const value = cleanVcardValue(rawValue);
if (!value) {
continue;
}
if (baseKey === "FN" && !nameFromFn) {
nameFromFn = normalizeVcardName(value);
continue;
}
if (baseKey === "N" && !nameFromN) {
nameFromN = normalizeVcardName(value);
continue;
}
if (baseKey === "TEL") {
const phone = normalizeVcardPhone(value);
if (phone) {
phones.push(phone);
}
}
}
return { name: nameFromFn ?? nameFromN, phones };
}
function normalizeVcardKey(key: string): string | undefined {
const [primary] = key.split(";");
if (!primary) {
return undefined;
}
const segments = primary.split(".");
return segments[segments.length - 1] || undefined;
}
function cleanVcardValue(value: string): string {
return value.replace(/\\n/gi, " ").replace(/\\,/g, ",").replace(/\\;/g, ";").trim();
}
function normalizeVcardName(value: string): string {
return value.replace(/;/g, " ").replace(/\s+/g, " ").trim();
}
function normalizeVcardPhone(value: string): string {
const trimmed = value.trim();
if (!trimmed) {
return "";
}
if (trimmed.toLowerCase().startsWith("tel:")) {
return trimmed.slice(4).trim();
}
return trimmed;
}
|