You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			39 lines
		
	
	
		
			947 B
		
	
	
	
		
			TypeScript
		
	
		
		
			
		
	
	
			39 lines
		
	
	
		
			947 B
		
	
	
	
		
			TypeScript
		
	
| 
											8 years ago
										 | import { instance, PhoneNumberFormat } from '../util/libphonenumberInstance'; | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 | export function format( | ||
| 
											8 years ago
										 |   phoneNumber: string, | ||
| 
											8 years ago
										 |   options: { | ||
|  |     ourRegionCode: string; | ||
|  |   } | ||
|  | ) { | ||
|  |   try { | ||
|  |     const { ourRegionCode } = options; | ||
| 
											8 years ago
										 |     const parsedNumber = instance.parse(phoneNumber); | ||
| 
											8 years ago
										 |     const regionCode = instance.getRegionCodeForNumber(parsedNumber); | ||
|  | 
 | ||
|  |     if (ourRegionCode && regionCode === ourRegionCode) { | ||
|  |       return instance.format(parsedNumber, PhoneNumberFormat.NATIONAL); | ||
|  |     } | ||
|  | 
 | ||
|  |     return instance.format(parsedNumber, PhoneNumberFormat.INTERNATIONAL); | ||
|  |   } catch (error) { | ||
| 
											8 years ago
										 |     return phoneNumber; | ||
| 
											8 years ago
										 |   } | ||
|  | } | ||
| 
											8 years ago
										 | 
 | ||
|  | export function parse( | ||
|  |   phoneNumber: string, | ||
|  |   options: { | ||
|  |     regionCode: string; | ||
|  |   } | ||
|  | ): string { | ||
|  |   const { regionCode } = options; | ||
|  |   const parsedNumber = instance.parse(phoneNumber, regionCode); | ||
|  | 
 | ||
|  |   if (instance.isValidNumber(parsedNumber)) { | ||
|  |     return instance.format(parsedNumber, PhoneNumberFormat.E164); | ||
|  |   } | ||
|  | 
 | ||
|  |   return phoneNumber; | ||
|  | } |