localized-regions 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/LICENSE +21 -0
- package/README.md +120 -0
- package/data/countries.json +1002 -0
- package/data/provinces.json +31030 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +141 -0
- package/dist/types.d.ts +41 -0
- package/dist/types.js +2 -0
- package/package.json +73 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Country, CountryOption, Province, City, Option } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* All countries. Pass a locale (e.g. "es") to get bilingual labels like
|
|
4
|
+
* "Spain (España)". Omit or pass "en" for English-only labels.
|
|
5
|
+
*/
|
|
6
|
+
export declare function getCountries(opts?: {
|
|
7
|
+
locale?: string;
|
|
8
|
+
}): CountryOption[];
|
|
9
|
+
export declare function getCountry(code: string): Country | undefined;
|
|
10
|
+
/** Bilingual (or English) display label for a single country code. */
|
|
11
|
+
export declare function formatCountryLabel(code: string, locale?: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* Provinces for a country. Spain returns the curated 50 Provinces (RAT-1776);
|
|
14
|
+
* other countries return their ISO 3166-2 subdivisions.
|
|
15
|
+
*/
|
|
16
|
+
export declare function getProvinces(countryCode: string): Province[];
|
|
17
|
+
export declare function getProvince(provinceCode: string): Province | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Cities. With no provinceCode, returns all cities of the country. With a
|
|
20
|
+
* provinceCode, returns cities for that subdivision (Spain: mapped to the
|
|
21
|
+
* parent community, best-effort).
|
|
22
|
+
*/
|
|
23
|
+
export declare function getCities(countryCode: string, provinceCode?: string): City[];
|
|
24
|
+
/** Country options for react-select / MUI. value = ISO code, label = bilingual. */
|
|
25
|
+
export declare function countryOptions(locale?: string): Option[];
|
|
26
|
+
/** Province options. value = ISO 3166-2 code, label = province name. */
|
|
27
|
+
export declare function provinceOptions(countryCode: string): Option[];
|
|
28
|
+
/** City options. value = label = city name (cities have no stable code). */
|
|
29
|
+
export declare function cityOptions(countryCode: string, provinceCode?: string): Option[];
|
|
30
|
+
export * from './types';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
36
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.getCountries = getCountries;
|
|
40
|
+
exports.getCountry = getCountry;
|
|
41
|
+
exports.formatCountryLabel = formatCountryLabel;
|
|
42
|
+
exports.getProvinces = getProvinces;
|
|
43
|
+
exports.getProvince = getProvince;
|
|
44
|
+
exports.getCities = getCities;
|
|
45
|
+
exports.countryOptions = countryOptions;
|
|
46
|
+
exports.provinceOptions = provinceOptions;
|
|
47
|
+
exports.cityOptions = cityOptions;
|
|
48
|
+
const path = __importStar(require("path"));
|
|
49
|
+
const country_state_city_1 = require("country-state-city");
|
|
50
|
+
const DATA = path.join(__dirname, '..', 'data');
|
|
51
|
+
// Static, build-time generated data (see scripts/generate.js). Loaded at runtime
|
|
52
|
+
// so tsc does not need to copy JSON into dist.
|
|
53
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
54
|
+
const countries = require(path.join(DATA, 'countries.json'));
|
|
55
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
56
|
+
const provinces = require(path.join(DATA, 'provinces.json'));
|
|
57
|
+
function displayName(code, locale) {
|
|
58
|
+
try {
|
|
59
|
+
return new Intl.DisplayNames([locale], { type: 'region' }).of(code) || undefined;
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function buildLabel(name, localized) {
|
|
66
|
+
return localized && localized !== name ? `${name} (${localized})` : name;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* All countries. Pass a locale (e.g. "es") to get bilingual labels like
|
|
70
|
+
* "Spain (España)". Omit or pass "en" for English-only labels.
|
|
71
|
+
*/
|
|
72
|
+
function getCountries(opts = {}) {
|
|
73
|
+
const { locale } = opts;
|
|
74
|
+
return countries.map((c) => {
|
|
75
|
+
const localized = locale && locale !== 'en' ? displayName(c.code, locale) : undefined;
|
|
76
|
+
return { code: c.code, name: c.name, localized, label: buildLabel(c.name, localized) };
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
function getCountry(code) {
|
|
80
|
+
return countries.find((c) => c.code === code.toUpperCase());
|
|
81
|
+
}
|
|
82
|
+
/** Bilingual (or English) display label for a single country code. */
|
|
83
|
+
function formatCountryLabel(code, locale) {
|
|
84
|
+
const c = getCountry(code);
|
|
85
|
+
if (!c)
|
|
86
|
+
return code;
|
|
87
|
+
const localized = locale && locale !== 'en' ? displayName(code, locale) : undefined;
|
|
88
|
+
return buildLabel(c.name, localized);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Provinces for a country. Spain returns the curated 50 Provinces (RAT-1776);
|
|
92
|
+
* other countries return their ISO 3166-2 subdivisions.
|
|
93
|
+
*/
|
|
94
|
+
function getProvinces(countryCode) {
|
|
95
|
+
return provinces[countryCode.toUpperCase()] || [];
|
|
96
|
+
}
|
|
97
|
+
function getProvince(provinceCode) {
|
|
98
|
+
const cc = provinceCode.split('-')[0];
|
|
99
|
+
return getProvinces(cc).find((p) => p.code === provinceCode);
|
|
100
|
+
}
|
|
101
|
+
// Spain cities in the source data are grouped at the autonomous-community level,
|
|
102
|
+
// not by the 50 provinces. Where the community ISO code differs from the source
|
|
103
|
+
// state code, map it here. City is best-effort for Spain and is expected to be a
|
|
104
|
+
// free-text field (RAT-1772).
|
|
105
|
+
const ES_COMMUNITY_TO_SOURCE = {
|
|
106
|
+
'ES-IB': 'PM', // Balearic Islands
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Cities. With no provinceCode, returns all cities of the country. With a
|
|
110
|
+
* provinceCode, returns cities for that subdivision (Spain: mapped to the
|
|
111
|
+
* parent community, best-effort).
|
|
112
|
+
*/
|
|
113
|
+
function getCities(countryCode, provinceCode) {
|
|
114
|
+
const cc = countryCode.toUpperCase();
|
|
115
|
+
if (!provinceCode) {
|
|
116
|
+
return country_state_city_1.City.getCitiesOfCountry(cc) || [];
|
|
117
|
+
}
|
|
118
|
+
if (cc === 'ES') {
|
|
119
|
+
const community = getProvince(provinceCode)?.communityCode;
|
|
120
|
+
if (!community)
|
|
121
|
+
return [];
|
|
122
|
+
const source = ES_COMMUNITY_TO_SOURCE[community] || community.split('-')[1];
|
|
123
|
+
return country_state_city_1.City.getCitiesOfState(cc, source) || [];
|
|
124
|
+
}
|
|
125
|
+
const bare = provinceCode.includes('-') ? provinceCode.split('-').slice(1).join('-') : provinceCode;
|
|
126
|
+
return country_state_city_1.City.getCitiesOfState(cc, bare) || [];
|
|
127
|
+
}
|
|
128
|
+
// --------------------------------------------------- ready-to-use dropdown options
|
|
129
|
+
/** Country options for react-select / MUI. value = ISO code, label = bilingual. */
|
|
130
|
+
function countryOptions(locale) {
|
|
131
|
+
return getCountries({ locale }).map((c) => ({ value: c.code, label: c.label }));
|
|
132
|
+
}
|
|
133
|
+
/** Province options. value = ISO 3166-2 code, label = province name. */
|
|
134
|
+
function provinceOptions(countryCode) {
|
|
135
|
+
return getProvinces(countryCode).map((p) => ({ value: p.code, label: p.name }));
|
|
136
|
+
}
|
|
137
|
+
/** City options. value = label = city name (cities have no stable code). */
|
|
138
|
+
function cityOptions(countryCode, provinceCode) {
|
|
139
|
+
return getCities(countryCode, provinceCode).map((c) => ({ value: c.name, label: c.name }));
|
|
140
|
+
}
|
|
141
|
+
__exportStar(require("./types"), exports);
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export interface Country {
|
|
2
|
+
/** ISO 3166-1 alpha-2 code, e.g. "ES" */
|
|
3
|
+
code: string;
|
|
4
|
+
/** English name, e.g. "Spain" */
|
|
5
|
+
name: string;
|
|
6
|
+
}
|
|
7
|
+
export interface CountryOption {
|
|
8
|
+
code: string;
|
|
9
|
+
/** English name */
|
|
10
|
+
name: string;
|
|
11
|
+
/** Name in the requested locale, e.g. "España" (undefined when locale is en/absent) */
|
|
12
|
+
localized?: string;
|
|
13
|
+
/** Display label, e.g. "Spain (España)" when localized, else "Spain" */
|
|
14
|
+
label: string;
|
|
15
|
+
}
|
|
16
|
+
export interface Province {
|
|
17
|
+
/** ISO 3166-2 code, e.g. "ES-MA" */
|
|
18
|
+
code: string;
|
|
19
|
+
/** Parent country ISO 3166-1 alpha-2 code, e.g. "ES" */
|
|
20
|
+
countryCode: string;
|
|
21
|
+
/** Province name, e.g. "Málaga" */
|
|
22
|
+
name: string;
|
|
23
|
+
/** Parent autonomous community / region name (Spain only), e.g. "Andalusia" */
|
|
24
|
+
community?: string;
|
|
25
|
+
/** Parent autonomous community ISO 3166-2 code (Spain only), e.g. "ES-AN" */
|
|
26
|
+
communityCode?: string;
|
|
27
|
+
/** iso-3166-2 subdivision type for non-Spain countries, e.g. "Province" / "State" */
|
|
28
|
+
type?: string;
|
|
29
|
+
}
|
|
30
|
+
export interface City {
|
|
31
|
+
name: string;
|
|
32
|
+
countryCode: string;
|
|
33
|
+
stateCode?: string;
|
|
34
|
+
latitude?: string;
|
|
35
|
+
longitude?: string;
|
|
36
|
+
}
|
|
37
|
+
/** react-select / MUI style option */
|
|
38
|
+
export interface Option {
|
|
39
|
+
value: string;
|
|
40
|
+
label: string;
|
|
41
|
+
}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "localized-regions",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Country, province and city data with bilingual localized labels (e.g. \"Spain (España)\") and ISO 3166-1 / ISO 3166-2 codes. Ships all 50 Spanish provinces. Framework-agnostic, drop-in options for react-select and MUI dropdowns.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"data",
|
|
10
|
+
"README.md",
|
|
11
|
+
"LICENSE"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"generate": "node scripts/generate.js",
|
|
15
|
+
"build": "tsc -p tsconfig.json",
|
|
16
|
+
"prepublishOnly": "npm run generate && npm run build"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"country",
|
|
20
|
+
"countries",
|
|
21
|
+
"province",
|
|
22
|
+
"provinces",
|
|
23
|
+
"city",
|
|
24
|
+
"cities",
|
|
25
|
+
"region",
|
|
26
|
+
"regions",
|
|
27
|
+
"iso-3166",
|
|
28
|
+
"iso-3166-1",
|
|
29
|
+
"iso-3166-2",
|
|
30
|
+
"country-codes",
|
|
31
|
+
"country-list",
|
|
32
|
+
"country-names",
|
|
33
|
+
"localized",
|
|
34
|
+
"bilingual",
|
|
35
|
+
"i18n",
|
|
36
|
+
"localization",
|
|
37
|
+
"country-picker",
|
|
38
|
+
"country-select",
|
|
39
|
+
"province-select",
|
|
40
|
+
"region-picker",
|
|
41
|
+
"dropdown",
|
|
42
|
+
"react-select",
|
|
43
|
+
"mui",
|
|
44
|
+
"spain",
|
|
45
|
+
"spanish-provinces",
|
|
46
|
+
"provincias",
|
|
47
|
+
"autonomous-communities",
|
|
48
|
+
"subdivisions",
|
|
49
|
+
"address",
|
|
50
|
+
"geo"
|
|
51
|
+
],
|
|
52
|
+
"homepage": "https://github.com/amit-2011/localized-regions-#readme",
|
|
53
|
+
"repository": {
|
|
54
|
+
"type": "git",
|
|
55
|
+
"url": "git+https://github.com/amit-2011/localized-regions-.git"
|
|
56
|
+
},
|
|
57
|
+
"bugs": {
|
|
58
|
+
"url": "https://github.com/amit-2011/localized-regions-/issues"
|
|
59
|
+
},
|
|
60
|
+
"author": "Amit Tank <amit@techvoot.com>",
|
|
61
|
+
"license": "MIT",
|
|
62
|
+
"engines": {
|
|
63
|
+
"node": ">=14"
|
|
64
|
+
},
|
|
65
|
+
"dependencies": {
|
|
66
|
+
"country-state-city": "^3.2.1"
|
|
67
|
+
},
|
|
68
|
+
"devDependencies": {
|
|
69
|
+
"@types/node": "^20.11.0",
|
|
70
|
+
"iso-3166-2": "^1.0.0",
|
|
71
|
+
"typescript": "^5.4.0"
|
|
72
|
+
}
|
|
73
|
+
}
|