[core] Add vcard waid and tests

This commit is contained in:
devlikepro committed 2026-03-05 12:41:17 +07:00
1 parent 615ccda4c3
commit d29e4f8351
2 files changed
+36 -2

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
import { parseVCardV3 } from './vcard';
describe('parseVCardV3', () => {
it('parses vcard with waid', () => {
const vcard =
'BEGIN:VCARD\nVERSION:3.0\nFN:Jane Doe\nORG:Company Name;\nTEL;type=CELL;type=VOICE;waid=911111111111:+91 11111 11111\nEND:VCARD';
const result = parseVCardV3(vcard);
expect(result.fullName).toBe('Jane Doe');
expect(result.phoneNumbers).toEqual(['+91 11111 11111']);
expect(result.whatsappId).toBe('911111111111');
});
it('parses vcard without waid', () => {
const vcard =
'BEGIN:VCARD\nVERSION:3.0\nFN:Jane Doe\nTEL;type=CELL;type=VOICE:+91 11111 11111\nEND:VCARD';
const result = parseVCardV3(vcard);
expect(result.fullName).toBe('Jane Doe');
expect(result.phoneNumbers).toEqual(['+91 11111 11111']);
expect(result.whatsappId).toBeNull();
});
});
+15 -2
View File
@@ -24,9 +24,14 @@ export function toVcardV3(data: Contact | VCardContact): string {
return parts.join('\n');
}
export function normalizePN(phoneNumber: string): string {
return phoneNumber.replace(/[^0-9]/g, '');
}
export interface SimpleVCardInfo {
fullName: string;
phoneNumbers: string[];
whatsappId: string | null;
}
export function parseVCardV3(vcardText: string): SimpleVCardInfo {
@@ -46,14 +51,22 @@ export function parseVCardV3(vcardText: string): SimpleVCardInfo {
}
}
// Collect all TEL values
// Collect all TEL values and waid parameter
let waid: string | null = null;
const phoneNumbers = comp
.getAllProperties('tel')
.map((p) => String(p.getFirstValue() ?? ''))
.map((p) => {
const w = p.getParameter('waid');
if (w && !waid) {
waid = String(w);
}
return String(p.getFirstValue() ?? '');
})
.filter((n) => n.length > 0);
return {
fullName: fullName || '',
phoneNumbers: phoneNumbers,
whatsappId: waid,
};
}