Release Vulpes OS 2.7 Preview 25 for Desktop, Android and Pixel 3a
This commit is contained in:
parent
6a44daeaaa
commit
3a1fba4efe
1 changed files with 1301 additions and 196 deletions
50
host/VCard.sys.mjs
Normal file
50
host/VCard.sys.mjs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
// Gaia contacts imported from a user-selected UTF-8 vCard file.
|
||||
function unescapeText(value) {
|
||||
return value.replace(/\\([nN,;\\])/g, (_, char) => /n/i.test(char) ? '\n' : char);
|
||||
}
|
||||
|
||||
export function readVCards(raw) {
|
||||
const cards = [];
|
||||
let card;
|
||||
const lines = raw.replace(/^\uFEFF/, '').replace(/\r\n?/g, '\n').replace(/\n[ \t]/g, '').split('\n');
|
||||
for (const line of lines) {
|
||||
if (!line.trim()) continue;
|
||||
if (/^BEGIN:VCARD$/i.test(line)) {
|
||||
if (card) throw Error('INVALID_VCARD');
|
||||
card = {name: [], tel: [], email: []};
|
||||
continue;
|
||||
}
|
||||
if (!card) throw Error('INVALID_VCARD');
|
||||
if (/^END:VCARD$/i.test(line)) {
|
||||
if (!card.name.length) {
|
||||
const name = [...(card.givenName || []), ...(card.familyName || [])].join(' ');
|
||||
if (name) card.name.push(name);
|
||||
}
|
||||
if (!card.name.length && !card.tel.length && !card.email.length) throw Error('INVALID_VCARD');
|
||||
cards.push(card);
|
||||
card = null;
|
||||
continue;
|
||||
}
|
||||
const field = /^((?:[^":]|"[^"]*")*):(.*)$/.exec(line);
|
||||
if (!field || !field[2]) continue;
|
||||
const [property, ...params] = field[1].split(';');
|
||||
const key = property.split('.').pop().toUpperCase();
|
||||
const value = unescapeText(field[2]);
|
||||
if (key === 'FN') card.name = [value];
|
||||
if (key === 'N') {
|
||||
const [, family, given] = /^((?:\\.|[^;\\])*)(?:;((?:\\.|[^;\\])*))?/.exec(field[2]);
|
||||
if (family) card.familyName = [unescapeText(family)];
|
||||
if (given) card.givenName = [unescapeText(given)];
|
||||
}
|
||||
if (key === 'TEL' || key === 'EMAIL') {
|
||||
const types = params.flatMap(param => {
|
||||
if (param.includes('=') && !/^TYPE=/i.test(param)) return [];
|
||||
return param.replace(/^TYPE=/i, '').replace(/"/g, '').toLowerCase().split(',');
|
||||
}).filter(Boolean).map(type => type === 'cell' ? 'mobile' : type);
|
||||
const clean = key === 'TEL' ? value.replace(/^tel:/i, '') : value.replace(/^mailto:/i, '');
|
||||
if (clean) card[key.toLowerCase()].push({value: clean, type: types.length ? types : ['other']});
|
||||
}
|
||||
}
|
||||
if (card || !cards.length) throw Error('INVALID_VCARD');
|
||||
return cards;
|
||||
}
|
||||
Loading…
Reference in a new issue