Google's libphonenumber pre-compiled with the closure compiler for comprehensive phone number parsing, validation, and formatting.
npx @tessl/cli install tessl/npm-awesome-phonenumber@7.5.0Awesome PhoneNumber is a pre-compiled TypeScript/JavaScript library wrapping Google's libphonenumber with a simplified interface. It provides comprehensive phone number parsing, validation, formatting, and finding capabilities with zero runtime dependencies and optimal performance.
npm install awesome-phonenumberimport {
parsePhoneNumber,
findNumbers,
getExample,
getAsYouType,
getNumberFrom,
getCountryCodeForRegionCode,
getRegionCodeForCountryCode,
getSupportedCallingCodes,
getSupportedRegionCodes
} from "awesome-phonenumber";For CommonJS:
const {
parsePhoneNumber,
findNumbers,
getExample,
getAsYouType,
getNumberFrom,
getCountryCodeForRegionCode,
getRegionCodeForCountryCode,
getSupportedCallingCodes,
getSupportedRegionCodes
} = require("awesome-phonenumber");import { parsePhoneNumber, findNumbers } from "awesome-phonenumber";
// Parse phone numbers
const phoneNumber = parsePhoneNumber("0707123456", { regionCode: "SE" });
// Or international format
const intlNumber = parsePhoneNumber("+46707123456");
if (phoneNumber.valid) {
console.log(phoneNumber.number.international); // "+46 70 712 34 56"
console.log(phoneNumber.type); // "mobile"
}
// Find phone numbers in text
const matches = findNumbers("Call me at 0707123456 or +1-555-123-4567", {
defaultRegionCode: "SE"
});
console.log(matches.length); // 2
// Short number validation (emergency numbers, service codes)
const emergency = parsePhoneNumber("112", { regionCode: "SE" });
console.log(emergency.valid); // false (not a regular phone number)
console.log(emergency.shortValid); // true (valid emergency number)Awesome PhoneNumber is built around several key components:
parsePhoneNumber function for converting strings to structured phone number objectsfindNumbers function for extracting phone numbers from unstructured textgetExample function for generating sample phone numbersAsYouType class for real-time formatting as users typeCore parsing functionality for converting phone number strings into structured, validated objects with comprehensive metadata.
function parsePhoneNumber(
phoneNumber: string,
options?: PhoneNumberParseOptions
): ParsedPhoneNumber;
interface PhoneNumberParseOptions {
regionCode?: string;
}
type ParsedPhoneNumber = ParsedPhoneNumberValid | ParsedPhoneNumberInvalid;Text scanning functionality to locate and extract phone numbers from unstructured text with configurable leniency and region settings.
function findNumbers(
text: string,
options?: FindNumbersOptions
): PhoneNumberMatch[];
interface FindNumbersOptions {
defaultRegionCode?: string;
leniency?: FindNumbersLeniency;
maxTries?: number;
}Utilities for converting between country calling codes and ISO region codes, plus discovery of supported regions.
function getCountryCodeForRegionCode(regionCode: string): number;
function getRegionCodeForCountryCode(countryCode: number): string;
function getSupportedCallingCodes(): string[];
function getSupportedRegionCodes(): string[];Tools for generating example phone numbers and providing real-time formatting as users input phone numbers.
function getExample(regionCode: string, type?: PhoneNumberTypes): ParsedPhoneNumber;
function getAsYouType(regionCode: string): AsYouType;
class AsYouType {
addChar(char: string): string;
number(): string;
removeChar(): string;
reset(number?: string): string;
getPhoneNumber(): ParsedPhoneNumber;
}Utility for determining how a phone number would be dialed from a specific country, useful for international calling applications.
function getNumberFrom(
parsedPhoneNumber: ParsedPhoneNumberValid,
regionCode?: string
): PhoneNumberFrom;
type PhoneNumberFrom = PhoneNumberFromValid | PhoneNumberFromInvalid;type PhoneNumberFormat = 'e164' | 'international' | 'national' | 'rfc3966' | 'significant';
type PhoneNumberTypes = 'fixed-line' | 'fixed-line-or-mobile' | 'mobile' | 'pager'
| 'personal-number' | 'premium-rate' | 'shared-cost' | 'toll-free' | 'uan'
| 'voip' | 'unknown';
type PhoneNumberPossibility = 'is-possible' | 'invalid' | 'invalid-country-code'
| 'too-long' | 'too-short' | 'unknown';
type FindNumbersLeniency = 'possible' | 'valid';
interface ParsedPhoneNumberValid {
valid: true;
number: {
input: string;
international: string;
national: string;
e164: string;
rfc3966: string;
significant: string;
};
possibility: PhoneNumberPossibility;
regionCode: string;
possible: boolean;
shortValid: boolean;
shortPossible: boolean;
canBeInternationallyDialled: boolean;
type: PhoneNumberTypes;
countryCode: number;
typeIsMobile: boolean;
typeIsFixedLine: boolean;
}
interface ParsedPhoneNumberInvalid {
valid: false;
possible: boolean;
possibility: PhoneNumberPossibility;
shortValid: boolean;
shortPossible: boolean;
error?: unknown;
}