maz-ui
Version:
A standalone components library for Vue.Js 3 & Nuxt.Js 3
1 lines • 4.58 kB
JavaScript
import{ref}from"vue";import{AsYouType,getCountries,getCountryCallingCode,getExampleNumber,isSupportedCountry,parsePhoneNumberFromString}from"libphonenumber-js";import{fetchLocaleIp}from"@maz-ui/utils/helpers/fetchLocaleIp";var examples=ref();function isCountryAvailable(locale){try{return isSupportedCountry(locale)||(console.error(`[maz-ui](MazInputPhoneNumber) The code country "${locale}" is not available`),!1)}catch(error){return console.error(`[maz-ui](MazInputPhoneNumber) ${error}`),!1}}function getPhoneNumberResults({phoneNumber,countryCode,checkCountryCode=!1}){try{if(!phoneNumber)return{isValid:!1,countryCode};let parsedNumber=parsePhoneNumberFromString(phoneNumber,countryCode??void 0);return{isValid:(parsedNumber?.isValid()??!1)&&!!(!(countryCode&&checkCountryCode)||parsedNumber?.country&&countryCode===parsedNumber.country),countryCode,parsedCountryCode:parsedNumber?.country,isPossible:parsedNumber?.isPossible(),countryCallingCode:parsedNumber?.countryCallingCode,nationalNumber:parsedNumber?.nationalNumber,type:parsedNumber?.getType(),formatInternational:parsedNumber?.formatInternational(),formatNational:parsedNumber?.formatNational(),uri:parsedNumber?.getURI(),e164:parsedNumber?.format(`E.164`),rfc3966:parsedNumber?.format(`RFC3966`),possibleCountries:parsedNumber?.getPossibleCountries(),phoneNumber}}catch(error){throw Error(`[MazInputPhoneNumber](getResultsFromPhoneNumber) ${error}`,{cause:error})}}async function getPhoneNumberExamplesFile(){let{default:data}=await import(`libphonenumber-js/examples.mobile.json`);return data}function getPhoneNumberExample(countryCode){try{return examples.value&&countryCode?getExampleNumber(countryCode,examples.value)?.formatNational():void 0}catch(error){console.error(`[maz-ui](MazInputPhoneNumber) ${error}`)}}function getAsYouTypeFormat(countryCode,phoneNumber){try{return!phoneNumber||!countryCode||typeof countryCode!=`string`||countryCode.length!==2?phoneNumber:new AsYouType(countryCode).input(phoneNumber)}catch(error){return console.error(`[MazInputPhoneNumber](getAsYouTypeFormat) Error with countryCode: "${countryCode}", phoneNumber: "${phoneNumber}"`,error),phoneNumber}}function isSameCountryCallingCode(countryCode,countryCode2){return getCountryCallingCode(countryCode)===getCountryCallingCode(countryCode2)}async function loadExamples(){try{if(examples.value)return;examples.value=await getPhoneNumberExamplesFile()}catch(error){console.error(`[maz-ui](MazInputPhoneNumber) while loading phone number examples file`,error)}}function useLibphonenumber(){return{examples,getAsYouTypeFormat,getPhoneNumberResults,getPhoneNumberExamplesFile,getPhoneNumberExample,isSameCountryCallingCode,isCountryAvailable,getCountries,getCountryCallingCode,loadExamples}}function getBrowserLocale(){if(globalThis.window===void 0)return;let browserLocale=globalThis.navigator.language;if(!browserLocale)return;let parts=browserLocale.split(`-`);let locale=parts.length>1?parts[1].slice(0,2).toUpperCase():parts[0].slice(0,2).toUpperCase();return locale===`EN`&&(locale=`US`),locale===`JA`&&(locale=`JP`),{locale,browserLocale}}var displayNamesInstance;var displayNamesLocale;function getCountryName(locale,code,customCountriesNameListByIsoCode){return customCountriesNameListByIsoCode?.[code]?customCountriesNameListByIsoCode[code]:((displayNamesLocale!==locale||!displayNamesInstance)&&(displayNamesLocale=locale,displayNamesInstance=new Intl.DisplayNames([locale],{type:`region`})),displayNamesInstance.of(code))}function getCountryList(locale,customCountriesNameListByIsoCode){let countriesList=[];let isoList=getCountries();locale=locale??getBrowserLocale()?.browserLocale??`en-US`;for(let code of isoList){let name=getCountryName(locale,code,customCountriesNameListByIsoCode);if(name)try{let dialCode=getCountryCallingCode(code);countriesList.push({code,dialCode,name})}catch(error){console.error(`[MazInputPhoneNumber](getCountryCallingCode) ${error}`)}}return countriesList}async function fetchCountryCode(){try{let countryCode=await fetchLocaleIp();return countryCode?{data:countryCode,error:void 0}:{data:void 0,error:Error(`[MazInputPhoneNumber](fetchCountryCode) No country code found`)}}catch(error){return{data:void 0,error:Error(`[MazInputPhoneNumber](fetchCountryCode) ${error}`)}}}var isLetterOrNumberRegex=/[^\d ()+-]/g;function sanitizePhoneNumber(input){return input?input.replaceAll(isLetterOrNumberRegex,``).trim():``}function useMazInputPhoneNumber(){return{fetchCountryCode,getBrowserLocale,getCountryList,sanitizePhoneNumber}}export{useLibphonenumber as n,useMazInputPhoneNumber as t};