Created
January 23, 2024 20:19
-
-
Save kassiogluten/a580dce15ddf52403f35a3a9cb84e3f5 to your computer and use it in GitHub Desktop.
Extrair informações de um numero internacional
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PhoneNumberUtil } from "google-libphonenumber"; | |
export function manualParsePhoneNumber( | |
input: string, | |
country: string = "BR" | |
): any { | |
const phoneUtil = PhoneNumberUtil.getInstance(); | |
try { | |
const parsedNumber = phoneUtil.parse(input, country); | |
const countryCode = String(parsedNumber.getCountryCode()); | |
const lengthToRemove = | |
phoneUtil.getLengthOfGeographicalAreaCode(parsedNumber); | |
const nationalNumber = parsedNumber.getNationalNumber(); | |
const stringPhoneNumber = String(nationalNumber); | |
const areaCode = String(stringPhoneNumber.slice(0, lengthToRemove)); | |
const phone = String(stringPhoneNumber.slice(lengthToRemove)); | |
return { | |
internationalNumber: input, | |
countryCode, | |
areaCode, | |
phone, | |
nationalNumber, | |
}; | |
} catch (error: unknown) { | |
console.error("Erro ao analisar o número de telefone:", error); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment