@innovayse/geo-atlas 2.0.2 → 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.
@@ -1,38 +1,17 @@
1
- import { Country } from '../types/country.interface';
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
- * Main helper class for querying countries, states, cities, timezones,
8
- * calling codes and currencies from the offline geo-atlas dataset.
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
  *
@@ -41,7 +20,11 @@ export declare class CountriesAtlas {
41
20
  */
42
21
  getStates(iso2: string): State[] | undefined;
43
22
  /**
44
- * Retrieve all states of a country by its ISO2 code using dynamic import (browser-compatible).
23
+ * Retrieve all states of a country by its ISO2 code using a static loader registry.
24
+ *
25
+ * Uses explicit string-literal dynamic imports (via countryLoaders) so bundlers
26
+ * such as Vite, webpack, and rollup can statically analyze paths and code-split
27
+ * country data. Works in both Node.js (CJS/ESM) and browser environments.
45
28
  *
46
29
  * @param {string} iso2 - ISO2 code of the country.
47
30
  * @returns {Promise<State[]>} - Promise resolving to array of state objects.
@@ -55,45 +38,6 @@ export declare class CountriesAtlas {
55
38
  * @returns {State | undefined} - State object or undefined if not found.
56
39
  */
57
40
  state(iso2: string, stateCode: string): State | undefined;
58
- /**
59
- * Retrieve all timezones of all available countries.
60
- *
61
- * @returns {Timezone[]} - Array of timezone objects.
62
- */
63
- getTimezones(): Timezone[];
64
- /**
65
- * Retrieve all timezones of a country by its ISO2 code.
66
- *
67
- * @param {string} iso2 - ISO2 code of the country.
68
- * @returns {Timezone[] | undefined} - Array of timezone objects or undefined if not found.
69
- */
70
- timezone(iso2: string): Timezone[] | undefined;
71
- /**
72
- * Retrieve all calling codes of all available countries.
73
- *
74
- * @returns {PhoneCode[]} - Array of phone code objects.
75
- */
76
- getCallingCodes(): PhoneCode[];
77
- /**
78
- * Retrieve calling codes of a country by its ISO2 code.
79
- *
80
- * @param {string} iso2 - ISO2 code of the country.
81
- * @returns {PhoneCode | undefined} - Phone code object or undefined if not found.
82
- */
83
- callingCode(iso2: string): PhoneCode | undefined;
84
- /**
85
- * Retrieve all currencies of all available countries.
86
- *
87
- * @returns {Currency[]} - Array of currency objects.
88
- */
89
- getCurrencies(): Currency[];
90
- /**
91
- * Retrieve currency of a country by its ISO2 code.
92
- *
93
- * @param {string} iso2 - ISO2 code of the country.
94
- * @returns {Currency | undefined} - Currency object or undefined if not found.
95
- */
96
- currency(iso2: string): Currency | undefined;
97
41
  }
98
42
  declare const _default: CountriesAtlas;
99
43
  export default _default;
@@ -7,65 +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 countriesData from '../data/atlas.json';
10
+ import { CountriesData } from './CountriesData';
11
+ import countryLoaders from './countryLoaders';
11
12
  /**
12
- * Main helper class for querying countries, states, cities, timezones,
13
- * calling codes and currencies from the offline geo-atlas dataset.
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.
14
22
  */
15
- export class CountriesAtlas {
16
- /** Initializes the atlas by loading the bundled country data. */
17
- constructor() {
18
- this.countries = countriesData;
19
- }
20
- /**
21
- * Retrieve all countries with specified properties, or all countries if no properties are specified.
22
- *
23
- * @param {string[]} properties - Array of properties to retrieve.
24
- * @return {Country[]} - Array of country objects with specified properties or all countries.
25
- */
26
- getCountries(properties) {
27
- if (properties) {
28
- return this.countries.map(country => {
29
- const newCountry = {};
30
- properties.forEach(property => {
31
- newCountry[property] = country[property];
32
- });
33
- return newCountry;
34
- });
35
- }
36
- return this.countries;
37
- }
38
- /**
39
- * Find a country by its ISO2 code.
40
- *
41
- * @param {string} iso2 - ISO2 code of the country to find.
42
- * @return {Country | undefined} - Country object or undefined if not found.
43
- */
44
- find(iso2) {
45
- return this.countries.find(country => { var _a; return ((_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toUpperCase()) === iso2.toUpperCase(); });
46
- }
47
- /**
48
- * Find a country by its ISO3 code.
49
- *
50
- * @param {string} iso3 - ISO3 code of the country to find.
51
- * @return {Country | undefined} - Country object or undefined if not found.
52
- */
53
- findByIso3(iso3) {
54
- return this.countries.find(country => { var _a; return ((_a = country.iso3) === null || _a === void 0 ? void 0 : _a.toUpperCase()) === iso3.toUpperCase(); });
55
- }
56
- // getStates(iso2: string): Promise<State[]> | undefined {
57
- // const country = this.find(iso2)
58
- // if (country) {
59
- // return import(`../data/countries/${country.iso2?.toLowerCase()}.json`)
60
- // .then(statesData => {
61
- // return statesData.states
62
- // })
63
- // .catch(() => {
64
- // return undefined
65
- // })
66
- // }
67
- // return undefined
68
- // }
23
+ export class CountriesAtlas extends CountriesData {
69
24
  /**
70
25
  * Retrieve all states of a country by its ISO2 code.
71
26
  *
@@ -87,7 +42,11 @@ export class CountriesAtlas {
87
42
  return undefined;
88
43
  }
89
44
  /**
90
- * Retrieve all states of a country by its ISO2 code using dynamic import (browser-compatible).
45
+ * Retrieve all states of a country by its ISO2 code using a static loader registry.
46
+ *
47
+ * Uses explicit string-literal dynamic imports (via countryLoaders) so bundlers
48
+ * such as Vite, webpack, and rollup can statically analyze paths and code-split
49
+ * country data. Works in both Node.js (CJS/ESM) and browser environments.
91
50
  *
92
51
  * @param {string} iso2 - ISO2 code of the country.
93
52
  * @returns {Promise<State[]>} - Promise resolving to array of state objects.
@@ -96,10 +55,13 @@ export class CountriesAtlas {
96
55
  var _a, _b;
97
56
  return __awaiter(this, void 0, void 0, function* () {
98
57
  const country = this.find(iso2);
99
- if (!country || !country.iso2)
58
+ if (!(country === null || country === void 0 ? void 0 : country.iso2))
59
+ return [];
60
+ const loader = countryLoaders[country.iso2.toLowerCase()];
61
+ if (!loader)
100
62
  return [];
101
63
  try {
102
- const mod = yield import(`../data/countries/${country.iso2.toLowerCase()}.json`);
64
+ const mod = yield loader();
103
65
  const data = (_a = mod.default) !== null && _a !== void 0 ? _a : mod;
104
66
  return (_b = data.states) !== null && _b !== void 0 ? _b : [];
105
67
  }
@@ -108,18 +70,6 @@ export class CountriesAtlas {
108
70
  }
109
71
  });
110
72
  }
111
- // state(iso2: string, stateCode: string): Promise<State | undefined> | undefined {
112
- // const country = this.find(iso2);
113
- // if (country) {
114
- // return import(`../data/countries/${country.iso2?.toLowerCase()}.json`)
115
- // .then((statesData: StateData) => {
116
- // const state = statesData.states.find((s: State) => s.state_code?.toUpperCase() === stateCode);
117
- // return state ? state : undefined;
118
- // })
119
- // .catch(() => undefined);
120
- // }
121
- // return undefined;
122
- // }
123
73
  /**
124
74
  * Find a state by its state code and country ISO2 code.
125
75
  *
@@ -142,96 +92,5 @@ export class CountriesAtlas {
142
92
  }
143
93
  return undefined;
144
94
  }
145
- /**
146
- * Retrieve all timezones of all available countries.
147
- *
148
- * @returns {Timezone[]} - Array of timezone objects.
149
- */
150
- getTimezones() {
151
- const timezoneSet = new Set();
152
- this.countries.forEach(country => {
153
- var _a;
154
- (_a = country.timezones) === null || _a === void 0 ? void 0 : _a.forEach(timezone => {
155
- timezoneSet.add(timezone);
156
- });
157
- });
158
- return Array.from(timezoneSet);
159
- }
160
- /**
161
- * Retrieve all timezones of a country by its ISO2 code.
162
- *
163
- * @param {string} iso2 - ISO2 code of the country.
164
- * @returns {Timezone[] | undefined} - Array of timezone objects or undefined if not found.
165
- */
166
- timezone(iso2) {
167
- const country = this.find(iso2);
168
- if (country) {
169
- return country.timezones;
170
- }
171
- return undefined;
172
- }
173
- /**
174
- * Retrieve all calling codes of all available countries.
175
- *
176
- * @returns {PhoneCode[]} - Array of phone code objects.
177
- */
178
- getCallingCodes() {
179
- return this.getCountries(['name', 'phone', 'iso2']).map(country => {
180
- var _a;
181
- return Object.assign(Object.assign({}, country), { phone_code: `+${country.phone}`, flag: `flag flag-${(_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toLowerCase()}` });
182
- });
183
- }
184
- /**
185
- * Retrieve calling codes of a country by its ISO2 code.
186
- *
187
- * @param {string} iso2 - ISO2 code of the country.
188
- * @returns {PhoneCode | undefined} - Phone code object or undefined if not found.
189
- */
190
- callingCode(iso2) {
191
- var _a;
192
- const country = this.find(iso2);
193
- if (country) {
194
- return {
195
- name: country.name,
196
- phone: country.phone,
197
- iso2: country.iso2,
198
- phone_code: `+${country.phone}`,
199
- flag: `flag flag-${(_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toLowerCase()}`
200
- };
201
- }
202
- return undefined;
203
- }
204
- /**
205
- * Retrieve all currencies of all available countries.
206
- *
207
- * @returns {Currency[]} - Array of currency objects.
208
- */
209
- getCurrencies() {
210
- return this.getCountries(['name', 'iso2', 'currency', 'currency_symbol', 'currency_name']).map(country => {
211
- var _a;
212
- return Object.assign(Object.assign({}, country), { flag: `flag flag-${(_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toLowerCase()}` });
213
- });
214
- }
215
- /**
216
- * Retrieve currency of a country by its ISO2 code.
217
- *
218
- * @param {string} iso2 - ISO2 code of the country.
219
- * @returns {Currency | undefined} - Currency object or undefined if not found.
220
- */
221
- currency(iso2) {
222
- var _a;
223
- const country = this.find(iso2);
224
- if (country) {
225
- return {
226
- name: country.name,
227
- iso2: country.iso2,
228
- currency: country.currency,
229
- currency_symbol: country.currency_symbol,
230
- currency_name: country.currency_name,
231
- flag: `flag flag-${(_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toLowerCase()}`
232
- };
233
- }
234
- return undefined;
235
- }
236
95
  }
237
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;
@@ -0,0 +1,150 @@
1
+ import countriesData from '../data/atlas.json';
2
+ /**
3
+ * Lightweight country-list helper.
4
+ *
5
+ * Loads ONLY the bundled `atlas.json` (the country list plus per-country
6
+ * metadata: names, translations, phone, currency, timezones). It deliberately
7
+ * does NOT import the per-country state/city loaders (`countryLoaders`), so
8
+ * importing this class never drags the ~171 MB of per-country state/city JSON
9
+ * into a consumer's bundle.
10
+ *
11
+ * Use this when you only need countries, phone codes, currencies or timezones
12
+ * — e.g. on a server-rendered path. When you need states/cities, use
13
+ * `CountriesAtlas` (which extends this) or the `@touchestate/geo-atlas/cities`
14
+ * entry point.
15
+ */
16
+ export class CountriesData {
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
+ /**
58
+ * Retrieve all timezones of all available countries.
59
+ *
60
+ * @returns {Timezone[]} - Array of timezone objects.
61
+ */
62
+ getTimezones() {
63
+ const timezoneSet = new Set();
64
+ this.countries.forEach(country => {
65
+ var _a;
66
+ (_a = country.timezones) === null || _a === void 0 ? void 0 : _a.forEach(timezone => {
67
+ timezoneSet.add(timezone);
68
+ });
69
+ });
70
+ return Array.from(timezoneSet);
71
+ }
72
+ /**
73
+ * Retrieve all timezones of a country by its ISO2 code.
74
+ *
75
+ * @param {string} iso2 - ISO2 code of the country.
76
+ * @returns {Timezone[] | undefined} - Array of timezone objects or undefined if not found.
77
+ */
78
+ timezone(iso2) {
79
+ const country = this.find(iso2);
80
+ if (country) {
81
+ return country.timezones;
82
+ }
83
+ return undefined;
84
+ }
85
+ /**
86
+ * Retrieve all calling codes of all available countries.
87
+ *
88
+ * @returns {PhoneCode[]} - Array of phone code objects.
89
+ */
90
+ getCallingCodes() {
91
+ return this.getCountries(['name', 'phone', 'iso2', 'phone_format']).map(country => {
92
+ var _a;
93
+ return Object.assign(Object.assign({}, country), { phone_code: `+${country.phone}`, flag: `flag flag-${(_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toLowerCase()}` });
94
+ });
95
+ }
96
+ /**
97
+ * Retrieve calling codes of a country by its ISO2 code.
98
+ *
99
+ * @param {string} iso2 - ISO2 code of the country.
100
+ * @returns {PhoneCode | undefined} - Phone code object or undefined if not found.
101
+ */
102
+ callingCode(iso2) {
103
+ var _a;
104
+ const country = this.find(iso2);
105
+ if (country) {
106
+ return {
107
+ name: country.name,
108
+ phone: country.phone,
109
+ iso2: country.iso2,
110
+ phone_code: `+${country.phone}`,
111
+ flag: `flag flag-${(_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toLowerCase()}`,
112
+ phone_format: country.phone_format
113
+ };
114
+ }
115
+ return undefined;
116
+ }
117
+ /**
118
+ * Retrieve all currencies of all available countries.
119
+ *
120
+ * @returns {Currency[]} - Array of currency objects.
121
+ */
122
+ getCurrencies() {
123
+ return this.getCountries(['name', 'iso2', 'currency', 'currency_symbol', 'currency_name']).map(country => {
124
+ var _a;
125
+ return Object.assign(Object.assign({}, country), { flag: `flag flag-${(_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toLowerCase()}` });
126
+ });
127
+ }
128
+ /**
129
+ * Retrieve currency of a country by its ISO2 code.
130
+ *
131
+ * @param {string} iso2 - ISO2 code of the country.
132
+ * @returns {Currency | undefined} - Currency object or undefined if not found.
133
+ */
134
+ currency(iso2) {
135
+ var _a;
136
+ const country = this.find(iso2);
137
+ if (country) {
138
+ return {
139
+ name: country.name,
140
+ iso2: country.iso2,
141
+ currency: country.currency,
142
+ currency_symbol: country.currency_symbol,
143
+ currency_name: country.currency_name,
144
+ flag: `flag flag-${(_a = country.iso2) === null || _a === void 0 ? void 0 : _a.toLowerCase()}`
145
+ };
146
+ }
147
+ return undefined;
148
+ }
149
+ }
150
+ export default new CountriesData();
@@ -0,0 +1,17 @@
1
+ import type { State } from '../types/state.interface';
2
+ /**
3
+ * Auto-generated static loader registry for per-country state/city data.
4
+ *
5
+ * Each entry uses an explicit string-literal dynamic import path, allowing
6
+ * bundlers (Vite, webpack, rollup) to statically analyze, bundle, and
7
+ * code-split country data. DO NOT EDIT MANUALLY.
8
+ *
9
+ * Regenerate with: npm run generate:loaders
10
+ */
11
+ declare const countryLoaders: Record<string, () => Promise<{
12
+ default?: {
13
+ states: State[];
14
+ };
15
+ states?: State[];
16
+ }>>;
17
+ export default countryLoaders;