UNPKG

emr-types

Version:

Comprehensive TypeScript Types Library for Electronic Medical Record (EMR) Applications - Domain-Driven Design with Zod Validation

84 lines 2.75 kB
/** * Phone Number Factory Functions */ export const PhoneNumberFactory = { /** * Create a phone number from string */ create: (value, countryCode = '+84') => { // Remove all non-digit characters except + const cleaned = value.replace(/[^\d+]/g, ''); // Basic validation for Vietnamese phone numbers const vietnameseRegex = /^(\+84|84|0)?(3[2-9]|5[689]|7[06-9]|8[1-9]|9[0-46-9])[0-9]{7}$/; const isValid = vietnameseRegex.test(cleaned); if (!isValid) { throw new Error(`Invalid phone number: ${value}`); } // Extract national number let nationalNumber = cleaned; if (cleaned.startsWith('+84')) { nationalNumber = cleaned.substring(3); } else if (cleaned.startsWith('84')) { nationalNumber = cleaned.substring(2); } else if (cleaned.startsWith('0')) { nationalNumber = cleaned.substring(1); } // Format for display const formatted = formatPhoneNumber(nationalNumber, countryCode); return { value: cleaned, isValid: true, countryCode, nationalNumber, formatted, validatedAt: new Date() }; }, /** * Create a phone number from parts */ fromParts: (countryCode, nationalNumber) => { const value = `${countryCode}${nationalNumber}`; return PhoneNumberFactory.create(value, countryCode); } }; /** * Phone Number Validation Functions */ export const PhoneNumberValidator = { /** * Check if a string is a valid Vietnamese phone number */ isValidVietnamese: (value) => { const cleaned = value.replace(/[^\d+]/g, ''); const vietnameseRegex = /^(\+84|84|0)?(3[2-9]|5[689]|7[06-9]|8[1-9]|9[0-46-9])[0-9]{7}$/; return vietnameseRegex.test(cleaned); }, /** * Check if phone number is mobile */ isMobile: (phoneNumber) => { const mobilePrefixes = ['3', '5', '7', '8', '9']; return mobilePrefixes.includes(phoneNumber.nationalNumber.charAt(0)); }, /** * Check if phone number is landline */ isLandline: (phoneNumber) => { return !PhoneNumberValidator.isMobile(phoneNumber); } }; /** * Format phone number for display */ function formatPhoneNumber(nationalNumber, countryCode) { if (nationalNumber.length === 9) { // Vietnamese mobile format: 0123 456 789 return `${countryCode} ${nationalNumber.substring(0, 4)} ${nationalNumber.substring(4, 7)} ${nationalNumber.substring(7)}`; } // Default format return `${countryCode} ${nationalNumber}`; } //# sourceMappingURL=PhoneNumber.js.map