Skip to content

Instantly share code, notes, and snippets.

@kassiogluten
Created January 23, 2024 20:19
Show Gist options
  • Save kassiogluten/a580dce15ddf52403f35a3a9cb84e3f5 to your computer and use it in GitHub Desktop.
Save kassiogluten/a580dce15ddf52403f35a3a9cb84e3f5 to your computer and use it in GitHub Desktop.
Extrair informações de um numero internacional
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