where-calling-from
v1.0.3
Published
identifying the country from where the number is frm
Maintainers
Readme
where-calling-from
A lightweight JavaScript utility that detects the country of a phone number by checking its international calling code. Automatically removes spaces, symbols, and formatting before detection.
Features
- Cleans phone numbers from formatting symbols
- Detects country based on international calling code
- Includes data for 30 common countries
- Easy to extend by adding more codes
- Zero dependencies
Installation
npm install where-calling-fromUsage
CommonJS
const whereCallingFrom = require("where-calling-from");
whereCallingFrom("+1 (202) 555-0199");
// Output:
// The phone number :+12025550199 is from United StatesMore Examples
whereCallingFrom(" +212-653_889[441] ");
// The phone number :+212653889441 is from Morocco
whereCallingFrom(" 91-8877-554-321 ");
// The phone number :+918877554321 is from India
whereCallingFrom("999999999");
// There is no country for this numberHow It Works
- Sanitization
number.replaceAll(/[-_()\[\]{}+ ]/g, "");- Prefix Detection
num.startsWith(item.code)- Console Output
The result is printed directly to the console.
Country Code List
The internal data looks like this:
const data = [
{ country: "United States", code: "1" },
{ country: "Canada", code: "1" },
{ country: "United Kingdom", code: "44" },
{ country: "Germany", code: "49" },
{ country: "France", code: "33" },
{ country: "Italy", code: "39" },
{ country: "Spain", code: "34" },
{ country: "Australia", code: "61" },
{ country: "New Zealand", code: "64" },
{ country: "India", code: "91" },
{ country: "Pakistan", code: "92" },
{ country: "Bangladesh", code: "880" },
{ country: "China", code: "86" },
{ country: "Japan", code: "81" },
{ country: "South Korea", code: "82" },
{ country: "Russia", code: "7" },
{ country: "Ukraine", code: "380" },
{ country: "Egypt", code: "20" },
{ country: "South Africa", code: "27" },
{ country: "Nigeria", code: "234" },
{ country: "Kenya", code: "254" },
{ country: "Morocco", code: "212" },
{ country: "Saudi Arabia", code: "966" },
{ country: "United Arab Emirates", code: "971" },
{ country: "Turkey", code: "90" },
{ country: "Brazil", code: "55" },
{ country: "Argentina", code: "54" },
{ country: "Mexico", code: "52" },
{ country: "Chile", code: "56" },
{ country: "Colombia", code: "57" }
];Extending the List
You can add new countries easily:
data.push({ country: "Tunisia", code: "216" });Roadmap
- Return structured results instead of console logging
- Longest-prefix matching
- TypeScript definitions
- Support for full global calling code database
License
MIT License.
