UNPKG

social-link-parser

Version:

Extract usernames, IDs, and metadata from social media URLs across 100+ platforms

46 lines 1.53 kB
import { Platforms } from '../../core/types.js'; import { parsePhoneNumberFromString } from 'libphonenumber-js'; const DIGIT_RE = /\+?[0-9 ()\-\.]{5,}/; export const phone = { id: Platforms.Phone, name: 'Phone', domains: [], patterns: { profile: DIGIT_RE, handle: /^\+?[0-9]{5,15}$/, }, detect(raw) { if (raw.startsWith('tel:')) raw = raw.slice(4); const pn = parsePhoneNumberFromString(raw); return !!pn; }, extract(raw, res) { let num = raw.startsWith('tel:') ? raw.slice(4) : raw; const pn = parsePhoneNumberFromString(num); if (pn) { res.userId = pn.number; res.metadata.phoneNumber = pn.formatInternational(); const country = pn.country || (pn.countryCallingCode === '1' ? 'US' : ''); res.metadata.phoneCountry = country; res.metadata.isProfile = true; res.metadata.contentType = 'phone'; } }, validateHandle(handle) { const pn = parsePhoneNumberFromString(handle); return !!pn; }, buildProfileUrl(username) { const pn = parsePhoneNumberFromString(username); const e164 = pn ? pn.number : username.replace(/[^0-9+]/g, ''); return `tel:${e164}`; }, normalizeUrl(url) { if (url.startsWith('tel:')) url = url.slice(4); const pn = parsePhoneNumberFromString(url); return pn ? `tel:${pn.number}` : url; }, }; //# sourceMappingURL=index.js.map