country-codes-library 1.1.2 → 2.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 +343 -60
- package/dist/countryData.d.ts +13 -0
- package/dist/countryData.js +39 -0
- package/dist/countryNames.d.ts +5 -0
- package/dist/countryNames.js +252 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +33 -1
- package/dist/types.d.ts +62 -0
- package/dist/types.js +2 -0
- package/dist/utils.d.ts +123 -0
- package/dist/utils.js +241 -0
- package/dist/validators.d.ts +94 -0
- package/dist/validators.js +225 -0
- package/package.json +31 -25
package/README.md
CHANGED
|
@@ -1,60 +1,343 @@
|
|
|
1
|
-
# Country Codes Library
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
console.log(
|
|
41
|
-
console.log(
|
|
42
|
-
console.log(
|
|
43
|
-
console.log(
|
|
44
|
-
console.log(
|
|
45
|
-
console.log(
|
|
46
|
-
console.log(
|
|
47
|
-
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
1
|
+
# Country Codes Library
|
|
2
|
+
|
|
3
|
+
A comprehensive TypeScript/JavaScript library providing country codes, calling codes, currencies, and administrative divisions with powerful utility functions for lookups, validation, and data manipulation.
|
|
4
|
+
|
|
5
|
+
## 🚀 Features
|
|
6
|
+
|
|
7
|
+
- ✅ **ISO 3166-1** two-letter and three-letter country codes
|
|
8
|
+
- ✅ **International calling codes** for all countries
|
|
9
|
+
- ✅ **Currency codes (ISO 4217)** and symbols
|
|
10
|
+
- ✅ **Administrative divisions** for USA, Canada, and China
|
|
11
|
+
- ✅ **Unified country data** - all information in one place
|
|
12
|
+
- ✅ **Utility functions** - reverse lookups, search, and conversion
|
|
13
|
+
- ✅ **Validation functions** - validate codes, currencies, phone numbers
|
|
14
|
+
- ✅ **TypeScript support** - full type definitions included
|
|
15
|
+
- ✅ **Tree-shakeable** - import only what you need
|
|
16
|
+
- ✅ **Zero dependencies** - lightweight and fast
|
|
17
|
+
|
|
18
|
+
## 📦 Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install country-codes-library
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## 📖 Usage
|
|
25
|
+
|
|
26
|
+
### Basic Usage (Original API - Still Supported)
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
const {
|
|
30
|
+
TwoLetterISORegionCode,
|
|
31
|
+
ThreeLetterISORegionCode,
|
|
32
|
+
CountryCallingCodes,
|
|
33
|
+
CountryCurrencyCodes,
|
|
34
|
+
CountryCurrencySymbols,
|
|
35
|
+
USAStateCode,
|
|
36
|
+
CanadaProvinceCode,
|
|
37
|
+
ChinaProvinceCode
|
|
38
|
+
} = require('country-codes-library');
|
|
39
|
+
|
|
40
|
+
console.log(TwoLetterISORegionCode.UnitedStates); // "US"
|
|
41
|
+
console.log(ThreeLetterISORegionCode.India); // "IND"
|
|
42
|
+
console.log(CountryCallingCodes.Germany); // "+49"
|
|
43
|
+
console.log(CountryCurrencyCodes.Japan); // "JPY"
|
|
44
|
+
console.log(CountryCurrencySymbols.Japan); // "¥"
|
|
45
|
+
console.log(USAStateCode.California); // "CA"
|
|
46
|
+
console.log(CanadaProvinceCode.Ontario); // "ON"
|
|
47
|
+
console.log(ChinaProvinceCode.Beijing); // "BJ"
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### ⭐ NEW: Unified Country Data
|
|
51
|
+
|
|
52
|
+
Access all country information in one object:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { CountryData, getAllCountries } from 'country-codes-library';
|
|
56
|
+
|
|
57
|
+
// Get specific country
|
|
58
|
+
const usa = CountryData.UnitedStates;
|
|
59
|
+
console.log(usa);
|
|
60
|
+
/*
|
|
61
|
+
{
|
|
62
|
+
name: "UnitedStates",
|
|
63
|
+
code2: "US",
|
|
64
|
+
code3: "USA",
|
|
65
|
+
callingCode: "+1",
|
|
66
|
+
currencyCode: "USD",
|
|
67
|
+
currencySymbol: "$"
|
|
68
|
+
}
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
// Get all countries as array
|
|
72
|
+
const allCountries = getAllCountries();
|
|
73
|
+
console.log(allCountries.length); // 249 countries
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### ⭐ NEW: Utility Functions
|
|
77
|
+
|
|
78
|
+
#### Reverse Lookups
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import {
|
|
82
|
+
getCountryByCode2,
|
|
83
|
+
getCountryByCode3,
|
|
84
|
+
getCountryByName,
|
|
85
|
+
getCountriesByCurrency,
|
|
86
|
+
getCountriesByCallingCode
|
|
87
|
+
} from 'country-codes-library';
|
|
88
|
+
|
|
89
|
+
// Find country by 2-letter code
|
|
90
|
+
const country = getCountryByCode2('US');
|
|
91
|
+
console.log(country.name); // "UnitedStates"
|
|
92
|
+
|
|
93
|
+
// Find country by 3-letter code
|
|
94
|
+
const country2 = getCountryByCode3('GBR');
|
|
95
|
+
console.log(country2.code2); // "GB"
|
|
96
|
+
|
|
97
|
+
// Find country by name (case-insensitive, handles spaces)
|
|
98
|
+
const country3 = getCountryByName('united states');
|
|
99
|
+
console.log(country3.code2); // "US"
|
|
100
|
+
|
|
101
|
+
// Find all countries using Euro
|
|
102
|
+
const euroCountries = getCountriesByCurrency('EUR');
|
|
103
|
+
console.log(euroCountries.length); // 35+ countries
|
|
104
|
+
|
|
105
|
+
// Find all countries with +1 calling code
|
|
106
|
+
const nanpaCountries = getCountriesByCallingCode('+1');
|
|
107
|
+
// Returns USA, Canada, and Caribbean countries
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
#### Search and Convert
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
import {
|
|
114
|
+
searchCountries,
|
|
115
|
+
convertCountryCode,
|
|
116
|
+
getCallingCode,
|
|
117
|
+
getCurrencyCode,
|
|
118
|
+
getCurrencySymbol
|
|
119
|
+
} from 'country-codes-library';
|
|
120
|
+
|
|
121
|
+
// Search countries by partial name
|
|
122
|
+
const results = searchCountries('united');
|
|
123
|
+
// Returns: UnitedStates, UnitedKingdom, UnitedArabEmirates
|
|
124
|
+
|
|
125
|
+
// Convert between code formats
|
|
126
|
+
convertCountryCode('US', 'code3'); // "USA"
|
|
127
|
+
convertCountryCode('GBR', 'code2'); // "GB"
|
|
128
|
+
|
|
129
|
+
// Get specific data by any identifier (name, code2, or code3)
|
|
130
|
+
getCallingCode('US'); // "+1"
|
|
131
|
+
getCallingCode('Germany'); // "+49"
|
|
132
|
+
getCurrencyCode('JP'); // "JPY"
|
|
133
|
+
getCurrencySymbol('UnitedKingdom'); // "£"
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
#### List Functions
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
import { getAllCurrencies, getAllCallingCodes } from 'country-codes-library';
|
|
140
|
+
|
|
141
|
+
// Get all unique currencies used worldwide
|
|
142
|
+
const currencies = getAllCurrencies();
|
|
143
|
+
// ["AED", "AFN", "ALL", ..., "ZMW", "ZWL"]
|
|
144
|
+
|
|
145
|
+
// Get all unique calling codes
|
|
146
|
+
const callingCodes = getAllCallingCodes();
|
|
147
|
+
// ["+1", "+7", "+20", ..., "+998"]
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### ⭐ NEW: Validation Functions
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
import {
|
|
154
|
+
isValidCountryCode2,
|
|
155
|
+
isValidCountryCode3,
|
|
156
|
+
isValidCountryCode,
|
|
157
|
+
isValidCountryName,
|
|
158
|
+
isValidCurrencyCode,
|
|
159
|
+
isValidCallingCode,
|
|
160
|
+
validatePhoneNumber,
|
|
161
|
+
parsePhoneNumber
|
|
162
|
+
} from 'country-codes-library';
|
|
163
|
+
|
|
164
|
+
// Validate country codes
|
|
165
|
+
isValidCountryCode2('US'); // true
|
|
166
|
+
isValidCountryCode2('XX'); // false
|
|
167
|
+
isValidCountryCode3('USA'); // true
|
|
168
|
+
isValidCountryCode('US'); // true (accepts 2 or 3 letter)
|
|
169
|
+
isValidCountryCode('USA'); // true
|
|
170
|
+
|
|
171
|
+
// Validate country names
|
|
172
|
+
isValidCountryName('Germany'); // true
|
|
173
|
+
isValidCountryName('united states'); // true (case-insensitive)
|
|
174
|
+
isValidCountryName('Atlantis'); // false
|
|
175
|
+
|
|
176
|
+
// Validate currency codes
|
|
177
|
+
isValidCurrencyCode('USD'); // true
|
|
178
|
+
isValidCurrencyCode('EUR'); // true
|
|
179
|
+
isValidCurrencyCode('XXX'); // false
|
|
180
|
+
|
|
181
|
+
// Validate calling codes
|
|
182
|
+
isValidCallingCode('+1'); // true
|
|
183
|
+
isValidCallingCode('44'); // true (+ is optional)
|
|
184
|
+
isValidCallingCode('+999'); // false
|
|
185
|
+
|
|
186
|
+
// Validate phone numbers
|
|
187
|
+
const result = validatePhoneNumber('(202) 555-1234', 'US');
|
|
188
|
+
console.log(result);
|
|
189
|
+
/*
|
|
190
|
+
{
|
|
191
|
+
isValid: true,
|
|
192
|
+
formatted: "+1-2025551234",
|
|
193
|
+
countryCode: "+1"
|
|
194
|
+
}
|
|
195
|
+
*/
|
|
196
|
+
|
|
197
|
+
// Parse international phone numbers
|
|
198
|
+
const parsed = parsePhoneNumber('+44 20 7946 0958');
|
|
199
|
+
console.log(parsed);
|
|
200
|
+
/*
|
|
201
|
+
{
|
|
202
|
+
isValid: true,
|
|
203
|
+
formatted: "+442079460958",
|
|
204
|
+
countryCode: "+44"
|
|
205
|
+
}
|
|
206
|
+
*/
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### TypeScript Support
|
|
210
|
+
|
|
211
|
+
Full TypeScript definitions with autocomplete:
|
|
212
|
+
|
|
213
|
+
```ts
|
|
214
|
+
import type {
|
|
215
|
+
Country,
|
|
216
|
+
CountryName,
|
|
217
|
+
CountryCode2,
|
|
218
|
+
CountryCode3,
|
|
219
|
+
SearchOptions,
|
|
220
|
+
PhoneValidationResult
|
|
221
|
+
} from 'country-codes-library';
|
|
222
|
+
|
|
223
|
+
// Strong typing for country data
|
|
224
|
+
const country: Country = {
|
|
225
|
+
name: 'UnitedStates',
|
|
226
|
+
code2: 'US',
|
|
227
|
+
code3: 'USA',
|
|
228
|
+
callingCode: '+1',
|
|
229
|
+
currencyCode: 'USD',
|
|
230
|
+
currencySymbol: '$'
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
// Use type-safe country names for autocomplete
|
|
234
|
+
const name: CountryName = 'Germany'; // Autocomplete available!
|
|
235
|
+
|
|
236
|
+
// Search with options
|
|
237
|
+
const options: SearchOptions = {
|
|
238
|
+
caseSensitive: false,
|
|
239
|
+
exactMatch: true
|
|
240
|
+
};
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## 📚 API Reference
|
|
244
|
+
|
|
245
|
+
### Constants (Original API)
|
|
246
|
+
|
|
247
|
+
- `TwoLetterISORegionCode` - ISO 3166-1 alpha-2 codes
|
|
248
|
+
- `ThreeLetterISORegionCode` - ISO 3166-1 alpha-3 codes
|
|
249
|
+
- `CountryCallingCodes` - International dialing codes
|
|
250
|
+
- `CountryCurrencyCodes` - ISO 4217 currency codes
|
|
251
|
+
- `CountryCurrencySymbols` - Currency symbols
|
|
252
|
+
- `USAStateCode` - US state 2-letter codes
|
|
253
|
+
- `CanadaProvinceCode` - Canadian province codes
|
|
254
|
+
- `ChinaProvinceCode` - Chinese province codes
|
|
255
|
+
|
|
256
|
+
### New Features
|
|
257
|
+
|
|
258
|
+
#### Data Objects
|
|
259
|
+
- `CountryData` - Unified country information object
|
|
260
|
+
- `getAllCountries()` - Returns array of all countries
|
|
261
|
+
- `getAllCountryNames()` - Returns array of country names
|
|
262
|
+
|
|
263
|
+
#### Lookup Functions
|
|
264
|
+
- `getCountryByCode2(code)` - Find by 2-letter code
|
|
265
|
+
- `getCountryByCode3(code)` - Find by 3-letter code
|
|
266
|
+
- `getCountryByName(name)` - Find by country name
|
|
267
|
+
- `getCountriesByCurrency(code)` - Find all countries using a currency
|
|
268
|
+
- `getCountriesByCallingCode(code)` - Find all countries with calling code
|
|
269
|
+
|
|
270
|
+
#### Utility Functions
|
|
271
|
+
- `searchCountries(query, options?)` - Search countries by name
|
|
272
|
+
- `convertCountryCode(code, format)` - Convert between code formats
|
|
273
|
+
- `getCallingCode(identifier)` - Get calling code for a country
|
|
274
|
+
- `getCurrencyCode(identifier)` - Get currency code for a country
|
|
275
|
+
- `getCurrencySymbol(identifier)` - Get currency symbol for a country
|
|
276
|
+
- `getAllCurrencies()` - Get all unique currencies
|
|
277
|
+
- `getAllCallingCodes()` - Get all unique calling codes
|
|
278
|
+
|
|
279
|
+
#### Validation Functions
|
|
280
|
+
- `isValidCountryCode2(code)` - Validate 2-letter code
|
|
281
|
+
- `isValidCountryCode3(code)` - Validate 3-letter code
|
|
282
|
+
- `isValidCountryCode(code)` - Validate any country code
|
|
283
|
+
- `isValidCountryName(name)` - Validate country name
|
|
284
|
+
- `isValidCurrencyCode(code)` - Validate currency code
|
|
285
|
+
- `isValidCallingCode(code)` - Validate calling code
|
|
286
|
+
- `validatePhoneNumber(number, country)` - Validate phone number
|
|
287
|
+
- `parsePhoneNumber(number)` - Parse international phone number
|
|
288
|
+
|
|
289
|
+
## 🎯 Use Cases
|
|
290
|
+
|
|
291
|
+
- **Form validation** - Validate country codes and phone numbers
|
|
292
|
+
- **Phone number formatting** - Format international phone numbers
|
|
293
|
+
- **Currency conversion apps** - Get currency info by country
|
|
294
|
+
- **E-commerce** - Display correct currency symbols
|
|
295
|
+
- **Travel apps** - Show calling codes for countries
|
|
296
|
+
- **Data normalization** - Convert between different code formats
|
|
297
|
+
- **Autocomplete** - Search countries for dropdowns
|
|
298
|
+
- **Data validation** - Ensure data quality in databases
|
|
299
|
+
|
|
300
|
+
## 🔄 Migration from v1.x
|
|
301
|
+
|
|
302
|
+
The library is **100% backwards compatible**. All original exports still work:
|
|
303
|
+
|
|
304
|
+
```js
|
|
305
|
+
// v1.x code still works
|
|
306
|
+
const { TwoLetterISORegionCode } = require('country-codes-library');
|
|
307
|
+
console.log(TwoLetterISORegionCode.UnitedStates); // Still works!
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
Simply add new features when you're ready:
|
|
311
|
+
|
|
312
|
+
```js
|
|
313
|
+
// Start using new features gradually
|
|
314
|
+
const { getCountryByCode2 } = require('country-codes-library');
|
|
315
|
+
const country = getCountryByCode2('US');
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
## 📊 Data Coverage
|
|
319
|
+
|
|
320
|
+
- **249 countries and territories**
|
|
321
|
+
- **170+ unique currencies**
|
|
322
|
+
- **240+ calling codes**
|
|
323
|
+
- **US States**: 51 (50 states + DC)
|
|
324
|
+
- **Canadian Provinces**: 13
|
|
325
|
+
- **Chinese Provinces**: 34
|
|
326
|
+
|
|
327
|
+
## 🤝 Contributing
|
|
328
|
+
|
|
329
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
330
|
+
|
|
331
|
+
## 📝 License
|
|
332
|
+
|
|
333
|
+
MIT
|
|
334
|
+
|
|
335
|
+
## 🔗 Links
|
|
336
|
+
|
|
337
|
+
- [GitHub Repository](https://github.com/mohansenthil/countrycodes-node)
|
|
338
|
+
- [npm Package](https://www.npmjs.com/package/country-codes-library)
|
|
339
|
+
- [Issues](https://github.com/mohansenthil/countrycodes-node/issues)
|
|
340
|
+
|
|
341
|
+
## ⭐ Show Your Support
|
|
342
|
+
|
|
343
|
+
If this library helps you, please give it a star on GitHub!
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Country } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Unified country data combining all available information
|
|
4
|
+
*/
|
|
5
|
+
export declare const CountryData: Record<string, Country>;
|
|
6
|
+
/**
|
|
7
|
+
* Get all countries as an array
|
|
8
|
+
*/
|
|
9
|
+
export declare function getAllCountries(): Country[];
|
|
10
|
+
/**
|
|
11
|
+
* Get all country names
|
|
12
|
+
*/
|
|
13
|
+
export declare function getAllCountryNames(): string[];
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CountryData = void 0;
|
|
4
|
+
exports.getAllCountries = getAllCountries;
|
|
5
|
+
exports.getAllCountryNames = getAllCountryNames;
|
|
6
|
+
const twoLetterISORegionCode_1 = require("./twoLetterISORegionCode");
|
|
7
|
+
const threeLetterISORegionCode_1 = require("./threeLetterISORegionCode");
|
|
8
|
+
const countryCallingCodes_1 = require("./countryCallingCodes");
|
|
9
|
+
const countryCurrencyCodes_1 = require("./countryCurrencyCodes");
|
|
10
|
+
const countryCurrencySymbols_1 = require("./countryCurrencySymbols");
|
|
11
|
+
const countryNames_1 = require("./countryNames");
|
|
12
|
+
/**
|
|
13
|
+
* Unified country data combining all available information
|
|
14
|
+
*/
|
|
15
|
+
exports.CountryData = {};
|
|
16
|
+
// Build the unified country data from existing constants
|
|
17
|
+
for (const countryName in twoLetterISORegionCode_1.TwoLetterISORegionCode) {
|
|
18
|
+
const name = countryName;
|
|
19
|
+
exports.CountryData[countryName] = {
|
|
20
|
+
name: countryNames_1.CountryNames[countryName] || countryName,
|
|
21
|
+
code2: twoLetterISORegionCode_1.TwoLetterISORegionCode[name] || '',
|
|
22
|
+
code3: threeLetterISORegionCode_1.ThreeLetterISORegionCode[name] || '',
|
|
23
|
+
callingCode: countryCallingCodes_1.CountryCallingCodes[name] || '',
|
|
24
|
+
currencyCode: countryCurrencyCodes_1.CountryCurrencyCodes[name] || '',
|
|
25
|
+
currencySymbol: countryCurrencySymbols_1.CountryCurrencySymbols[name] || '',
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Get all countries as an array
|
|
30
|
+
*/
|
|
31
|
+
function getAllCountries() {
|
|
32
|
+
return Object.values(exports.CountryData);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Get all country names
|
|
36
|
+
*/
|
|
37
|
+
function getAllCountryNames() {
|
|
38
|
+
return Object.keys(exports.CountryData);
|
|
39
|
+
}
|