| 
									
										
										
										
											2023-05-03 00:57:15 +02:00
										 |  |  | import {parsePhoneNumberFromString} from "libphonenumber-js" | 
					
						
							|  |  |  | import {Validator} from "../Validator" | 
					
						
							|  |  |  | import {Translation} from "../../i18n/Translation"; | 
					
						
							|  |  |  | import Translations from "../../i18n/Translations"; | 
					
						
							| 
									
										
										
										
											2023-03-29 17:21:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class PhoneValidator extends Validator { | 
					
						
							|  |  |  |     constructor() { | 
					
						
							|  |  |  |         super("phone", "A phone number", "tel") | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-03 00:57:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     getFeedback(s: string, requestCountry?: () => string): Translation { | 
					
						
							|  |  |  |         const tr = Translations.t.validation.phone | 
					
						
							|  |  |  |         const generic = tr.feedback | 
					
						
							|  |  |  |         if(requestCountry){ | 
					
						
							|  |  |  |         const country = requestCountry() | 
					
						
							|  |  |  |             if(country){ | 
					
						
							|  |  |  |                 return  tr.feedbackCountry.Subs({country}) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return generic | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public isValid(str, country: () => string): boolean { | 
					
						
							| 
									
										
										
										
											2023-03-29 17:21:20 +02:00
										 |  |  |         if (str === undefined) { | 
					
						
							|  |  |  |             return false | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (str.startsWith("tel:")) { | 
					
						
							|  |  |  |             str = str.substring("tel:".length) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         let countryCode = undefined | 
					
						
							|  |  |  |         if (country !== undefined) { | 
					
						
							|  |  |  |             countryCode = country()?.toUpperCase() | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return parsePhoneNumberFromString(str, countryCode)?.isValid() ?? false | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-03 00:57:15 +02:00
										 |  |  |     public reformat(str, country: () => string) { | 
					
						
							| 
									
										
										
										
											2023-03-29 17:21:20 +02:00
										 |  |  |         if (str.startsWith("tel:")) { | 
					
						
							|  |  |  |             str = str.substring("tel:".length) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-05-03 00:57:15 +02:00
										 |  |  |         let countryCode = undefined | 
					
						
							|  |  |  |         if(country){ | 
					
						
							|  |  |  |             countryCode = country() | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-03-29 17:21:20 +02:00
										 |  |  |         return parsePhoneNumberFromString( | 
					
						
							|  |  |  |             str, | 
					
						
							| 
									
										
										
										
											2023-05-03 00:57:15 +02:00
										 |  |  |             countryCode?.toUpperCase() as any | 
					
						
							| 
									
										
										
										
											2023-03-29 17:21:20 +02:00
										 |  |  |         )?.formatInternational() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |