mirror of
				https://github.com/CorentinTh/it-tools.git
				synced 2025-11-04 05:53:25 +00:00 
			
		
		
		
	feat(new tool): phone parser and normalizer
This commit is contained in:
		
				
					committed by
					
						
						Corentin THOMASSET
					
				
			
			
				
	
			
			
			
						parent
						
							3f6c8f0edd
						
					
				
				
					commit
					ce3150c65d
				
			@@ -0,0 +1,41 @@
 | 
			
		||||
import type { NumberType } from 'libphonenumber-js/types';
 | 
			
		||||
import lookup from 'country-code-lookup';
 | 
			
		||||
 | 
			
		||||
export { formatTypeToHumanReadable, getFullCountryName, getDefaultCountryCode };
 | 
			
		||||
 | 
			
		||||
const typeToLabel: Record<NonNullable<NumberType>, string> = {
 | 
			
		||||
  MOBILE: 'Mobile',
 | 
			
		||||
  FIXED_LINE: 'Fixed line',
 | 
			
		||||
  FIXED_LINE_OR_MOBILE: 'Fixed line or mobile',
 | 
			
		||||
  PERSONAL_NUMBER: 'Personal number',
 | 
			
		||||
  PREMIUM_RATE: 'Premium rate',
 | 
			
		||||
  SHARED_COST: 'Shared cost',
 | 
			
		||||
  TOLL_FREE: 'Toll free',
 | 
			
		||||
  UAN: 'Universal access number',
 | 
			
		||||
  VOICEMAIL: 'Voicemail',
 | 
			
		||||
  VOIP: 'VoIP',
 | 
			
		||||
  PAGER: 'Pager',
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
function formatTypeToHumanReadable(type: NumberType): string | undefined {
 | 
			
		||||
  if (!type) return undefined;
 | 
			
		||||
 | 
			
		||||
  return typeToLabel[type];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function getFullCountryName(countryCode: string | undefined) {
 | 
			
		||||
  if (!countryCode) return undefined;
 | 
			
		||||
 | 
			
		||||
  return lookup.byIso(countryCode)?.country;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function getDefaultCountryCode({
 | 
			
		||||
  locale = window.navigator.language,
 | 
			
		||||
  defaultCode = 'FR',
 | 
			
		||||
}: { locale?: string; defaultCode?: string } = {}): string {
 | 
			
		||||
  const countryCode = locale.split('-')[1]?.toUpperCase();
 | 
			
		||||
 | 
			
		||||
  if (!countryCode) return defaultCode;
 | 
			
		||||
 | 
			
		||||
  return lookup.byIso(countryCode)?.iso2 ?? defaultCode;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user