diff --git a/src/core/vcard.test.ts b/src/core/vcard.test.ts new file mode 100644 index 00000000..bc0d912a --- /dev/null +++ b/src/core/vcard.test.ts @@ -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(); + }); +}); diff --git a/src/core/vcard.ts b/src/core/vcard.ts index c4a819c2..7af2403c 100644 --- a/src/core/vcard.ts +++ b/src/core/vcard.ts @@ -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, }; }