
Security News
The Hidden Blast Radius of the Axios Compromise
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.
phone-forge
Advanced tools
A comprehensive JavaScript library for formatting, validating, and analyzing phone numbers with international country database
A comprehensive JavaScript library for formatting, validating, and analyzing phone numbers with a complete international phone database containing all world countries and their dial codes.
npm install phone-forge
const {
formatPhoneNumber,
isValidPhoneNumber,
getPhoneNumberInfo,
phoneUtils,
} = require("phone-forge");
// Basic formatting
formatPhoneNumber("2128691246");
// Returns: "+1 (212) 869-1246"
// International formatting with country detection
formatPhoneNumber("447700900123", {
format: "international",
autoDetect: true,
});
// Returns: "+44 7700900123"
// Get detailed phone number information
const info = getPhoneNumberInfo("447700900123");
console.log(info.possibleCountries[0].countries[0].name); // "United Kingdom"
Enhanced phone number formatting with multiple format options and country detection.
Parameters:
phoneNumber (string): The phone number to formatoptions (object, optional):
format (string): Format type - 'us', 'international', 'national', 'e164'countryCode (string): Country code (ISO2, ISO3, or dial code)autoDetect (boolean): Auto-detect country from phone numberstrict (boolean): Enable strict validation modeExamples:
// US format (default)
formatPhoneNumber("2128691246");
// "+1 (212) 869-1246"
// International format with specific country
formatPhoneNumber("15123456789", {
format: "international",
countryCode: "DE",
});
// "+49 15123456789"
// E.164 format
formatPhoneNumber("2128691246", {
format: "e164",
countryCode: "US",
});
// "+12128691246"
// National format
formatPhoneNumber("447700900123", {
format: "national",
autoDetect: true,
});
// "07700 900123" (UK national format)
// Auto-detection
formatPhoneNumber("33142868326", {
format: "international",
autoDetect: true,
});
// "+33 1 42 86 83 26"
Enhanced validation with country-specific rules.
Parameters:
phoneNumber (string): Phone number to validateoptions (object, optional):
countryCode (string): Expected country codestrict (boolean): Strict validation modeExamples:
isValidPhoneNumber("(212) 869-1246");
// true
isValidPhoneNumber("447700900123", { strict: true });
// true (valid international format)
isValidPhoneNumber("123", { strict: true });
// false (too short for any country)
isValidPhoneNumber("2128691246", { countryCode: "US" });
// true
Get comprehensive information about a phone number.
Returns: Object with phone number analysis
const info = getPhoneNumberInfo("447700900123");
console.log(info);
// {
// valid: true,
// originalInput: "447700900123",
// digits: "447700900123",
// length: 12,
// possibleCountries: [{
// dialCode: "+44",
// countries: [{
// name: "United Kingdom",
// iso2: "GB",
// iso3: "GBR",
// flag: "🇬🇧"
// }],
// nationalNumber: "7700900123"
// }],
// formats: {
// international: "+44 7700900123",
// e164: "+447700900123",
// national: "07700 900123"
// }
// }
Extract only digits from a phone number string.
extractDigits("(212) 869-1246");
// "2128691246"
Access the comprehensive international phone database through phoneUtils:
const country = phoneUtils.getCountryByDialCode("+49");
// {
// name: "Germany",
// iso2: "DE",
// iso3: "DEU",
// dialCode: "+49",
// flag: "🇩🇪"
// }
const country = phoneUtils.getCountryByISO2("US");
const country2 = phoneUtils.getCountryByISO3("USA");
const country = phoneUtils.getCountryByName("United Kingdom");
const detected = phoneUtils.detectCountryFromPhoneNumber("4915123456789");
// Returns array of possible countries with dial code matches
const results = phoneUtils.searchCountries({
name: "United",
dialCode: "+1",
});
// Returns countries matching all criteria
const codes = phoneUtils.getAllDialCodes();
// ["+1", "+7", "+20", "+27", "+30", "+31", ...]
const stats = phoneUtils.getDatabaseStats();
// {
// totalCountries: 249,
// totalDialCodes: 230,
// averageDialCodeLength: 3.2,
// shortestDialCode: "+1",
// longestDialCode: "+1684",
// commonRegions: { "+1": 25, "+7": 2, "+44": 4, ... },
// version: "1.0.0",
// lastUpdated: "2025-08-10"
// }
+1 (XXX) XXX-XXXX+CC XXXXXXXXXX+CCXXXXXXXXXX (ITU-T standard)XXX-XXXX (for 7-digit numbers)The library includes a comprehensive database with 249 countries and territories, featuring:
// Format a German mobile number
const germanNumber = formatPhoneNumber("15123456789", {
format: "international",
countryCode: "DE",
});
// "+49 15123456789"
// Format for France with auto-detection
const frenchNumber = formatPhoneNumber("33142868326", {
format: "national",
autoDetect: true,
});
// "01 42 86 83 26"
// Handle +1 region (US, Canada, Caribbean)
const countries = phoneUtils.getCountriesByDialCode("+1");
console.log(countries.map((c) => c.name));
// ["United States", "Canada", "Bahamas", "Barbados", ...]
// Detect specific country from full number
const info = getPhoneNumberInfo("12128691246");
console.log(info.possibleCountries[0].countries[0].name);
// "United States" (or "Canada" - both use +1)
// Strict validation for specific country
const isValidUS = isValidPhoneNumber("2128691246", {
countryCode: "US",
strict: true,
});
// Validate international format
const isValidIntl = isValidPhoneNumber("447700900123", {
strict: true,
});
const phoneNumbers = [
"2128691246", // US
"447700900123", // UK
"4915123456789", // Germany
"33142868326", // France
];
const results = phoneNumbers.map((number) => {
const info = getPhoneNumberInfo(number);
return {
original: number,
country: info.possibleCountries[0]?.countries[0]?.name,
international: info.formats?.international,
valid: info.valid,
};
});
console.log(results);
The library provides detailed error messages for various scenarios:
// Missing phone number
formatPhoneNumber("");
// Error: "Phone number is required"
// Invalid country code
formatPhoneNumber("123456789", { countryCode: "XX" });
// Error: "Unknown country code: XX"
// Insufficient information for international format
formatPhoneNumber("123456789", { format: "international" });
// Error: "Cannot format as international without country information"
// Invalid US format
formatPhoneNumber("22128691246");
// Error: "11-digit numbers must start with country code 1"
Full TypeScript definitions are included:
import {
formatPhoneNumber,
isValidPhoneNumber,
getPhoneNumberInfo,
phoneUtils,
FormatOptions,
PhoneNumberInfo,
CountryInfo,
} from "phone-forge";
const options: FormatOptions = {
format: "international",
countryCode: "DE",
autoDetect: true,
strict: false,
};
const formatted: string = formatPhoneNumber("15123456789", options);
const info: PhoneNumberInfo = getPhoneNumberInfo("447700900123");
const country: CountryInfo | null = phoneUtils.getCountryByISO2("US");
The library is optimized for high-performance applications:
// Performance test example
console.time("1000 lookups");
for (let i = 0; i < 1000; i++) {
phoneUtils.getCountryByDialCode("+1");
formatPhoneNumber("2128691246");
isValidPhoneNumber("447700900123");
}
console.timeEnd("1000 lookups");
// Typically < 100ms
If upgrading from the basic version:
// Old way
const { formatPhoneNumber } = require("phone-number-formatter");
formatPhoneNumber("2128691246");
// New way (backward compatible)
const { formatPhoneNumber } = require("phone-forge");
formatPhoneNumber("2128691246"); // Same result
// New features
const info = getPhoneNumberInfo("2128691246");
const country = phoneUtils.getCountryByDialCode("+1");
Works in all modern browsers and Node.js environments:
phone-forge/
├── src/
│ ├── index.js # Main library
│ ├── phone-database.json # Complete country database
│ ├── phone-utils.js # Database utility functions
│ └── index.d.ts # TypeScript definitions
├── test/
│ ├── index.test.js # Basic tests
├── CONTRIBUTING.md
├── LICENSE.md
├── package.json
└── README.md
We welcome contributions! Please see our Contributing Guide for details.
git clone https://github.com/easyware-io/phone-forge.git
cd phone-forge
npm install
npm test
# Run all tests
npm test
# Run specific test file
node test/enhanced.test.js
MIT License - see LICENSE file for details.
Phone Forge - Making international phone number handling simple and reliable. 🌍📱
FAQs
A comprehensive JavaScript library for formatting, validating, and analyzing phone numbers with international country database
We found that phone-forge demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.