@devdataphone/sdk 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/index.d.ts +249 -0
- package/index.js +175 -0
- package/index.mjs +31 -0
- package/package.json +57 -0
- package/src/countries.js +240 -0
- package/src/formatter.js +299 -0
- package/src/generator.js +334 -0
- package/src/validator.js +364 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DevDataPhone SDK TypeScript Definitions
|
|
3
|
+
* @module @devdataphone/sdk
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface Country {
|
|
7
|
+
code: string;
|
|
8
|
+
name: string;
|
|
9
|
+
dialCode: string;
|
|
10
|
+
flag: string;
|
|
11
|
+
example: string;
|
|
12
|
+
regex: RegExp;
|
|
13
|
+
reservedRanges: Array<{
|
|
14
|
+
areaCode?: string;
|
|
15
|
+
exchange?: string;
|
|
16
|
+
to?: string;
|
|
17
|
+
prefix?: string;
|
|
18
|
+
start?: string;
|
|
19
|
+
end?: string;
|
|
20
|
+
}>;
|
|
21
|
+
numberLength: number | { min: number; max: number };
|
|
22
|
+
mobilePrefix?: string | null;
|
|
23
|
+
mobilePrefixes?: string[];
|
|
24
|
+
mobileStartDigits?: string[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface AreaCode {
|
|
28
|
+
code: number;
|
|
29
|
+
state: string;
|
|
30
|
+
city?: string;
|
|
31
|
+
province?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface GenerateOptions {
|
|
35
|
+
/** Country code (US, UK, CN, IN, AU, CA, DE, FR, JP, BR) */
|
|
36
|
+
region?: string;
|
|
37
|
+
/** Number of phone numbers to generate (1-1000) */
|
|
38
|
+
count?: number;
|
|
39
|
+
/** Output format ('e164', 'national', 'international', 'digits') */
|
|
40
|
+
format?: 'e164' | 'national' | 'international' | 'digits';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface GeneratedNumber {
|
|
44
|
+
/** Formatted phone number */
|
|
45
|
+
number: string;
|
|
46
|
+
/** E.164 formatted number */
|
|
47
|
+
e164: string;
|
|
48
|
+
/** National format */
|
|
49
|
+
national: string;
|
|
50
|
+
/** Country name */
|
|
51
|
+
country: string;
|
|
52
|
+
/** ISO country code */
|
|
53
|
+
countryCode: string;
|
|
54
|
+
/** International dial code */
|
|
55
|
+
dialCode: string;
|
|
56
|
+
/** Location (city/state or country) */
|
|
57
|
+
location: string;
|
|
58
|
+
/** Raw digits */
|
|
59
|
+
raw: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface ValidateOptions {
|
|
63
|
+
/** Default country code for local numbers */
|
|
64
|
+
defaultCountry?: string;
|
|
65
|
+
/** Enable strict validation */
|
|
66
|
+
strict?: boolean;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface ValidationResult {
|
|
70
|
+
/** Whether the number is valid */
|
|
71
|
+
valid: boolean;
|
|
72
|
+
/** E.164 formatted number */
|
|
73
|
+
e164: string | null;
|
|
74
|
+
/** Country name */
|
|
75
|
+
country: string | null;
|
|
76
|
+
/** ISO country code */
|
|
77
|
+
countryCode: string | null;
|
|
78
|
+
/** International dial code */
|
|
79
|
+
dialCode: string | null;
|
|
80
|
+
/** National significant number */
|
|
81
|
+
nationalNumber: string | null;
|
|
82
|
+
/** Number type (MOBILE, FIXED_LINE, UNKNOWN) */
|
|
83
|
+
type: 'MOBILE' | 'FIXED_LINE' | 'UNKNOWN';
|
|
84
|
+
/** Error message if invalid */
|
|
85
|
+
error: string | null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface FormatOptions {
|
|
89
|
+
/** Default country code for local numbers */
|
|
90
|
+
defaultCountry?: string;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export type FormatStyleType = 'e164' | 'international' | 'national' | 'rfc3966' | 'digits';
|
|
94
|
+
|
|
95
|
+
export const FormatStyle: {
|
|
96
|
+
E164: 'e164';
|
|
97
|
+
INTERNATIONAL: 'international';
|
|
98
|
+
NATIONAL: 'national';
|
|
99
|
+
RFC3966: 'rfc3966';
|
|
100
|
+
DIGITS: 'digits';
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Generate random phone numbers
|
|
105
|
+
* @param options - Generation options
|
|
106
|
+
* @returns Array of generated phone numbers
|
|
107
|
+
* @throws Error if region is not supported or count is invalid
|
|
108
|
+
* @example
|
|
109
|
+
* const numbers = generate({ region: 'US', count: 10 });
|
|
110
|
+
*/
|
|
111
|
+
export function generate(options?: GenerateOptions): GeneratedNumber[];
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Generate a single phone number
|
|
115
|
+
* @param region - Country code (default: 'US')
|
|
116
|
+
* @param format - Output format (default: 'e164')
|
|
117
|
+
* @returns Single phone number string
|
|
118
|
+
* @example
|
|
119
|
+
* const number = generateOne('CN', 'national');
|
|
120
|
+
*/
|
|
121
|
+
export function generateOne(region?: string, format?: string): string;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Validate a phone number
|
|
125
|
+
* @param input - Phone number to validate
|
|
126
|
+
* @param options - Validation options
|
|
127
|
+
* @returns Validation result
|
|
128
|
+
* @example
|
|
129
|
+
* const result = validate('+8613912345678');
|
|
130
|
+
* console.log(result.country); // 'China'
|
|
131
|
+
*/
|
|
132
|
+
export function validate(input: string, options?: ValidateOptions): ValidationResult;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Quick validation check
|
|
136
|
+
* @param input - Phone number to validate
|
|
137
|
+
* @param defaultCountry - Default country code
|
|
138
|
+
* @returns true if valid
|
|
139
|
+
*/
|
|
140
|
+
export function isValid(input: string, defaultCountry?: string): boolean;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Check if input is valid E.164 format
|
|
144
|
+
* @param input - Phone number
|
|
145
|
+
* @returns true if valid E.164
|
|
146
|
+
*/
|
|
147
|
+
export function isE164(input: string): boolean;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Format a phone number
|
|
151
|
+
* @param input - Phone number
|
|
152
|
+
* @param style - Output format style
|
|
153
|
+
* @param options - Additional options
|
|
154
|
+
* @returns Formatted number or null if invalid
|
|
155
|
+
* @example
|
|
156
|
+
* format('+14155550100', 'national'); // '(415) 555-0100'
|
|
157
|
+
*/
|
|
158
|
+
export function format(
|
|
159
|
+
input: string,
|
|
160
|
+
style?: FormatStyleType,
|
|
161
|
+
options?: FormatOptions
|
|
162
|
+
): string | null;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Convert any phone number format to E.164
|
|
166
|
+
* @param input - Phone number
|
|
167
|
+
* @param defaultCountry - Default country code for local numbers
|
|
168
|
+
* @returns E.164 formatted number or null
|
|
169
|
+
*/
|
|
170
|
+
export function toE164(input: string, defaultCountry?: string): string | null;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Convert to national format
|
|
174
|
+
* @param input - Phone number
|
|
175
|
+
* @param defaultCountry - Default country code
|
|
176
|
+
* @returns National formatted number or null
|
|
177
|
+
*/
|
|
178
|
+
export function toNational(input: string, defaultCountry?: string): string | null;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Convert to international format
|
|
182
|
+
* @param input - Phone number
|
|
183
|
+
* @param defaultCountry - Default country code
|
|
184
|
+
* @returns International formatted number or null
|
|
185
|
+
*/
|
|
186
|
+
export function toInternational(input: string, defaultCountry?: string): string | null;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Remove all non-digit characters except leading +
|
|
190
|
+
* @param input - Phone number
|
|
191
|
+
* @returns Sanitized number
|
|
192
|
+
*/
|
|
193
|
+
export function sanitize(input: string): string;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Get country by code
|
|
197
|
+
* @param code - ISO 3166-1 alpha-2 country code
|
|
198
|
+
* @returns Country data or undefined
|
|
199
|
+
*/
|
|
200
|
+
export function getCountry(code: string): Country | undefined;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Get all supported country codes
|
|
204
|
+
* @returns Array of country codes
|
|
205
|
+
*/
|
|
206
|
+
export function getSupportedCountries(): string[];
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Check if a country code is supported
|
|
210
|
+
* @param code - Country code
|
|
211
|
+
* @returns true if supported
|
|
212
|
+
*/
|
|
213
|
+
export function isSupported(code: string): boolean;
|
|
214
|
+
|
|
215
|
+
/** Supported countries data */
|
|
216
|
+
export const COUNTRIES: Country[];
|
|
217
|
+
|
|
218
|
+
/** US Area codes data */
|
|
219
|
+
export const US_AREA_CODES: AreaCode[];
|
|
220
|
+
|
|
221
|
+
/** Canada Area codes data */
|
|
222
|
+
export const CA_AREA_CODES: AreaCode[];
|
|
223
|
+
|
|
224
|
+
/** SDK Version */
|
|
225
|
+
export const VERSION: string;
|
|
226
|
+
|
|
227
|
+
// Default export
|
|
228
|
+
declare const _default: {
|
|
229
|
+
generate: typeof generate;
|
|
230
|
+
generateOne: typeof generateOne;
|
|
231
|
+
validate: typeof validate;
|
|
232
|
+
isValid: typeof isValid;
|
|
233
|
+
isE164: typeof isE164;
|
|
234
|
+
format: typeof format;
|
|
235
|
+
toE164: typeof toE164;
|
|
236
|
+
toNational: typeof toNational;
|
|
237
|
+
toInternational: typeof toInternational;
|
|
238
|
+
FormatStyle: typeof FormatStyle;
|
|
239
|
+
sanitize: typeof sanitize;
|
|
240
|
+
COUNTRIES: typeof COUNTRIES;
|
|
241
|
+
US_AREA_CODES: typeof US_AREA_CODES;
|
|
242
|
+
CA_AREA_CODES: typeof CA_AREA_CODES;
|
|
243
|
+
getCountry: typeof getCountry;
|
|
244
|
+
getSupportedCountries: typeof getSupportedCountries;
|
|
245
|
+
isSupported: typeof isSupported;
|
|
246
|
+
VERSION: typeof VERSION;
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
export default _default;
|
package/index.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DevDataPhone SDK
|
|
3
|
+
* Generate valid E.164 phone numbers for QA testing, database seeding, and mock data.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* const { generate, validate, format } = require('@devdataphone/sdk');
|
|
7
|
+
*
|
|
8
|
+
* // Generate 10 US phone numbers
|
|
9
|
+
* const numbers = generate({ region: 'US', count: 10 });
|
|
10
|
+
*
|
|
11
|
+
* // Validate a phone number
|
|
12
|
+
* const result = validate('+14155550100');
|
|
13
|
+
*
|
|
14
|
+
* // Format a phone number
|
|
15
|
+
* const formatted = format('+14155550100', 'national');
|
|
16
|
+
*
|
|
17
|
+
* @module @devdataphone/sdk
|
|
18
|
+
* @version 1.0.0
|
|
19
|
+
* @license MIT
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
'use strict';
|
|
23
|
+
|
|
24
|
+
const { generate, generateOne } = require('./src/generator');
|
|
25
|
+
const { validate, isValid, isE164, sanitize } = require('./src/validator');
|
|
26
|
+
const { format, toE164, toNational, toInternational, FormatStyle } = require('./src/formatter');
|
|
27
|
+
const { COUNTRIES, US_AREA_CODES, CA_AREA_CODES, getCountry, getSupportedCountries, isSupported } = require('./src/countries');
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* SDK Version
|
|
31
|
+
* @type {string}
|
|
32
|
+
*/
|
|
33
|
+
const VERSION = '1.0.0';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Generate random phone numbers
|
|
37
|
+
* @function generate
|
|
38
|
+
* @param {Object} options - Generation options
|
|
39
|
+
* @param {string} [options.region='US'] - Country code (US, UK, CN, IN, AU, CA, DE, FR, JP, BR)
|
|
40
|
+
* @param {number} [options.count=1] - Number of phone numbers to generate (1-1000)
|
|
41
|
+
* @param {string} [options.format='e164'] - Output format ('e164', 'national', 'international', 'digits')
|
|
42
|
+
* @returns {Array<Object>} Array of generated phone numbers
|
|
43
|
+
* @throws {Error} If region is not supported or count is invalid
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* // Generate 5 UK phone numbers
|
|
47
|
+
* const numbers = generate({ region: 'UK', count: 5 });
|
|
48
|
+
* console.log(numbers[0].number); // +447700900123
|
|
49
|
+
* console.log(numbers[0].national); // 07700 900123
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Generate a single phone number
|
|
54
|
+
* @function generateOne
|
|
55
|
+
* @param {string} [region='US'] - Country code
|
|
56
|
+
* @param {string} [format='e164'] - Output format
|
|
57
|
+
* @returns {string} Single phone number string
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* const number = generateOne('CN', 'national');
|
|
61
|
+
* console.log(number); // 139 1234 5678
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Validate a phone number
|
|
66
|
+
* @function validate
|
|
67
|
+
* @param {string} input - Phone number to validate
|
|
68
|
+
* @param {Object} [options] - Validation options
|
|
69
|
+
* @param {string} [options.defaultCountry] - Default country code for local numbers
|
|
70
|
+
* @param {boolean} [options.strict=false] - Enable strict validation
|
|
71
|
+
* @returns {Object} Validation result
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* const result = validate('+8613912345678');
|
|
75
|
+
* console.log(result.valid); // true
|
|
76
|
+
* console.log(result.country); // China
|
|
77
|
+
* console.log(result.countryCode); // CN
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Quick validation check
|
|
82
|
+
* @function isValid
|
|
83
|
+
* @param {string} input - Phone number to validate
|
|
84
|
+
* @param {string} [defaultCountry] - Default country code
|
|
85
|
+
* @returns {boolean}
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* console.log(isValid('+14155550100')); // true
|
|
89
|
+
* console.log(isValid('555-0100', 'US')); // true
|
|
90
|
+
*/
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Check if input is valid E.164 format
|
|
94
|
+
* @function isE164
|
|
95
|
+
* @param {string} input - Phone number
|
|
96
|
+
* @returns {boolean}
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* console.log(isE164('+14155550100')); // true
|
|
100
|
+
* console.log(isE164('(415) 555-0100')); // false
|
|
101
|
+
*/
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Format a phone number
|
|
105
|
+
* @function format
|
|
106
|
+
* @param {string} input - Phone number
|
|
107
|
+
* @param {string} [style='e164'] - Output format ('e164', 'international', 'national', 'rfc3966', 'digits')
|
|
108
|
+
* @param {Object} [options] - Additional options
|
|
109
|
+
* @returns {string|null} Formatted number or null if invalid
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* format('+14155550100', 'national'); // (415) 555-0100
|
|
113
|
+
* format('+14155550100', 'international'); // +1 415 555 0100
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Supported countries data
|
|
118
|
+
* @constant {Array<Object>} COUNTRIES
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* US Area codes data
|
|
123
|
+
* @constant {Array<Object>} US_AREA_CODES
|
|
124
|
+
*/
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Get country by code
|
|
128
|
+
* @function getCountry
|
|
129
|
+
* @param {string} code - ISO 3166-1 alpha-2 country code
|
|
130
|
+
* @returns {Object|undefined}
|
|
131
|
+
*/
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Get all supported country codes
|
|
135
|
+
* @function getSupportedCountries
|
|
136
|
+
* @returns {string[]}
|
|
137
|
+
*/
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Check if country is supported
|
|
141
|
+
* @function isSupported
|
|
142
|
+
* @param {string} code - Country code
|
|
143
|
+
* @returns {boolean}
|
|
144
|
+
*/
|
|
145
|
+
|
|
146
|
+
// Export all functions and constants
|
|
147
|
+
module.exports = {
|
|
148
|
+
// Core functions
|
|
149
|
+
generate,
|
|
150
|
+
generateOne,
|
|
151
|
+
validate,
|
|
152
|
+
isValid,
|
|
153
|
+
isE164,
|
|
154
|
+
format,
|
|
155
|
+
|
|
156
|
+
// Formatter helpers
|
|
157
|
+
toE164,
|
|
158
|
+
toNational,
|
|
159
|
+
toInternational,
|
|
160
|
+
FormatStyle,
|
|
161
|
+
|
|
162
|
+
// Utilities
|
|
163
|
+
sanitize,
|
|
164
|
+
|
|
165
|
+
// Country data
|
|
166
|
+
COUNTRIES,
|
|
167
|
+
US_AREA_CODES,
|
|
168
|
+
CA_AREA_CODES,
|
|
169
|
+
getCountry,
|
|
170
|
+
getSupportedCountries,
|
|
171
|
+
isSupported,
|
|
172
|
+
|
|
173
|
+
// Meta
|
|
174
|
+
VERSION
|
|
175
|
+
};
|
package/index.mjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DevDataPhone SDK - ESM Module Entry
|
|
3
|
+
* @module @devdataphone/sdk
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Re-export from CommonJS module
|
|
7
|
+
import pkg from './index.js';
|
|
8
|
+
|
|
9
|
+
export const {
|
|
10
|
+
generate,
|
|
11
|
+
generateOne,
|
|
12
|
+
validate,
|
|
13
|
+
isValid,
|
|
14
|
+
isE164,
|
|
15
|
+
format,
|
|
16
|
+
toE164,
|
|
17
|
+
toNational,
|
|
18
|
+
toInternational,
|
|
19
|
+
FormatStyle,
|
|
20
|
+
sanitize,
|
|
21
|
+
COUNTRIES,
|
|
22
|
+
US_AREA_CODES,
|
|
23
|
+
CA_AREA_CODES,
|
|
24
|
+
getCountry,
|
|
25
|
+
getSupportedCountries,
|
|
26
|
+
isSupported,
|
|
27
|
+
VERSION
|
|
28
|
+
} = pkg;
|
|
29
|
+
|
|
30
|
+
// Default export
|
|
31
|
+
export default pkg;
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@devdataphone/sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Generate valid E.164 phone numbers for QA testing, database seeding, and mock data. Zero server-side retention.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"module": "index.mjs",
|
|
7
|
+
"types": "index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"require": "./index.js",
|
|
11
|
+
"import": "./index.mjs",
|
|
12
|
+
"types": "./index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.js",
|
|
17
|
+
"index.mjs",
|
|
18
|
+
"index.d.ts",
|
|
19
|
+
"src/"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"test": "node test.js"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"phone",
|
|
26
|
+
"phone-number",
|
|
27
|
+
"e164",
|
|
28
|
+
"mock-data",
|
|
29
|
+
"faker",
|
|
30
|
+
"generator",
|
|
31
|
+
"validation",
|
|
32
|
+
"testing",
|
|
33
|
+
"qa",
|
|
34
|
+
"libphonenumber"
|
|
35
|
+
],
|
|
36
|
+
"author": "DevDataPhone",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "https://github.com/user/devdataphone"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://devdataphone.com",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/user/devdataphone/issues"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=14.0.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"libphonenumber-js": "^1.10.0"
|
|
51
|
+
},
|
|
52
|
+
"peerDependenciesMeta": {
|
|
53
|
+
"libphonenumber-js": {
|
|
54
|
+
"optional": true
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|