@innovayse/geo-atlas 2.1.0 → 2.2.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 +57 -20
- package/dist/cjs/cities.d.ts +12 -0
- package/dist/cjs/cities.js +58 -0
- package/dist/cjs/helpers/CountriesAtlas.d.ts +11 -71
- package/dist/cjs/helpers/CountriesAtlas.js +11 -161
- package/dist/cjs/helpers/CountriesData.d.ts +86 -0
- package/dist/cjs/helpers/CountriesData.js +157 -0
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +7 -1
- package/dist/cjs/lite.d.ts +27 -0
- package/dist/cjs/lite.js +30 -0
- package/dist/esm/cities.d.ts +12 -0
- package/dist/esm/cities.js +51 -0
- package/dist/esm/helpers/CountriesAtlas.d.ts +11 -71
- package/dist/esm/helpers/CountriesAtlas.js +11 -161
- package/dist/esm/helpers/CountriesData.d.ts +86 -0
- package/dist/esm/helpers/CountriesData.js +150 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/lite.d.ts +27 -0
- package/dist/esm/lite.js +22 -0
- package/package.json +13 -1
|
@@ -0,0 +1,157 @@
|
|
|
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.CountriesData = void 0;
|
|
7
|
+
const atlas_json_1 = __importDefault(require("../data/atlas.json"));
|
|
8
|
+
/**
|
|
9
|
+
* Lightweight country-list helper.
|
|
10
|
+
*
|
|
11
|
+
* Loads ONLY the bundled `atlas.json` (the country list plus per-country
|
|
12
|
+
* metadata: names, translations, phone, currency, timezones). It deliberately
|
|
13
|
+
* does NOT import the per-country state/city loaders (`countryLoaders`), so
|
|
14
|
+
* importing this class never drags the ~171 MB of per-country state/city JSON
|
|
15
|
+
* into a consumer's bundle.
|
|
16
|
+
*
|
|
17
|
+
* Use this when you only need countries, phone codes, currencies or timezones
|
|
18
|
+
* — e.g. on a server-rendered path. When you need states/cities, use
|
|
19
|
+
* `CountriesAtlas` (which extends this) or the `@touchestate/geo-atlas/cities`
|
|
20
|
+
* entry point.
|
|
21
|
+
*/
|
|
22
|
+
class CountriesData {
|
|
23
|
+
/** Initializes the atlas by loading the bundled country data. */
|
|
24
|
+
constructor() {
|
|
25
|
+
this.countries = atlas_json_1.default;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Retrieve all countries with specified properties, or all countries if no properties are specified.
|
|
29
|
+
*
|
|
30
|
+
* @param {string[]} properties - Array of properties to retrieve.
|
|
31
|
+
* @return {Country[]} - Array of country objects with specified properties or all countries.
|
|
32
|
+
*/
|
|
33
|
+
getCountries(properties) {
|
|
34
|
+
if (properties) {
|
|
35
|
+
return this.countries.map(country => {
|
|
36
|
+
const newCountry = {};
|
|
37
|
+
properties.forEach(property => {
|
|
38
|
+
newCountry[property] = country[property];
|
|
39
|
+
});
|
|
40
|
+
return newCountry;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return this.countries;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Find a country by its ISO2 code.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} iso2 - ISO2 code of the country to find.
|
|
49
|
+
* @return {Country | undefined} - Country object or undefined if not found.
|
|
50
|
+
*/
|
|
51
|
+
find(iso2) {
|
|
52
|
+
return this.countries.find(country => { var _a; return ((_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toUpperCase()) === iso2.toUpperCase(); });
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Find a country by its ISO3 code.
|
|
56
|
+
*
|
|
57
|
+
* @param {string} iso3 - ISO3 code of the country to find.
|
|
58
|
+
* @return {Country | undefined} - Country object or undefined if not found.
|
|
59
|
+
*/
|
|
60
|
+
findByIso3(iso3) {
|
|
61
|
+
return this.countries.find(country => { var _a; return ((_a = country.iso3) === null || _a === void 0 ? void 0 : _a.toUpperCase()) === iso3.toUpperCase(); });
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Retrieve all timezones of all available countries.
|
|
65
|
+
*
|
|
66
|
+
* @returns {Timezone[]} - Array of timezone objects.
|
|
67
|
+
*/
|
|
68
|
+
getTimezones() {
|
|
69
|
+
const timezoneSet = new Set();
|
|
70
|
+
this.countries.forEach(country => {
|
|
71
|
+
var _a;
|
|
72
|
+
(_a = country.timezones) === null || _a === void 0 ? void 0 : _a.forEach(timezone => {
|
|
73
|
+
timezoneSet.add(timezone);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
return Array.from(timezoneSet);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Retrieve all timezones of a country by its ISO2 code.
|
|
80
|
+
*
|
|
81
|
+
* @param {string} iso2 - ISO2 code of the country.
|
|
82
|
+
* @returns {Timezone[] | undefined} - Array of timezone objects or undefined if not found.
|
|
83
|
+
*/
|
|
84
|
+
timezone(iso2) {
|
|
85
|
+
const country = this.find(iso2);
|
|
86
|
+
if (country) {
|
|
87
|
+
return country.timezones;
|
|
88
|
+
}
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Retrieve all calling codes of all available countries.
|
|
93
|
+
*
|
|
94
|
+
* @returns {PhoneCode[]} - Array of phone code objects.
|
|
95
|
+
*/
|
|
96
|
+
getCallingCodes() {
|
|
97
|
+
return this.getCountries(['name', 'phone', 'iso2', 'phone_format']).map(country => {
|
|
98
|
+
var _a;
|
|
99
|
+
return Object.assign(Object.assign({}, country), { phone_code: `+${country.phone}`, flag: `flag flag-${(_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toLowerCase()}` });
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Retrieve calling codes of a country by its ISO2 code.
|
|
104
|
+
*
|
|
105
|
+
* @param {string} iso2 - ISO2 code of the country.
|
|
106
|
+
* @returns {PhoneCode | undefined} - Phone code object or undefined if not found.
|
|
107
|
+
*/
|
|
108
|
+
callingCode(iso2) {
|
|
109
|
+
var _a;
|
|
110
|
+
const country = this.find(iso2);
|
|
111
|
+
if (country) {
|
|
112
|
+
return {
|
|
113
|
+
name: country.name,
|
|
114
|
+
phone: country.phone,
|
|
115
|
+
iso2: country.iso2,
|
|
116
|
+
phone_code: `+${country.phone}`,
|
|
117
|
+
flag: `flag flag-${(_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toLowerCase()}`,
|
|
118
|
+
phone_format: country.phone_format
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Retrieve all currencies of all available countries.
|
|
125
|
+
*
|
|
126
|
+
* @returns {Currency[]} - Array of currency objects.
|
|
127
|
+
*/
|
|
128
|
+
getCurrencies() {
|
|
129
|
+
return this.getCountries(['name', 'iso2', 'currency', 'currency_symbol', 'currency_name']).map(country => {
|
|
130
|
+
var _a;
|
|
131
|
+
return Object.assign(Object.assign({}, country), { flag: `flag flag-${(_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toLowerCase()}` });
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Retrieve currency of a country by its ISO2 code.
|
|
136
|
+
*
|
|
137
|
+
* @param {string} iso2 - ISO2 code of the country.
|
|
138
|
+
* @returns {Currency | undefined} - Currency object or undefined if not found.
|
|
139
|
+
*/
|
|
140
|
+
currency(iso2) {
|
|
141
|
+
var _a;
|
|
142
|
+
const country = this.find(iso2);
|
|
143
|
+
if (country) {
|
|
144
|
+
return {
|
|
145
|
+
name: country.name,
|
|
146
|
+
iso2: country.iso2,
|
|
147
|
+
currency: country.currency,
|
|
148
|
+
currency_symbol: country.currency_symbol,
|
|
149
|
+
currency_name: country.currency_name,
|
|
150
|
+
flag: `flag flag-${(_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toLowerCase()}`
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
exports.CountriesData = CountriesData;
|
|
157
|
+
exports.default = new CountriesData();
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
* const country = CountriesAtlas.find('US');
|
|
14
14
|
*/
|
|
15
15
|
export { default as CountriesAtlas } from './helpers/CountriesAtlas';
|
|
16
|
+
export { default as CountriesData, CountriesData as CountriesDataClass } from './helpers/CountriesData';
|
|
16
17
|
export { default as ValidatorAtlas } from './helpers/ValidatorAtlas';
|
|
17
18
|
export type { Country } from './types/country.interface';
|
|
18
19
|
export type { State } from './types/state.interface';
|
package/dist/cjs/index.js
CHANGED
|
@@ -17,8 +17,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
17
17
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
18
|
};
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.ValidatorAtlas = exports.CountriesAtlas = void 0;
|
|
20
|
+
exports.ValidatorAtlas = exports.CountriesDataClass = exports.CountriesData = exports.CountriesAtlas = void 0;
|
|
21
21
|
var CountriesAtlas_1 = require("./helpers/CountriesAtlas");
|
|
22
22
|
Object.defineProperty(exports, "CountriesAtlas", { enumerable: true, get: function () { return __importDefault(CountriesAtlas_1).default; } });
|
|
23
|
+
// Lightweight country-list helper (no per-country state/city dataset). Prefer
|
|
24
|
+
// importing this — or the `@touchestate/geo-atlas/lite` entry — on paths that
|
|
25
|
+
// only need the country list, to avoid bundling the ~171 MB per-country data.
|
|
26
|
+
var CountriesData_1 = require("./helpers/CountriesData");
|
|
27
|
+
Object.defineProperty(exports, "CountriesData", { enumerable: true, get: function () { return __importDefault(CountriesData_1).default; } });
|
|
28
|
+
Object.defineProperty(exports, "CountriesDataClass", { enumerable: true, get: function () { return CountriesData_1.CountriesData; } });
|
|
23
29
|
var ValidatorAtlas_1 = require("./helpers/ValidatorAtlas");
|
|
24
30
|
Object.defineProperty(exports, "ValidatorAtlas", { enumerable: true, get: function () { return __importDefault(ValidatorAtlas_1).default; } });
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @module touchestate/geo-atlas/lite
|
|
4
|
+
*
|
|
5
|
+
* @description Lightweight entry point exposing only the country list and its
|
|
6
|
+
* metadata (names, translations, phone codes, currencies, timezones). Importing
|
|
7
|
+
* from here loads just the small bundled `atlas.json` — it never references the
|
|
8
|
+
* per-country state/city dataset (~171 MB), so it is safe to use on
|
|
9
|
+
* server-rendered / memory-constrained bundling paths.
|
|
10
|
+
*
|
|
11
|
+
* For states/cities, import from `@touchestate/geo-atlas/cities` (loaded on
|
|
12
|
+
* demand, browser/client side) or use the full `CountriesAtlas` from the root
|
|
13
|
+
* entry point.
|
|
14
|
+
*
|
|
15
|
+
* @license MIT
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* import CountriesData from '@touchestate/geo-atlas/lite';
|
|
19
|
+
* const countries = CountriesData.getCountries();
|
|
20
|
+
* const country = CountriesData.find('US');
|
|
21
|
+
*/
|
|
22
|
+
export { CountriesData, default } from './helpers/CountriesData';
|
|
23
|
+
export type { Country } from './types/country.interface';
|
|
24
|
+
export type { Timezone } from './types/timezone.type';
|
|
25
|
+
export type { Translations } from './types/translation.type';
|
|
26
|
+
export type { Currency } from './types/currency.type';
|
|
27
|
+
export type { PhoneCode } from './types/phone-code.interface';
|
package/dist/cjs/lite.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
* @module touchestate/geo-atlas/lite
|
|
5
|
+
*
|
|
6
|
+
* @description Lightweight entry point exposing only the country list and its
|
|
7
|
+
* metadata (names, translations, phone codes, currencies, timezones). Importing
|
|
8
|
+
* from here loads just the small bundled `atlas.json` — it never references the
|
|
9
|
+
* per-country state/city dataset (~171 MB), so it is safe to use on
|
|
10
|
+
* server-rendered / memory-constrained bundling paths.
|
|
11
|
+
*
|
|
12
|
+
* For states/cities, import from `@touchestate/geo-atlas/cities` (loaded on
|
|
13
|
+
* demand, browser/client side) or use the full `CountriesAtlas` from the root
|
|
14
|
+
* entry point.
|
|
15
|
+
*
|
|
16
|
+
* @license MIT
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* import CountriesData from '@touchestate/geo-atlas/lite';
|
|
20
|
+
* const countries = CountriesData.getCountries();
|
|
21
|
+
* const country = CountriesData.find('US');
|
|
22
|
+
*/
|
|
23
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
24
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
25
|
+
};
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
exports.default = exports.CountriesData = void 0;
|
|
28
|
+
var CountriesData_1 = require("./helpers/CountriesData");
|
|
29
|
+
Object.defineProperty(exports, "CountriesData", { enumerable: true, get: function () { return CountriesData_1.CountriesData; } });
|
|
30
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(CountriesData_1).default; } });
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { State } from './types/state.interface';
|
|
2
|
+
/**
|
|
3
|
+
* Load the states (with their cities) for a country by ISO2 code.
|
|
4
|
+
*
|
|
5
|
+
* Resolves the per-country data chunk via the code-split `countryLoaders`
|
|
6
|
+
* registry, so only the requested country's JSON is fetched at runtime.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} iso2 - ISO2 code of the country (case-insensitive).
|
|
9
|
+
* @returns {Promise<State[]>} - Promise resolving to the country's states, or an empty array if unavailable.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getStatesForCountry(iso2: string): Promise<State[]>;
|
|
12
|
+
export type { State } from './types/state.interface';
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* @packageDocumentation
|
|
12
|
+
* @module touchestate/geo-atlas/cities
|
|
13
|
+
*
|
|
14
|
+
* @description On-demand per-country state/city loader. This entry point
|
|
15
|
+
* references `countryLoaders`, which pulls in the per-country JSON dataset
|
|
16
|
+
* (~171 MB across ~250 code-split chunks). Import it only from client/browser
|
|
17
|
+
* paths where the data is actually needed — keep it out of server-rendered
|
|
18
|
+
* bundles by importing it dynamically (e.g. `await import('...cities')`).
|
|
19
|
+
*
|
|
20
|
+
* @license MIT
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const { getStatesForCountry } = await import('@touchestate/geo-atlas/cities');
|
|
24
|
+
* const states = await getStatesForCountry('AM');
|
|
25
|
+
*/
|
|
26
|
+
import countryLoaders from './helpers/countryLoaders';
|
|
27
|
+
/**
|
|
28
|
+
* Load the states (with their cities) for a country by ISO2 code.
|
|
29
|
+
*
|
|
30
|
+
* Resolves the per-country data chunk via the code-split `countryLoaders`
|
|
31
|
+
* registry, so only the requested country's JSON is fetched at runtime.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} iso2 - ISO2 code of the country (case-insensitive).
|
|
34
|
+
* @returns {Promise<State[]>} - Promise resolving to the country's states, or an empty array if unavailable.
|
|
35
|
+
*/
|
|
36
|
+
export function getStatesForCountry(iso2) {
|
|
37
|
+
var _a, _b;
|
|
38
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
const loader = countryLoaders[iso2.toLowerCase()];
|
|
40
|
+
if (!loader)
|
|
41
|
+
return [];
|
|
42
|
+
try {
|
|
43
|
+
const mod = yield loader();
|
|
44
|
+
const data = (_a = mod.default) !== null && _a !== void 0 ? _a : mod;
|
|
45
|
+
return (_b = data.states) !== null && _b !== void 0 ? _b : [];
|
|
46
|
+
}
|
|
47
|
+
catch (_c) {
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
@@ -1,38 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Currency } from '../types/currency.type';
|
|
3
|
-
import { PhoneCode } from '../types/phone-code.interface';
|
|
1
|
+
import { CountriesData } from './CountriesData';
|
|
4
2
|
import { State } from '../types/state.interface';
|
|
5
|
-
import { Timezone } from '../types/timezone.type';
|
|
6
3
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
4
|
+
* Full offline geo-atlas helper.
|
|
5
|
+
*
|
|
6
|
+
* Extends {@link CountriesData} (country list / phone / currency / timezone —
|
|
7
|
+
* all from the small bundled `atlas.json`) and adds per-country state & city
|
|
8
|
+
* access. The state/city methods pull data through `countryLoaders`, which
|
|
9
|
+
* references every per-country JSON file (~171 MB total). Because that import
|
|
10
|
+
* lives here and NOT in `CountriesData`, consumers that only need the country
|
|
11
|
+
* list can import `CountriesData` (or the `/lite` entry) and avoid bundling the
|
|
12
|
+
* per-country dataset entirely.
|
|
9
13
|
*/
|
|
10
|
-
export declare class CountriesAtlas {
|
|
11
|
-
/** Internal countries array loaded from the bundled atlas.json. */
|
|
12
|
-
private countries;
|
|
13
|
-
/** Initializes the atlas by loading the bundled country data. */
|
|
14
|
-
constructor();
|
|
15
|
-
/**
|
|
16
|
-
* Retrieve all countries with specified properties, or all countries if no properties are specified.
|
|
17
|
-
*
|
|
18
|
-
* @param {string[]} properties - Array of properties to retrieve.
|
|
19
|
-
* @return {Country[]} - Array of country objects with specified properties or all countries.
|
|
20
|
-
*/
|
|
21
|
-
getCountries(properties?: string[]): Country[];
|
|
22
|
-
/**
|
|
23
|
-
* Find a country by its ISO2 code.
|
|
24
|
-
*
|
|
25
|
-
* @param {string} iso2 - ISO2 code of the country to find.
|
|
26
|
-
* @return {Country | undefined} - Country object or undefined if not found.
|
|
27
|
-
*/
|
|
28
|
-
find(iso2: string): Country | undefined;
|
|
29
|
-
/**
|
|
30
|
-
* Find a country by its ISO3 code.
|
|
31
|
-
*
|
|
32
|
-
* @param {string} iso3 - ISO3 code of the country to find.
|
|
33
|
-
* @return {Country | undefined} - Country object or undefined if not found.
|
|
34
|
-
*/
|
|
35
|
-
findByIso3(iso3: string): Country | undefined;
|
|
14
|
+
export declare class CountriesAtlas extends CountriesData {
|
|
36
15
|
/**
|
|
37
16
|
* Retrieve all states of a country by its ISO2 code.
|
|
38
17
|
*
|
|
@@ -59,45 +38,6 @@ export declare class CountriesAtlas {
|
|
|
59
38
|
* @returns {State | undefined} - State object or undefined if not found.
|
|
60
39
|
*/
|
|
61
40
|
state(iso2: string, stateCode: string): State | undefined;
|
|
62
|
-
/**
|
|
63
|
-
* Retrieve all timezones of all available countries.
|
|
64
|
-
*
|
|
65
|
-
* @returns {Timezone[]} - Array of timezone objects.
|
|
66
|
-
*/
|
|
67
|
-
getTimezones(): Timezone[];
|
|
68
|
-
/**
|
|
69
|
-
* Retrieve all timezones of a country by its ISO2 code.
|
|
70
|
-
*
|
|
71
|
-
* @param {string} iso2 - ISO2 code of the country.
|
|
72
|
-
* @returns {Timezone[] | undefined} - Array of timezone objects or undefined if not found.
|
|
73
|
-
*/
|
|
74
|
-
timezone(iso2: string): Timezone[] | undefined;
|
|
75
|
-
/**
|
|
76
|
-
* Retrieve all calling codes of all available countries.
|
|
77
|
-
*
|
|
78
|
-
* @returns {PhoneCode[]} - Array of phone code objects.
|
|
79
|
-
*/
|
|
80
|
-
getCallingCodes(): PhoneCode[];
|
|
81
|
-
/**
|
|
82
|
-
* Retrieve calling codes of a country by its ISO2 code.
|
|
83
|
-
*
|
|
84
|
-
* @param {string} iso2 - ISO2 code of the country.
|
|
85
|
-
* @returns {PhoneCode | undefined} - Phone code object or undefined if not found.
|
|
86
|
-
*/
|
|
87
|
-
callingCode(iso2: string): PhoneCode | undefined;
|
|
88
|
-
/**
|
|
89
|
-
* Retrieve all currencies of all available countries.
|
|
90
|
-
*
|
|
91
|
-
* @returns {Currency[]} - Array of currency objects.
|
|
92
|
-
*/
|
|
93
|
-
getCurrencies(): Currency[];
|
|
94
|
-
/**
|
|
95
|
-
* Retrieve currency of a country by its ISO2 code.
|
|
96
|
-
*
|
|
97
|
-
* @param {string} iso2 - ISO2 code of the country.
|
|
98
|
-
* @returns {Currency | undefined} - Currency object or undefined if not found.
|
|
99
|
-
*/
|
|
100
|
-
currency(iso2: string): Currency | undefined;
|
|
101
41
|
}
|
|
102
42
|
declare const _default: CountriesAtlas;
|
|
103
43
|
export default _default;
|
|
@@ -7,66 +7,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import
|
|
10
|
+
import { CountriesData } from './CountriesData';
|
|
11
11
|
import countryLoaders from './countryLoaders';
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
13
|
+
* Full offline geo-atlas helper.
|
|
14
|
+
*
|
|
15
|
+
* Extends {@link CountriesData} (country list / phone / currency / timezone —
|
|
16
|
+
* all from the small bundled `atlas.json`) and adds per-country state & city
|
|
17
|
+
* access. The state/city methods pull data through `countryLoaders`, which
|
|
18
|
+
* references every per-country JSON file (~171 MB total). Because that import
|
|
19
|
+
* lives here and NOT in `CountriesData`, consumers that only need the country
|
|
20
|
+
* list can import `CountriesData` (or the `/lite` entry) and avoid bundling the
|
|
21
|
+
* per-country dataset entirely.
|
|
15
22
|
*/
|
|
16
|
-
export class CountriesAtlas {
|
|
17
|
-
/** Initializes the atlas by loading the bundled country data. */
|
|
18
|
-
constructor() {
|
|
19
|
-
this.countries = countriesData;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Retrieve all countries with specified properties, or all countries if no properties are specified.
|
|
23
|
-
*
|
|
24
|
-
* @param {string[]} properties - Array of properties to retrieve.
|
|
25
|
-
* @return {Country[]} - Array of country objects with specified properties or all countries.
|
|
26
|
-
*/
|
|
27
|
-
getCountries(properties) {
|
|
28
|
-
if (properties) {
|
|
29
|
-
return this.countries.map(country => {
|
|
30
|
-
const newCountry = {};
|
|
31
|
-
properties.forEach(property => {
|
|
32
|
-
newCountry[property] = country[property];
|
|
33
|
-
});
|
|
34
|
-
return newCountry;
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
return this.countries;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Find a country by its ISO2 code.
|
|
41
|
-
*
|
|
42
|
-
* @param {string} iso2 - ISO2 code of the country to find.
|
|
43
|
-
* @return {Country | undefined} - Country object or undefined if not found.
|
|
44
|
-
*/
|
|
45
|
-
find(iso2) {
|
|
46
|
-
return this.countries.find(country => { var _a; return ((_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toUpperCase()) === iso2.toUpperCase(); });
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Find a country by its ISO3 code.
|
|
50
|
-
*
|
|
51
|
-
* @param {string} iso3 - ISO3 code of the country to find.
|
|
52
|
-
* @return {Country | undefined} - Country object or undefined if not found.
|
|
53
|
-
*/
|
|
54
|
-
findByIso3(iso3) {
|
|
55
|
-
return this.countries.find(country => { var _a; return ((_a = country.iso3) === null || _a === void 0 ? void 0 : _a.toUpperCase()) === iso3.toUpperCase(); });
|
|
56
|
-
}
|
|
57
|
-
// getStates(iso2: string): Promise<State[]> | undefined {
|
|
58
|
-
// const country = this.find(iso2)
|
|
59
|
-
// if (country) {
|
|
60
|
-
// return import(`../data/countries/${country.iso2?.toLowerCase()}.json`)
|
|
61
|
-
// .then(statesData => {
|
|
62
|
-
// return statesData.states
|
|
63
|
-
// })
|
|
64
|
-
// .catch(() => {
|
|
65
|
-
// return undefined
|
|
66
|
-
// })
|
|
67
|
-
// }
|
|
68
|
-
// return undefined
|
|
69
|
-
// }
|
|
23
|
+
export class CountriesAtlas extends CountriesData {
|
|
70
24
|
/**
|
|
71
25
|
* Retrieve all states of a country by its ISO2 code.
|
|
72
26
|
*
|
|
@@ -116,18 +70,6 @@ export class CountriesAtlas {
|
|
|
116
70
|
}
|
|
117
71
|
});
|
|
118
72
|
}
|
|
119
|
-
// state(iso2: string, stateCode: string): Promise<State | undefined> | undefined {
|
|
120
|
-
// const country = this.find(iso2);
|
|
121
|
-
// if (country) {
|
|
122
|
-
// return import(`../data/countries/${country.iso2?.toLowerCase()}.json`)
|
|
123
|
-
// .then((statesData: StateData) => {
|
|
124
|
-
// const state = statesData.states.find((s: State) => s.state_code?.toUpperCase() === stateCode);
|
|
125
|
-
// return state ? state : undefined;
|
|
126
|
-
// })
|
|
127
|
-
// .catch(() => undefined);
|
|
128
|
-
// }
|
|
129
|
-
// return undefined;
|
|
130
|
-
// }
|
|
131
73
|
/**
|
|
132
74
|
* Find a state by its state code and country ISO2 code.
|
|
133
75
|
*
|
|
@@ -150,97 +92,5 @@ export class CountriesAtlas {
|
|
|
150
92
|
}
|
|
151
93
|
return undefined;
|
|
152
94
|
}
|
|
153
|
-
/**
|
|
154
|
-
* Retrieve all timezones of all available countries.
|
|
155
|
-
*
|
|
156
|
-
* @returns {Timezone[]} - Array of timezone objects.
|
|
157
|
-
*/
|
|
158
|
-
getTimezones() {
|
|
159
|
-
const timezoneSet = new Set();
|
|
160
|
-
this.countries.forEach(country => {
|
|
161
|
-
var _a;
|
|
162
|
-
(_a = country.timezones) === null || _a === void 0 ? void 0 : _a.forEach(timezone => {
|
|
163
|
-
timezoneSet.add(timezone);
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
return Array.from(timezoneSet);
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Retrieve all timezones of a country by its ISO2 code.
|
|
170
|
-
*
|
|
171
|
-
* @param {string} iso2 - ISO2 code of the country.
|
|
172
|
-
* @returns {Timezone[] | undefined} - Array of timezone objects or undefined if not found.
|
|
173
|
-
*/
|
|
174
|
-
timezone(iso2) {
|
|
175
|
-
const country = this.find(iso2);
|
|
176
|
-
if (country) {
|
|
177
|
-
return country.timezones;
|
|
178
|
-
}
|
|
179
|
-
return undefined;
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Retrieve all calling codes of all available countries.
|
|
183
|
-
*
|
|
184
|
-
* @returns {PhoneCode[]} - Array of phone code objects.
|
|
185
|
-
*/
|
|
186
|
-
getCallingCodes() {
|
|
187
|
-
return this.getCountries(['name', 'phone', 'iso2', 'phone_format']).map(country => {
|
|
188
|
-
var _a;
|
|
189
|
-
return Object.assign(Object.assign({}, country), { phone_code: `+${country.phone}`, flag: `flag flag-${(_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toLowerCase()}` });
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Retrieve calling codes of a country by its ISO2 code.
|
|
194
|
-
*
|
|
195
|
-
* @param {string} iso2 - ISO2 code of the country.
|
|
196
|
-
* @returns {PhoneCode | undefined} - Phone code object or undefined if not found.
|
|
197
|
-
*/
|
|
198
|
-
callingCode(iso2) {
|
|
199
|
-
var _a;
|
|
200
|
-
const country = this.find(iso2);
|
|
201
|
-
if (country) {
|
|
202
|
-
return {
|
|
203
|
-
name: country.name,
|
|
204
|
-
phone: country.phone,
|
|
205
|
-
iso2: country.iso2,
|
|
206
|
-
phone_code: `+${country.phone}`,
|
|
207
|
-
flag: `flag flag-${(_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toLowerCase()}`,
|
|
208
|
-
phone_format: country.phone_format
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
return undefined;
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Retrieve all currencies of all available countries.
|
|
215
|
-
*
|
|
216
|
-
* @returns {Currency[]} - Array of currency objects.
|
|
217
|
-
*/
|
|
218
|
-
getCurrencies() {
|
|
219
|
-
return this.getCountries(['name', 'iso2', 'currency', 'currency_symbol', 'currency_name']).map(country => {
|
|
220
|
-
var _a;
|
|
221
|
-
return Object.assign(Object.assign({}, country), { flag: `flag flag-${(_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toLowerCase()}` });
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* Retrieve currency of a country by its ISO2 code.
|
|
226
|
-
*
|
|
227
|
-
* @param {string} iso2 - ISO2 code of the country.
|
|
228
|
-
* @returns {Currency | undefined} - Currency object or undefined if not found.
|
|
229
|
-
*/
|
|
230
|
-
currency(iso2) {
|
|
231
|
-
var _a;
|
|
232
|
-
const country = this.find(iso2);
|
|
233
|
-
if (country) {
|
|
234
|
-
return {
|
|
235
|
-
name: country.name,
|
|
236
|
-
iso2: country.iso2,
|
|
237
|
-
currency: country.currency,
|
|
238
|
-
currency_symbol: country.currency_symbol,
|
|
239
|
-
currency_name: country.currency_name,
|
|
240
|
-
flag: `flag flag-${(_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toLowerCase()}`
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
return undefined;
|
|
244
|
-
}
|
|
245
95
|
}
|
|
246
96
|
export default new CountriesAtlas();
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Country } from '../types/country.interface';
|
|
2
|
+
import { Currency } from '../types/currency.type';
|
|
3
|
+
import { PhoneCode } from '../types/phone-code.interface';
|
|
4
|
+
import { Timezone } from '../types/timezone.type';
|
|
5
|
+
/**
|
|
6
|
+
* Lightweight country-list helper.
|
|
7
|
+
*
|
|
8
|
+
* Loads ONLY the bundled `atlas.json` (the country list plus per-country
|
|
9
|
+
* metadata: names, translations, phone, currency, timezones). It deliberately
|
|
10
|
+
* does NOT import the per-country state/city loaders (`countryLoaders`), so
|
|
11
|
+
* importing this class never drags the ~171 MB of per-country state/city JSON
|
|
12
|
+
* into a consumer's bundle.
|
|
13
|
+
*
|
|
14
|
+
* Use this when you only need countries, phone codes, currencies or timezones
|
|
15
|
+
* — e.g. on a server-rendered path. When you need states/cities, use
|
|
16
|
+
* `CountriesAtlas` (which extends this) or the `@touchestate/geo-atlas/cities`
|
|
17
|
+
* entry point.
|
|
18
|
+
*/
|
|
19
|
+
export declare class CountriesData {
|
|
20
|
+
/** Internal countries array loaded from the bundled atlas.json. */
|
|
21
|
+
protected countries: Country[];
|
|
22
|
+
/** Initializes the atlas by loading the bundled country data. */
|
|
23
|
+
constructor();
|
|
24
|
+
/**
|
|
25
|
+
* Retrieve all countries with specified properties, or all countries if no properties are specified.
|
|
26
|
+
*
|
|
27
|
+
* @param {string[]} properties - Array of properties to retrieve.
|
|
28
|
+
* @return {Country[]} - Array of country objects with specified properties or all countries.
|
|
29
|
+
*/
|
|
30
|
+
getCountries(properties?: string[]): Country[];
|
|
31
|
+
/**
|
|
32
|
+
* Find a country by its ISO2 code.
|
|
33
|
+
*
|
|
34
|
+
* @param {string} iso2 - ISO2 code of the country to find.
|
|
35
|
+
* @return {Country | undefined} - Country object or undefined if not found.
|
|
36
|
+
*/
|
|
37
|
+
find(iso2: string): Country | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Find a country by its ISO3 code.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} iso3 - ISO3 code of the country to find.
|
|
42
|
+
* @return {Country | undefined} - Country object or undefined if not found.
|
|
43
|
+
*/
|
|
44
|
+
findByIso3(iso3: string): Country | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Retrieve all timezones of all available countries.
|
|
47
|
+
*
|
|
48
|
+
* @returns {Timezone[]} - Array of timezone objects.
|
|
49
|
+
*/
|
|
50
|
+
getTimezones(): Timezone[];
|
|
51
|
+
/**
|
|
52
|
+
* Retrieve all timezones of a country by its ISO2 code.
|
|
53
|
+
*
|
|
54
|
+
* @param {string} iso2 - ISO2 code of the country.
|
|
55
|
+
* @returns {Timezone[] | undefined} - Array of timezone objects or undefined if not found.
|
|
56
|
+
*/
|
|
57
|
+
timezone(iso2: string): Timezone[] | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Retrieve all calling codes of all available countries.
|
|
60
|
+
*
|
|
61
|
+
* @returns {PhoneCode[]} - Array of phone code objects.
|
|
62
|
+
*/
|
|
63
|
+
getCallingCodes(): PhoneCode[];
|
|
64
|
+
/**
|
|
65
|
+
* Retrieve calling codes of a country by its ISO2 code.
|
|
66
|
+
*
|
|
67
|
+
* @param {string} iso2 - ISO2 code of the country.
|
|
68
|
+
* @returns {PhoneCode | undefined} - Phone code object or undefined if not found.
|
|
69
|
+
*/
|
|
70
|
+
callingCode(iso2: string): PhoneCode | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* Retrieve all currencies of all available countries.
|
|
73
|
+
*
|
|
74
|
+
* @returns {Currency[]} - Array of currency objects.
|
|
75
|
+
*/
|
|
76
|
+
getCurrencies(): Currency[];
|
|
77
|
+
/**
|
|
78
|
+
* Retrieve currency of a country by its ISO2 code.
|
|
79
|
+
*
|
|
80
|
+
* @param {string} iso2 - ISO2 code of the country.
|
|
81
|
+
* @returns {Currency | undefined} - Currency object or undefined if not found.
|
|
82
|
+
*/
|
|
83
|
+
currency(iso2: string): Currency | undefined;
|
|
84
|
+
}
|
|
85
|
+
declare const _default: CountriesData;
|
|
86
|
+
export default _default;
|