global-phone-validator 1.2.0 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/utils.js CHANGED
@@ -10,36 +10,35 @@ exports.cleanPhoneNumber = cleanPhoneNumber;
10
10
  exports.getAllCountryCodes = getAllCountryCodes;
11
11
  const CountryCodes_json_1 = __importDefault(require("./CountryCodes.json"));
12
12
  const COUNTRY_CODES = CountryCodes_json_1.default;
13
- /**
14
- * Get country dial code by country code (e.g., "IN" -> "91")
15
- */
16
13
  function getCountryDialCode(countryCode) {
17
14
  const country = COUNTRY_CODES.find((c) => c.code.toUpperCase() === countryCode.toUpperCase());
18
15
  return country ? country.dial_code.replace("+", "") : null;
19
16
  }
20
- /**
21
- * Get country code by dial code (e.g., "91" -> CountryCode for India)
22
- */
23
17
  function getCountryCodeByDialCode(dialCode) {
24
18
  const normalizedDialCode = dialCode.replace("+", "");
25
- return (COUNTRY_CODES.find((c) => c.dial_code.replace("+", "") === normalizedDialCode) || null);
19
+ const matches = COUNTRY_CODES.filter((c) => c.dial_code.replace("+", "") === normalizedDialCode);
20
+ if (matches.length === 0)
21
+ return null;
22
+ if (matches.length === 1)
23
+ return matches[0];
24
+ const preferred = ["US", "GB", "CA", "AU"];
25
+ for (const code of preferred) {
26
+ const match = matches.find((c) => c.code === code);
27
+ if (match)
28
+ return match;
29
+ }
30
+ return matches[0];
26
31
  }
27
- /**
28
- * Find country code from phone number by matching dial codes
29
- * Returns the longest matching dial code
30
- */
31
32
  function detectCountryFromPhoneNumber(phoneNumber) {
32
33
  const cleaned = phoneNumber.replace(/[^0-9+]/g, "");
33
34
  if (!cleaned.startsWith("+")) {
34
35
  return { country: null, dialCode: "", nationalNumber: cleaned };
35
36
  }
36
- // Try to match dial codes from longest to shortest (1-4 digits)
37
37
  for (let len = 4; len >= 1; len--) {
38
38
  const potentialDialCode = cleaned.substring(1, 1 + len);
39
39
  const country = getCountryCodeByDialCode(potentialDialCode);
40
40
  if (country) {
41
41
  const nationalNumber = cleaned.substring(1 + len);
42
- // Basic sanity check: national number should be at least 4 digits
43
42
  if (nationalNumber.length >= 4) {
44
43
  return {
45
44
  country,
@@ -51,16 +50,9 @@ function detectCountryFromPhoneNumber(phoneNumber) {
51
50
  }
52
51
  return { country: null, dialCode: "", nationalNumber: cleaned.substring(1) };
53
52
  }
54
- /**
55
- * Clean phone number by removing spaces, dashes, parentheses, etc.
56
- */
57
53
  function cleanPhoneNumber(input) {
58
54
  return input.replace(/\s+/g, "").replace(/[^0-9+]/g, "");
59
55
  }
60
- /**
61
- * Get all country codes
62
- */
63
56
  function getAllCountryCodes() {
64
57
  return COUNTRY_CODES;
65
58
  }
66
- //# sourceMappingURL=utils.js.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "global-phone-validator",
3
- "version": "1.2.0",
4
- "description": "A reusable Node.js + TypeScript package for validating phone numbers worldwide. Supports any country using CountryCodes JSON, handles international formats (+CC), 0-prefixed, and plain digits. Includes mobile/landline detection for India and returns standardized E.164 format.",
3
+ "version": "1.3.2",
4
+ "description": "Comprehensive phone number validator for all countries. Validates mobile, landline, VoIP, toll-free, and premium numbers. Supports 50+ countries with type detection. Returns E.164 format.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "exports": {
@@ -26,12 +26,11 @@
26
26
  "validator",
27
27
  "validation",
28
28
  "mobile",
29
- "landline",
29
+ "mobile-phone",
30
30
  "international",
31
31
  "E164",
32
32
  "E.164",
33
33
  "country-code",
34
- "india",
35
34
  "worldwide",
36
35
  "typescript"
37
36
  ],