global-phone-validator 1.0.0
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/README.md +218 -0
- package/dist/CountryCodes.json +1212 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +126 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +27 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +67 -0
- package/dist/utils.js.map +1 -0
- package/package.json +50 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { PhoneValidationResult } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Validates phone numbers for any country
|
|
4
|
+
*
|
|
5
|
+
* @param input - Phone number in various formats (+91..., 0..., or plain digits)
|
|
6
|
+
* @param defaultCountry - ISO country code (e.g., "IN", "US") used when country cannot be detected
|
|
7
|
+
* @param mobileOnly - If true, only accepts mobile numbers (currently only for India)
|
|
8
|
+
* @returns PhoneValidationResult with validation status and parsed information
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* validatePhoneNumber("+91 98765 43210") // India with country code
|
|
12
|
+
* validatePhoneNumber("09876543210") // India with 0 prefix
|
|
13
|
+
* validatePhoneNumber("9876543210", "IN") // India plain digits
|
|
14
|
+
* validatePhoneNumber("+1 555 123 4567") // US number
|
|
15
|
+
*/
|
|
16
|
+
export declare function validatePhoneNumber(input: string, defaultCountry?: string, mobileOnly?: boolean): PhoneValidationResult;
|
|
17
|
+
export type { PhoneValidationResult, CountryCode } from "./types";
|
|
18
|
+
export { getAllCountryCodes, getCountryDialCode, getCountryCodeByDialCode } from "./utils";
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAe,MAAM,SAAS,CAAC;AAW7D;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,MAAM,EACb,cAAc,GAAE,MAAa,EAC7B,UAAU,GAAE,OAAe,GAC1B,qBAAqB,CA0GvB;AAGD,YAAY,EAAE,qBAAqB,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAClE,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getCountryCodeByDialCode = exports.getCountryDialCode = exports.getAllCountryCodes = exports.validatePhoneNumber = void 0;
|
|
7
|
+
const utils_1 = require("./utils");
|
|
8
|
+
const CountryCodes_json_1 = __importDefault(require("./CountryCodes.json"));
|
|
9
|
+
const COUNTRY_CODES = CountryCodes_json_1.default;
|
|
10
|
+
/**
|
|
11
|
+
* Validates phone numbers for any country
|
|
12
|
+
*
|
|
13
|
+
* @param input - Phone number in various formats (+91..., 0..., or plain digits)
|
|
14
|
+
* @param defaultCountry - ISO country code (e.g., "IN", "US") used when country cannot be detected
|
|
15
|
+
* @param mobileOnly - If true, only accepts mobile numbers (currently only for India)
|
|
16
|
+
* @returns PhoneValidationResult with validation status and parsed information
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* validatePhoneNumber("+91 98765 43210") // India with country code
|
|
20
|
+
* validatePhoneNumber("09876543210") // India with 0 prefix
|
|
21
|
+
* validatePhoneNumber("9876543210", "IN") // India plain digits
|
|
22
|
+
* validatePhoneNumber("+1 555 123 4567") // US number
|
|
23
|
+
*/
|
|
24
|
+
function validatePhoneNumber(input, defaultCountry = "IN", mobileOnly = false) {
|
|
25
|
+
if (!input || typeof input !== "string") {
|
|
26
|
+
return { isValid: false };
|
|
27
|
+
}
|
|
28
|
+
const cleaned = (0, utils_1.cleanPhoneNumber)(input);
|
|
29
|
+
if (cleaned.length < 7) {
|
|
30
|
+
return { isValid: false };
|
|
31
|
+
}
|
|
32
|
+
let countryCode = "";
|
|
33
|
+
let nationalNumber = "";
|
|
34
|
+
let detectedCountry = null;
|
|
35
|
+
// Handle international format with + prefix
|
|
36
|
+
if (cleaned.startsWith("+")) {
|
|
37
|
+
const detection = (0, utils_1.detectCountryFromPhoneNumber)(cleaned);
|
|
38
|
+
if (detection.country) {
|
|
39
|
+
detectedCountry = detection.country;
|
|
40
|
+
countryCode = detection.dialCode;
|
|
41
|
+
nationalNumber = detection.nationalNumber;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
return { isValid: false };
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
// Handle 0-prefixed numbers (common in many countries)
|
|
48
|
+
else if (cleaned.startsWith("0")) {
|
|
49
|
+
const defaultDialCode = (0, utils_1.getCountryDialCode)(defaultCountry);
|
|
50
|
+
if (!defaultDialCode) {
|
|
51
|
+
return { isValid: false };
|
|
52
|
+
}
|
|
53
|
+
countryCode = defaultDialCode;
|
|
54
|
+
nationalNumber = cleaned.substring(1);
|
|
55
|
+
detectedCountry = COUNTRY_CODES.find((c) => c.code.toUpperCase() === defaultCountry.toUpperCase()) || null;
|
|
56
|
+
}
|
|
57
|
+
// Handle plain digits (assume default country)
|
|
58
|
+
else {
|
|
59
|
+
const defaultDialCode = (0, utils_1.getCountryDialCode)(defaultCountry);
|
|
60
|
+
if (!defaultDialCode) {
|
|
61
|
+
return { isValid: false };
|
|
62
|
+
}
|
|
63
|
+
countryCode = defaultDialCode;
|
|
64
|
+
nationalNumber = cleaned;
|
|
65
|
+
detectedCountry = COUNTRY_CODES.find((c) => c.code.toUpperCase() === defaultCountry.toUpperCase()) || null;
|
|
66
|
+
}
|
|
67
|
+
// Validate national number length (general rule: 4-15 digits)
|
|
68
|
+
if (nationalNumber.length < 4 || nationalNumber.length > 15) {
|
|
69
|
+
return { isValid: false };
|
|
70
|
+
}
|
|
71
|
+
// Country-specific validation
|
|
72
|
+
if (countryCode === "91") {
|
|
73
|
+
// India: must be exactly 10 digits (mobile: 6-9, landline: 0-5)
|
|
74
|
+
if (!/^\d{10}$/.test(nationalNumber)) {
|
|
75
|
+
return { isValid: false };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
else if (countryCode === "1") {
|
|
79
|
+
// US/Canada: 10 digits (without country code)
|
|
80
|
+
if (!/^\d{10}$/.test(nationalNumber)) {
|
|
81
|
+
return { isValid: false };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
// General validation: 4-15 digits
|
|
86
|
+
if (!/^\d{4,15}$/.test(nationalNumber)) {
|
|
87
|
+
return { isValid: false };
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// Mobile/Fixed line detection (India)
|
|
91
|
+
let isMobile;
|
|
92
|
+
let isFixedLine;
|
|
93
|
+
if (countryCode === "91" && nationalNumber.length === 10) {
|
|
94
|
+
const firstDigit = nationalNumber[0];
|
|
95
|
+
// Indian mobile numbers start with 6, 7, 8, or 9
|
|
96
|
+
if ("6789".includes(firstDigit)) {
|
|
97
|
+
isMobile = true;
|
|
98
|
+
isFixedLine = false;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
// Landline numbers typically start with 0-5
|
|
102
|
+
isMobile = false;
|
|
103
|
+
isFixedLine = true;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// If mobileOnly is true, fail non-mobile numbers
|
|
107
|
+
if (mobileOnly && countryCode === "91" && !isMobile) {
|
|
108
|
+
return { isValid: false };
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
isValid: true,
|
|
112
|
+
countryCode,
|
|
113
|
+
nationalNumber,
|
|
114
|
+
e164: `+${countryCode}${nationalNumber}`,
|
|
115
|
+
isMobile,
|
|
116
|
+
isFixedLine,
|
|
117
|
+
country: detectedCountry?.code,
|
|
118
|
+
countryName: detectedCountry?.name,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
exports.validatePhoneNumber = validatePhoneNumber;
|
|
122
|
+
var utils_2 = require("./utils");
|
|
123
|
+
Object.defineProperty(exports, "getAllCountryCodes", { enumerable: true, get: function () { return utils_2.getAllCountryCodes; } });
|
|
124
|
+
Object.defineProperty(exports, "getCountryDialCode", { enumerable: true, get: function () { return utils_2.getCountryDialCode; } });
|
|
125
|
+
Object.defineProperty(exports, "getCountryCodeByDialCode", { enumerable: true, get: function () { return utils_2.getCountryCodeByDialCode; } });
|
|
126
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AACA,mCAKiB;AACjB,4EAA+C;AAE/C,MAAM,aAAa,GAAG,2BAA6B,CAAC;AAEpD;;;;;;;;;;;;;GAaG;AACH,SAAgB,mBAAmB,CACjC,KAAa,EACb,iBAAyB,IAAI,EAC7B,aAAsB,KAAK;IAE3B,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACxC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;IAC5B,CAAC;IAED,MAAM,OAAO,GAAG,IAAA,wBAAgB,EAAC,KAAK,CAAC,CAAC;IAExC,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACvB,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;IAC5B,CAAC;IAED,IAAI,WAAW,GAAG,EAAE,CAAC;IACrB,IAAI,cAAc,GAAG,EAAE,CAAC;IACxB,IAAI,eAAe,GAAuB,IAA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AG,KAAK,CAAC;YACjB,WAAW,GAAG,IAAI,CAAC;QACrB,CAAC;IACH,CAAC;IAED,iDAAiD;IACjD,IAAI,UAAU,IAAI,WAAW,KAAK,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;QACpD,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;IAC5B,CAAC;IAED,OAAO;QACL,OAAO,EAAE,IAAI;QACb,WAAW;QACX,cAAc;QACd,IAAI,EAAE,IAAI,WAAW,GAAG,cAAc,EAAE;QACxC,QAAQ;QACR,WAAW;QACX,OAAO,EAAE,eAAe,EAAE,IAAI;QAC9B,WAAW,EAAE,eAAe,EAAE,IAAI;KACnC,CAAC;AACJ,CAAC;AA9GD,kDA8GC;AAID,iCAA2F;AAAlF,2GAAA,kBAAkB,OAAA;AAAE,2GAAA,kBAAkB,OAAA;AAAE,iHAAA,wBAAwB,OAAA"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface PhoneValidationResult {
|
|
2
|
+
isValid: boolean;
|
|
3
|
+
countryCode?: string;
|
|
4
|
+
nationalNumber?: string;
|
|
5
|
+
e164?: string;
|
|
6
|
+
isMobile?: boolean;
|
|
7
|
+
isFixedLine?: boolean;
|
|
8
|
+
country?: string;
|
|
9
|
+
countryName?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface CountryCode {
|
|
12
|
+
name: string;
|
|
13
|
+
dial_code: string;
|
|
14
|
+
code: string;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,qBAAqB;IACpC,OAAO,EAAE,OAAO,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;CACd"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { CountryCode } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Get country dial code by country code (e.g., "IN" -> "91")
|
|
4
|
+
*/
|
|
5
|
+
export declare function getCountryDialCode(countryCode: string): string | null;
|
|
6
|
+
/**
|
|
7
|
+
* Get country code by dial code (e.g., "91" -> CountryCode for India)
|
|
8
|
+
*/
|
|
9
|
+
export declare function getCountryCodeByDialCode(dialCode: string): CountryCode | null;
|
|
10
|
+
/**
|
|
11
|
+
* Find country code from phone number by matching dial codes
|
|
12
|
+
* Returns the longest matching dial code
|
|
13
|
+
*/
|
|
14
|
+
export declare function detectCountryFromPhoneNumber(phoneNumber: string): {
|
|
15
|
+
country: CountryCode | null;
|
|
16
|
+
dialCode: string;
|
|
17
|
+
nationalNumber: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Clean phone number by removing spaces, dashes, parentheses, etc.
|
|
21
|
+
*/
|
|
22
|
+
export declare function cleanPhoneNumber(input: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* Get all country codes
|
|
25
|
+
*/
|
|
26
|
+
export declare function getAllCountryCodes(): CountryCode[];
|
|
27
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAKtC;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAKrE;AAED;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI,CAO7E;AAED;;;GAGG;AACH,wBAAgB,4BAA4B,CAAC,WAAW,EAAE,MAAM,GAAG;IACjE,OAAO,EAAE,WAAW,GAAG,IAAI,CAAC;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;CACxB,CA0BA;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEtD;AAED;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,WAAW,EAAE,CAElD"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getAllCountryCodes = exports.cleanPhoneNumber = exports.detectCountryFromPhoneNumber = exports.getCountryCodeByDialCode = exports.getCountryDialCode = void 0;
|
|
7
|
+
const CountryCodes_json_1 = __importDefault(require("./CountryCodes.json"));
|
|
8
|
+
const COUNTRY_CODES = CountryCodes_json_1.default;
|
|
9
|
+
/**
|
|
10
|
+
* Get country dial code by country code (e.g., "IN" -> "91")
|
|
11
|
+
*/
|
|
12
|
+
function getCountryDialCode(countryCode) {
|
|
13
|
+
const country = COUNTRY_CODES.find((c) => c.code.toUpperCase() === countryCode.toUpperCase());
|
|
14
|
+
return country ? country.dial_code.replace("+", "") : null;
|
|
15
|
+
}
|
|
16
|
+
exports.getCountryDialCode = getCountryDialCode;
|
|
17
|
+
/**
|
|
18
|
+
* Get country code by dial code (e.g., "91" -> CountryCode for India)
|
|
19
|
+
*/
|
|
20
|
+
function getCountryCodeByDialCode(dialCode) {
|
|
21
|
+
const normalizedDialCode = dialCode.replace("+", "");
|
|
22
|
+
return (COUNTRY_CODES.find((c) => c.dial_code.replace("+", "") === normalizedDialCode) || null);
|
|
23
|
+
}
|
|
24
|
+
exports.getCountryCodeByDialCode = getCountryCodeByDialCode;
|
|
25
|
+
/**
|
|
26
|
+
* Find country code from phone number by matching dial codes
|
|
27
|
+
* Returns the longest matching dial code
|
|
28
|
+
*/
|
|
29
|
+
function detectCountryFromPhoneNumber(phoneNumber) {
|
|
30
|
+
const cleaned = phoneNumber.replace(/[^0-9+]/g, "");
|
|
31
|
+
if (!cleaned.startsWith("+")) {
|
|
32
|
+
return { country: null, dialCode: "", nationalNumber: cleaned };
|
|
33
|
+
}
|
|
34
|
+
// Try to match dial codes from longest to shortest (1-4 digits)
|
|
35
|
+
for (let len = 4; len >= 1; len--) {
|
|
36
|
+
const potentialDialCode = cleaned.substring(1, 1 + len);
|
|
37
|
+
const country = getCountryCodeByDialCode(potentialDialCode);
|
|
38
|
+
if (country) {
|
|
39
|
+
const nationalNumber = cleaned.substring(1 + len);
|
|
40
|
+
// Basic sanity check: national number should be at least 4 digits
|
|
41
|
+
if (nationalNumber.length >= 4) {
|
|
42
|
+
return {
|
|
43
|
+
country,
|
|
44
|
+
dialCode: country.dial_code.replace("+", ""),
|
|
45
|
+
nationalNumber,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return { country: null, dialCode: "", nationalNumber: cleaned.substring(1) };
|
|
51
|
+
}
|
|
52
|
+
exports.detectCountryFromPhoneNumber = detectCountryFromPhoneNumber;
|
|
53
|
+
/**
|
|
54
|
+
* Clean phone number by removing spaces, dashes, parentheses, etc.
|
|
55
|
+
*/
|
|
56
|
+
function cleanPhoneNumber(input) {
|
|
57
|
+
return input.replace(/\s+/g, "").replace(/[^0-9+]/g, "");
|
|
58
|
+
}
|
|
59
|
+
exports.cleanPhoneNumber = cleanPhoneNumber;
|
|
60
|
+
/**
|
|
61
|
+
* Get all country codes
|
|
62
|
+
*/
|
|
63
|
+
function getAllCountryCodes() {
|
|
64
|
+
return COUNTRY_CODES;
|
|
65
|
+
}
|
|
66
|
+
exports.getAllCountryCodes = getAllCountryCodes;
|
|
67
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;;;AACA,4EAA+C;AAE/C,MAAM,aAAa,GAAG,2BAA6B,CAAC;AAEpD;;GAEG;AACH,SAAgB,kBAAkB,CAAC,WAAmB;IACpD,MAAM,OAAO,GAAG,aAAa,CAAC,IAAI,CAChC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,EAAE,KAAK,WAAW,CAAC,WAAW,EAAE,CAC1D,CAAC;IACF,OAAO,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AAC7D,CAAC;AALD,gDAKC;AAED;;GAEG;AACH,SAAgB,wBAAwB,CAAC,QAAgB;IACvD,MAAM,kBAAkB,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;IACrD,OAAO,CACL,aAAa,CAAC,IAAI,CAChB,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,KAAK,kBAAkB,CAC3D,IAAI,IAAI,CACV,CAAC;AACJ,CAAC;AAPD,4DAOC;AAED;;;GAGG;AACH,SAAgB,4BAA4B,CAAC,WAAmB;IAK9D,MAAM,OAAO,GAAG,WAAW,CAA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gDAEC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "global-phone-validator",
|
|
3
|
+
"version": "1.0.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.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc",
|
|
19
|
+
"prepare": "npm run build",
|
|
20
|
+
"test": "ts-node test/validator.test.ts",
|
|
21
|
+
"prepublishOnly": "npm run build"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"phone",
|
|
25
|
+
"validator",
|
|
26
|
+
"validation",
|
|
27
|
+
"mobile",
|
|
28
|
+
"landline",
|
|
29
|
+
"international",
|
|
30
|
+
"E164",
|
|
31
|
+
"E.164",
|
|
32
|
+
"country-code",
|
|
33
|
+
"india",
|
|
34
|
+
"worldwide",
|
|
35
|
+
"typescript"
|
|
36
|
+
],
|
|
37
|
+
"author": "Your Name",
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "https://github.com/yourusername/phone-validator.git"
|
|
42
|
+
},
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=12.0.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"typescript": "^5.1.0",
|
|
48
|
+
"ts-node": "^10.9.1"
|
|
49
|
+
}
|
|
50
|
+
}
|