country-codes-library 1.1.3 → 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 CHANGED
@@ -1,60 +1,343 @@
1
1
  # Country Codes Library
2
2
 
3
- The Country Code Library provides a collection of two-letter and three-letter country codes according to the ISO 3166-1 standard,
4
- as well as it provides USA, China and Canada Province codes (State codes / adminstrative division codes).
5
- In addition, it includes telephone calling codes, currency codes, currency Symbols for countries across the world.
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.
6
4
 
7
- ## Installation
5
+ ## 🚀 Features
8
6
 
9
- To install the Country Codes Library:
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
10
19
 
11
20
  ```bash
12
- npm install country-codes-lib
21
+ npm install country-codes-library
13
22
  ```
14
23
 
15
- ## Usage
16
-
17
- After installing the Country Codes Library, you can use it in your project to retrieve two-letter, three-letter country codes, telephone calling codes, state/province/administrative division codes, and currency codes. Here are some examples:
24
+ ## 📖 Usage
18
25
 
19
- ## JavaScript Example
26
+ ### Basic Usage (Original API - Still Supported)
20
27
 
21
28
  ```js
22
- const { TwoLetterISORegionCode, ThreeLetterISORegionCode, CountryCallingCodes, USAStateCode, CanadaProvinceCode, ChinaProvinceCode, CountryCurrencyCodes, CountryCurrencySymbols } = require('country-codes-library');
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
23
170
 
24
- console.log(`USA's Two Letter Country Code is ${TwoLetterISORegionCode.UnitedStates}`);
25
- console.log(`India's Three Letter Country Code is ${ThreeLetterISORegionCode.India}`);
26
- console.log(`Germany's Telephone Code is ${CountryCallingCodes.Germany}`);
27
- console.log(`Alabama's Two Letter State Code is ${USAStateCode.Alabama}`);
28
- console.log(`Ontario's Two Letter Province Code is ${CanadaProvinceCode.Ontario}`);
29
- console.log(`Beijing's Two Letter Province Code is ${ChinaProvinceCode.Beijing}`);
30
- console.log(`Bangladesh's Currency Code is ${CountryCurrencyCodes.Bangladesh}`);
31
- console.log(`Japan's Currency Symbol is ${CountryCurrencySymbols.Japan}`);
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
+ */
32
207
  ```
33
208
 
34
- ## TypeScript Example
209
+ ### TypeScript Support
210
+
211
+ Full TypeScript definitions with autocomplete:
35
212
 
36
213
  ```ts
37
- import { TwoLetterISORegionCode, ThreeLetterISORegionCode, CountryCallingCodes, USAStateCode, CanadaProvinceCode, ChinaProvinceCode, CountryCurrencyCodes, CountryCurrencySymbols } from 'country-codes-library';
38
-
39
- console.log(`USA's Two Letter Country Code is ${TwoLetterISORegionCode.UnitedStates}`);
40
- console.log(`India's Three Letter Country Code is ${ThreeLetterISORegionCode.India}`);
41
- console.log(`Germany's Telephone Code is ${CountryCallingCodes.Germany}`);
42
- console.log(`Alabama's Two Letter State Code is ${USAStateCode.Alabama}`);
43
- console.log(`Ontario's Two Letter Province Code is ${CanadaProvinceCode.Ontario}`);
44
- console.log(`Beijing's Two Letter Province Code is ${ChinaProvinceCode.Beijing}`);
45
- console.log(`Bangladesh's Currency Code is ${CountryCurrencyCodes.Bangladesh}`);
46
- console.log(`Japan's Currency Symbol is ${CountryCurrencySymbols.Japan}`);
47
-
48
- ```
49
-
50
- ## Output
51
- ```console
52
- USA's Two Letter Country Code is US
53
- India's Three Letter Country Code is IND
54
- Germany's Telephone Code is +49
55
- Alabama's Two Letter State Code is AL
56
- Ontario's Two Letter Province Code is ON
57
- Beijing's Two Letter Province Code is BJ
58
- Bangladesh's Currency Code is BDT
59
- Japan's Currency Symbol is ¥
60
- ```
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
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Mapping of PascalCase keys to proper country names
3
+ * This provides the canonical display names for all countries
4
+ */
5
+ export declare const CountryNames: Record<string, string>;