@etsoo/appscript 1.0.97 → 1.1.1

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.
@@ -35,12 +35,12 @@ AddressRegion.CN = new AddressRegion('CN', 'CHN', '156', AddressContinent_1.Addr
35
35
  * HK - HK, China
36
36
  * 中国香港
37
37
  */
38
- AddressRegion.HK = new AddressRegion('HK', 'HKG', '344', AddressContinent_1.AddressContinent.AS, '001', '852', 'HKD', ['zh-HK']);
38
+ AddressRegion.HK = new AddressRegion('HK', 'HKG', '344', AddressContinent_1.AddressContinent.AS, '001', '852', 'HKD', ['zh-HK', 'en-HK']);
39
39
  /**
40
40
  * SG - Singapore
41
41
  * 新加坡
42
42
  */
43
- AddressRegion.SG = new AddressRegion('SG', 'SGP', '702', AddressContinent_1.AddressContinent.AS, '000', '65', 'SGD', ['zh-SG']);
43
+ AddressRegion.SG = new AddressRegion('SG', 'SGP', '702', AddressContinent_1.AddressContinent.AS, '000', '65', 'SGD', ['zh-SG', 'en-SG']);
44
44
  /**
45
45
  * JP - Japan
46
46
  * 日本
@@ -1,3 +1,4 @@
1
+ import { AddressRegion } from '..';
1
2
  import { IdLabelDto } from '../dto/IdLabelDto';
2
3
  import { ICultureGet } from '../state/Culture';
3
4
  /**
@@ -11,4 +12,11 @@ export declare namespace AddressUtils {
11
12
  * @returns Continents
12
13
  */
13
14
  function getContinents(func: ICultureGet, isNumberKey?: boolean): IdLabelDto[];
15
+ /**
16
+ * Get region
17
+ * @param regions Supported regions
18
+ * @param detectedRegion Detected region
19
+ * @param detectedLanguage Detected language
20
+ */
21
+ function getRegion(regions: string[], detectedRegion?: string | null, detectedLanguage?: string | null): AddressRegion;
14
22
  }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AddressUtils = void 0;
4
4
  const shared_1 = require("@etsoo/shared");
5
+ const __1 = require("..");
5
6
  const AddressContinent_1 = require("./AddressContinent");
6
7
  /**
7
8
  * Address utils
@@ -26,4 +27,29 @@ var AddressUtils;
26
27
  });
27
28
  }
28
29
  AddressUtils.getContinents = getContinents;
30
+ /**
31
+ * Get region
32
+ * @param regions Supported regions
33
+ * @param detectedRegion Detected region
34
+ * @param detectedLanguage Detected language
35
+ */
36
+ function getRegion(regions, detectedRegion, detectedLanguage) {
37
+ // Exactly match
38
+ if (detectedRegion && regions.includes(detectedRegion)) {
39
+ const region = __1.AddressRegion.getById(detectedRegion);
40
+ if (region)
41
+ return region;
42
+ }
43
+ // Match with language
44
+ if (detectedLanguage) {
45
+ const region = regions
46
+ .map((id) => __1.AddressRegion.getById(id))
47
+ .find((item) => item.languages.includes(detectedLanguage));
48
+ if (region)
49
+ region;
50
+ }
51
+ // Default
52
+ return __1.AddressRegion.getById(regions[0]);
53
+ }
54
+ AddressUtils.getRegion = getRegion;
29
55
  })(AddressUtils = exports.AddressUtils || (exports.AddressUtils = {}));
@@ -30,15 +30,15 @@ export interface ICoreApp<S extends IAppSettings, N> {
30
30
  /**
31
31
  * Culture, like zh-CN
32
32
  */
33
- readonly culture: string | undefined;
33
+ readonly culture: string;
34
34
  /**
35
35
  * Currency, like USD for US dollar
36
36
  */
37
- readonly currency: string | undefined;
37
+ readonly currency: string;
38
38
  /**
39
39
  * Country or region, like CN
40
40
  */
41
- readonly region: string | undefined;
41
+ readonly region: string;
42
42
  /**
43
43
  * IP data
44
44
  */
@@ -65,9 +65,9 @@ export interface ICoreApp<S extends IAppSettings, N> {
65
65
  authorize(token?: string, refreshToken?: string, keep?: boolean): void;
66
66
  /**
67
67
  * Change country or region
68
- * @param regionId New country or region id
68
+ * @param region New country or region
69
69
  */
70
- changeRegion(regionId: string): void;
70
+ changeRegion(region: string | AddressRegion): void;
71
71
  /**
72
72
  * Change culture
73
73
  * @param culture New culture definition
@@ -119,13 +119,6 @@ export interface ICoreApp<S extends IAppSettings, N> {
119
119
  * @returns Cached token
120
120
  */
121
121
  getCacheToken(): string | null;
122
- /**
123
- * Get region id
124
- * @param regionId Country or region id
125
- * @param culture Culture, like zh-CN
126
- * @returns Matched region id
127
- */
128
- getRegion(regionId: string | undefined | null, culture: string | undefined | null): string;
129
122
  /**
130
123
  * Get all regions
131
124
  * @returns Regions
@@ -180,21 +173,21 @@ export declare abstract class CoreApp<S extends IAppSettings, N> implements ICor
180
173
  * Notifier
181
174
  */
182
175
  readonly notifier: INotifier<N>;
183
- private _culture?;
176
+ private _culture;
184
177
  /**
185
178
  * Culture, like zh-CN
186
179
  */
187
- get culture(): string | undefined;
188
- private _currency?;
180
+ get culture(): string;
181
+ private _currency;
189
182
  /**
190
183
  * Currency, like USD for US dollar
191
184
  */
192
- get currency(): string | undefined;
193
- private _region?;
185
+ get currency(): string;
186
+ private _region;
194
187
  /**
195
188
  * Country or region, like CN
196
189
  */
197
- get region(): string | undefined;
190
+ get region(): string;
198
191
  /**
199
192
  * IP data
200
193
  */
@@ -233,9 +226,9 @@ export declare abstract class CoreApp<S extends IAppSettings, N> implements ICor
233
226
  authorize(token?: string, refreshToken?: string, keep?: boolean): void;
234
227
  /**
235
228
  * Change country or region
236
- * @param regionId New country or region id
229
+ * @param regionId New country or region
237
230
  */
238
- changeRegion(regionId: string): void;
231
+ changeRegion(region: string | AddressRegion): void;
239
232
  /**
240
233
  * Change culture
241
234
  * @param culture New culture definition
@@ -294,13 +287,6 @@ export declare abstract class CoreApp<S extends IAppSettings, N> implements ICor
294
287
  * @returns Cached token
295
288
  */
296
289
  getCacheToken(): string | null;
297
- /**
298
- * Get region id
299
- * @param regionId Country or region id
300
- * @param culture Culture, like zh-CN
301
- * @returns Matched region id
302
- */
303
- getRegion(regionId: string | undefined | null, culture: string | undefined | null): string;
304
290
  /**
305
291
  * Get all regions
306
292
  * @returns Regions
@@ -49,6 +49,9 @@ class CoreApp {
49
49
  this.settings = settings;
50
50
  this.api = api;
51
51
  this.notifier = notifier;
52
+ const { currentCulture, currentRegion } = settings;
53
+ this.changeCulture(currentCulture);
54
+ this.changeRegion(currentRegion);
52
55
  // Setup callback
53
56
  this.setup();
54
57
  }
@@ -91,26 +94,33 @@ class CoreApp {
91
94
  }
92
95
  /**
93
96
  * Change country or region
94
- * @param regionId New country or region id
97
+ * @param regionId New country or region
95
98
  */
96
- changeRegion(regionId) {
97
- // Same?
99
+ changeRegion(region) {
100
+ // Get data
101
+ let regionId;
102
+ let regionItem;
103
+ if (typeof region === 'string') {
104
+ regionId = region;
105
+ regionItem = AddressRegion_1.AddressRegion.getById(region);
106
+ }
107
+ else {
108
+ regionId = region.id;
109
+ regionItem = region;
110
+ }
111
+ // Same
98
112
  if (regionId === this._region)
99
113
  return;
100
- // Exists in settings
101
- if (!this.settings.regions.includes(regionId))
102
- return;
103
- // Region
104
- const region = AddressRegion_1.AddressRegion.getById(regionId);
105
- if (region == null)
114
+ // Not included
115
+ if (regionItem == null || !this.settings.regions.includes(regionId))
106
116
  return;
107
117
  // Save the id to local storage
108
118
  shared_1.DomUtils.saveCountry(regionId);
109
119
  // Set the currency and culture
110
- this._culture = region.currency;
120
+ this._currency = regionItem.currency;
111
121
  this._region = regionId;
112
122
  // Hold the current country or region
113
- this.settings.currentRegion = region;
123
+ this.settings.currentRegion = regionItem;
114
124
  }
115
125
  /**
116
126
  * Change culture
@@ -119,6 +129,9 @@ class CoreApp {
119
129
  changeCulture(culture) {
120
130
  // Name
121
131
  const { name } = culture;
132
+ // Same?
133
+ if (this._culture === name)
134
+ return;
122
135
  // Save the cultrue to local storage
123
136
  shared_1.DomUtils.saveCulture(name);
124
137
  // Change the API's Content-Language header
@@ -182,7 +195,7 @@ class CoreApp {
182
195
  * @returns Result
183
196
  */
184
197
  formatMoney(input, isInteger = false, options) {
185
- return shared_1.NumberUtils.formatMoney(input, this.region, this.culture, isInteger, options);
198
+ return shared_1.NumberUtils.formatMoney(input, this.currency, this.culture, isInteger, options);
186
199
  }
187
200
  /**
188
201
  * Format number
@@ -232,28 +245,6 @@ class CoreApp {
232
245
  return null;
233
246
  return refreshToken;
234
247
  }
235
- /**
236
- * Get region id
237
- * @param regionId Country or region id
238
- * @param culture Culture, like zh-CN
239
- * @returns Matched region id
240
- */
241
- getRegion(regionId, culture) {
242
- // Exactly match
243
- if (regionId && this.settings.regions.includes(regionId)) {
244
- return regionId;
245
- }
246
- // Match with language
247
- if (culture) {
248
- const region = this.settings.regions
249
- .map((id) => AddressRegion_1.AddressRegion.getById(id))
250
- .find((item) => item.languages.includes(culture));
251
- if (region)
252
- return region.id;
253
- }
254
- // Default
255
- return this.settings.regions[0];
256
- }
257
248
  /**
258
249
  * Get all regions
259
250
  * @returns Regions
@@ -31,12 +31,12 @@ AddressRegion.CN = new AddressRegion('CN', 'CHN', '156', AddressContinent.AS, '0
31
31
  * HK - HK, China
32
32
  * 中国香港
33
33
  */
34
- AddressRegion.HK = new AddressRegion('HK', 'HKG', '344', AddressContinent.AS, '001', '852', 'HKD', ['zh-HK']);
34
+ AddressRegion.HK = new AddressRegion('HK', 'HKG', '344', AddressContinent.AS, '001', '852', 'HKD', ['zh-HK', 'en-HK']);
35
35
  /**
36
36
  * SG - Singapore
37
37
  * 新加坡
38
38
  */
39
- AddressRegion.SG = new AddressRegion('SG', 'SGP', '702', AddressContinent.AS, '000', '65', 'SGD', ['zh-SG']);
39
+ AddressRegion.SG = new AddressRegion('SG', 'SGP', '702', AddressContinent.AS, '000', '65', 'SGD', ['zh-SG', 'en-SG']);
40
40
  /**
41
41
  * JP - Japan
42
42
  * 日本
@@ -1,3 +1,4 @@
1
+ import { AddressRegion } from '..';
1
2
  import { IdLabelDto } from '../dto/IdLabelDto';
2
3
  import { ICultureGet } from '../state/Culture';
3
4
  /**
@@ -11,4 +12,11 @@ export declare namespace AddressUtils {
11
12
  * @returns Continents
12
13
  */
13
14
  function getContinents(func: ICultureGet, isNumberKey?: boolean): IdLabelDto[];
15
+ /**
16
+ * Get region
17
+ * @param regions Supported regions
18
+ * @param detectedRegion Detected region
19
+ * @param detectedLanguage Detected language
20
+ */
21
+ function getRegion(regions: string[], detectedRegion?: string | null, detectedLanguage?: string | null): AddressRegion;
14
22
  }
@@ -1,4 +1,5 @@
1
1
  import { Utils } from '@etsoo/shared';
2
+ import { AddressRegion } from '..';
2
3
  import { AddressContinent } from './AddressContinent';
3
4
  /**
4
5
  * Address utils
@@ -23,4 +24,29 @@ export var AddressUtils;
23
24
  });
24
25
  }
25
26
  AddressUtils.getContinents = getContinents;
27
+ /**
28
+ * Get region
29
+ * @param regions Supported regions
30
+ * @param detectedRegion Detected region
31
+ * @param detectedLanguage Detected language
32
+ */
33
+ function getRegion(regions, detectedRegion, detectedLanguage) {
34
+ // Exactly match
35
+ if (detectedRegion && regions.includes(detectedRegion)) {
36
+ const region = AddressRegion.getById(detectedRegion);
37
+ if (region)
38
+ return region;
39
+ }
40
+ // Match with language
41
+ if (detectedLanguage) {
42
+ const region = regions
43
+ .map((id) => AddressRegion.getById(id))
44
+ .find((item) => item.languages.includes(detectedLanguage));
45
+ if (region)
46
+ region;
47
+ }
48
+ // Default
49
+ return AddressRegion.getById(regions[0]);
50
+ }
51
+ AddressUtils.getRegion = getRegion;
26
52
  })(AddressUtils || (AddressUtils = {}));
@@ -30,15 +30,15 @@ export interface ICoreApp<S extends IAppSettings, N> {
30
30
  /**
31
31
  * Culture, like zh-CN
32
32
  */
33
- readonly culture: string | undefined;
33
+ readonly culture: string;
34
34
  /**
35
35
  * Currency, like USD for US dollar
36
36
  */
37
- readonly currency: string | undefined;
37
+ readonly currency: string;
38
38
  /**
39
39
  * Country or region, like CN
40
40
  */
41
- readonly region: string | undefined;
41
+ readonly region: string;
42
42
  /**
43
43
  * IP data
44
44
  */
@@ -65,9 +65,9 @@ export interface ICoreApp<S extends IAppSettings, N> {
65
65
  authorize(token?: string, refreshToken?: string, keep?: boolean): void;
66
66
  /**
67
67
  * Change country or region
68
- * @param regionId New country or region id
68
+ * @param region New country or region
69
69
  */
70
- changeRegion(regionId: string): void;
70
+ changeRegion(region: string | AddressRegion): void;
71
71
  /**
72
72
  * Change culture
73
73
  * @param culture New culture definition
@@ -119,13 +119,6 @@ export interface ICoreApp<S extends IAppSettings, N> {
119
119
  * @returns Cached token
120
120
  */
121
121
  getCacheToken(): string | null;
122
- /**
123
- * Get region id
124
- * @param regionId Country or region id
125
- * @param culture Culture, like zh-CN
126
- * @returns Matched region id
127
- */
128
- getRegion(regionId: string | undefined | null, culture: string | undefined | null): string;
129
122
  /**
130
123
  * Get all regions
131
124
  * @returns Regions
@@ -180,21 +173,21 @@ export declare abstract class CoreApp<S extends IAppSettings, N> implements ICor
180
173
  * Notifier
181
174
  */
182
175
  readonly notifier: INotifier<N>;
183
- private _culture?;
176
+ private _culture;
184
177
  /**
185
178
  * Culture, like zh-CN
186
179
  */
187
- get culture(): string | undefined;
188
- private _currency?;
180
+ get culture(): string;
181
+ private _currency;
189
182
  /**
190
183
  * Currency, like USD for US dollar
191
184
  */
192
- get currency(): string | undefined;
193
- private _region?;
185
+ get currency(): string;
186
+ private _region;
194
187
  /**
195
188
  * Country or region, like CN
196
189
  */
197
- get region(): string | undefined;
190
+ get region(): string;
198
191
  /**
199
192
  * IP data
200
193
  */
@@ -233,9 +226,9 @@ export declare abstract class CoreApp<S extends IAppSettings, N> implements ICor
233
226
  authorize(token?: string, refreshToken?: string, keep?: boolean): void;
234
227
  /**
235
228
  * Change country or region
236
- * @param regionId New country or region id
229
+ * @param regionId New country or region
237
230
  */
238
- changeRegion(regionId: string): void;
231
+ changeRegion(region: string | AddressRegion): void;
239
232
  /**
240
233
  * Change culture
241
234
  * @param culture New culture definition
@@ -294,13 +287,6 @@ export declare abstract class CoreApp<S extends IAppSettings, N> implements ICor
294
287
  * @returns Cached token
295
288
  */
296
289
  getCacheToken(): string | null;
297
- /**
298
- * Get region id
299
- * @param regionId Country or region id
300
- * @param culture Culture, like zh-CN
301
- * @returns Matched region id
302
- */
303
- getRegion(regionId: string | undefined | null, culture: string | undefined | null): string;
304
290
  /**
305
291
  * Get all regions
306
292
  * @returns Regions
@@ -46,6 +46,9 @@ export class CoreApp {
46
46
  this.settings = settings;
47
47
  this.api = api;
48
48
  this.notifier = notifier;
49
+ const { currentCulture, currentRegion } = settings;
50
+ this.changeCulture(currentCulture);
51
+ this.changeRegion(currentRegion);
49
52
  // Setup callback
50
53
  this.setup();
51
54
  }
@@ -88,26 +91,33 @@ export class CoreApp {
88
91
  }
89
92
  /**
90
93
  * Change country or region
91
- * @param regionId New country or region id
94
+ * @param regionId New country or region
92
95
  */
93
- changeRegion(regionId) {
94
- // Same?
96
+ changeRegion(region) {
97
+ // Get data
98
+ let regionId;
99
+ let regionItem;
100
+ if (typeof region === 'string') {
101
+ regionId = region;
102
+ regionItem = AddressRegion.getById(region);
103
+ }
104
+ else {
105
+ regionId = region.id;
106
+ regionItem = region;
107
+ }
108
+ // Same
95
109
  if (regionId === this._region)
96
110
  return;
97
- // Exists in settings
98
- if (!this.settings.regions.includes(regionId))
99
- return;
100
- // Region
101
- const region = AddressRegion.getById(regionId);
102
- if (region == null)
111
+ // Not included
112
+ if (regionItem == null || !this.settings.regions.includes(regionId))
103
113
  return;
104
114
  // Save the id to local storage
105
115
  DomUtils.saveCountry(regionId);
106
116
  // Set the currency and culture
107
- this._culture = region.currency;
117
+ this._currency = regionItem.currency;
108
118
  this._region = regionId;
109
119
  // Hold the current country or region
110
- this.settings.currentRegion = region;
120
+ this.settings.currentRegion = regionItem;
111
121
  }
112
122
  /**
113
123
  * Change culture
@@ -116,6 +126,9 @@ export class CoreApp {
116
126
  changeCulture(culture) {
117
127
  // Name
118
128
  const { name } = culture;
129
+ // Same?
130
+ if (this._culture === name)
131
+ return;
119
132
  // Save the cultrue to local storage
120
133
  DomUtils.saveCulture(name);
121
134
  // Change the API's Content-Language header
@@ -179,7 +192,7 @@ export class CoreApp {
179
192
  * @returns Result
180
193
  */
181
194
  formatMoney(input, isInteger = false, options) {
182
- return NumberUtils.formatMoney(input, this.region, this.culture, isInteger, options);
195
+ return NumberUtils.formatMoney(input, this.currency, this.culture, isInteger, options);
183
196
  }
184
197
  /**
185
198
  * Format number
@@ -229,28 +242,6 @@ export class CoreApp {
229
242
  return null;
230
243
  return refreshToken;
231
244
  }
232
- /**
233
- * Get region id
234
- * @param regionId Country or region id
235
- * @param culture Culture, like zh-CN
236
- * @returns Matched region id
237
- */
238
- getRegion(regionId, culture) {
239
- // Exactly match
240
- if (regionId && this.settings.regions.includes(regionId)) {
241
- return regionId;
242
- }
243
- // Match with language
244
- if (culture) {
245
- const region = this.settings.regions
246
- .map((id) => AddressRegion.getById(id))
247
- .find((item) => item.languages.includes(culture));
248
- if (region)
249
- return region.id;
250
- }
251
- // Default
252
- return this.settings.regions[0];
253
- }
254
245
  /**
255
246
  * Get all regions
256
247
  * @returns Regions
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/appscript",
3
- "version": "1.0.97",
3
+ "version": "1.1.1",
4
4
  "description": "Applications shared TypeScript framework",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -49,7 +49,7 @@
49
49
  "dependencies": {
50
50
  "@etsoo/notificationbase": "^1.0.78",
51
51
  "@etsoo/restclient": "^1.0.51",
52
- "@etsoo/shared": "^1.0.44"
52
+ "@etsoo/shared": "^1.0.46"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@babel/cli": "^7.15.7",
@@ -90,7 +90,7 @@ export class AddressRegion implements IAddressRegion {
90
90
  '001',
91
91
  '852',
92
92
  'HKD',
93
- ['zh-HK']
93
+ ['zh-HK', 'en-HK']
94
94
  );
95
95
 
96
96
  /**
@@ -105,7 +105,7 @@ export class AddressRegion implements IAddressRegion {
105
105
  '000',
106
106
  '65',
107
107
  'SGD',
108
- ['zh-SG']
108
+ ['zh-SG', 'en-SG']
109
109
  );
110
110
 
111
111
  /**
@@ -1,4 +1,5 @@
1
1
  import { Utils } from '@etsoo/shared';
2
+ import { AddressRegion } from '..';
2
3
  import { IdLabelDto } from '../dto/IdLabelDto';
3
4
  import { ICultureGet } from '../state/Culture';
4
5
  import { AddressContinent } from './AddressContinent';
@@ -24,4 +25,33 @@ export namespace AddressUtils {
24
25
  label: func('continent' + key) ?? key
25
26
  }));
26
27
  }
28
+
29
+ /**
30
+ * Get region
31
+ * @param regions Supported regions
32
+ * @param detectedRegion Detected region
33
+ * @param detectedLanguage Detected language
34
+ */
35
+ export function getRegion(
36
+ regions: string[],
37
+ detectedRegion?: string | null,
38
+ detectedLanguage?: string | null
39
+ ): AddressRegion {
40
+ // Exactly match
41
+ if (detectedRegion && regions.includes(detectedRegion)) {
42
+ const region = AddressRegion.getById(detectedRegion);
43
+ if (region) return region;
44
+ }
45
+
46
+ // Match with language
47
+ if (detectedLanguage) {
48
+ const region = regions
49
+ .map((id) => AddressRegion.getById(id)!)
50
+ .find((item) => item.languages.includes(detectedLanguage));
51
+ if (region) region;
52
+ }
53
+
54
+ // Default
55
+ return AddressRegion.getById(regions[0])!;
56
+ }
27
57
  }
@@ -42,17 +42,17 @@ export interface ICoreApp<S extends IAppSettings, N> {
42
42
  /**
43
43
  * Culture, like zh-CN
44
44
  */
45
- readonly culture: string | undefined;
45
+ readonly culture: string;
46
46
 
47
47
  /**
48
48
  * Currency, like USD for US dollar
49
49
  */
50
- readonly currency: string | undefined;
50
+ readonly currency: string;
51
51
 
52
52
  /**
53
53
  * Country or region, like CN
54
54
  */
55
- readonly region: string | undefined;
55
+ readonly region: string;
56
56
 
57
57
  /**
58
58
  * IP data
@@ -85,9 +85,9 @@ export interface ICoreApp<S extends IAppSettings, N> {
85
85
 
86
86
  /**
87
87
  * Change country or region
88
- * @param regionId New country or region id
88
+ * @param region New country or region
89
89
  */
90
- changeRegion(regionId: string): void;
90
+ changeRegion(region: string | AddressRegion): void;
91
91
 
92
92
  /**
93
93
  * Change culture
@@ -157,17 +157,6 @@ export interface ICoreApp<S extends IAppSettings, N> {
157
157
  */
158
158
  getCacheToken(): string | null;
159
159
 
160
- /**
161
- * Get region id
162
- * @param regionId Country or region id
163
- * @param culture Culture, like zh-CN
164
- * @returns Matched region id
165
- */
166
- getRegion(
167
- regionId: string | undefined | null,
168
- culture: string | undefined | null
169
- ): string;
170
-
171
160
  /**
172
161
  * Get all regions
173
162
  * @returns Regions
@@ -234,8 +223,7 @@ export abstract class CoreApp<S extends IAppSettings, N>
234
223
  */
235
224
  readonly notifier: INotifier<N>;
236
225
 
237
- private _culture?: string;
238
-
226
+ private _culture!: string;
239
227
  /**
240
228
  * Culture, like zh-CN
241
229
  */
@@ -243,8 +231,7 @@ export abstract class CoreApp<S extends IAppSettings, N>
243
231
  return this._culture;
244
232
  }
245
233
 
246
- private _currency?: string;
247
-
234
+ private _currency!: string;
248
235
  /**
249
236
  * Currency, like USD for US dollar
250
237
  */
@@ -252,8 +239,7 @@ export abstract class CoreApp<S extends IAppSettings, N>
252
239
  return this._currency;
253
240
  }
254
241
 
255
- private _region?: string;
256
-
242
+ private _region!: string;
257
243
  /**
258
244
  * Country or region, like CN
259
245
  */
@@ -325,6 +311,10 @@ export abstract class CoreApp<S extends IAppSettings, N>
325
311
  this.api = api;
326
312
  this.notifier = notifier;
327
313
 
314
+ const { currentCulture, currentRegion } = settings;
315
+ this.changeCulture(currentCulture);
316
+ this.changeRegion(currentRegion);
317
+
328
318
  // Setup callback
329
319
  this.setup();
330
320
  }
@@ -359,28 +349,36 @@ export abstract class CoreApp<S extends IAppSettings, N>
359
349
 
360
350
  /**
361
351
  * Change country or region
362
- * @param regionId New country or region id
363
- */
364
- changeRegion(regionId: string) {
365
- // Same?
366
- if (regionId === this._region) return;
352
+ * @param regionId New country or region
353
+ */
354
+ changeRegion(region: string | AddressRegion) {
355
+ // Get data
356
+ let regionId: string;
357
+ let regionItem: AddressRegion | undefined;
358
+ if (typeof region === 'string') {
359
+ regionId = region;
360
+ regionItem = AddressRegion.getById(region);
361
+ } else {
362
+ regionId = region.id;
363
+ regionItem = region;
364
+ }
367
365
 
368
- // Exists in settings
369
- if (!this.settings.regions.includes(regionId)) return;
366
+ // Same
367
+ if (regionId === this._region) return;
370
368
 
371
- // Region
372
- const region = AddressRegion.getById(regionId);
373
- if (region == null) return;
369
+ // Not included
370
+ if (regionItem == null || !this.settings.regions.includes(regionId))
371
+ return;
374
372
 
375
373
  // Save the id to local storage
376
374
  DomUtils.saveCountry(regionId);
377
375
 
378
376
  // Set the currency and culture
379
- this._culture = region.currency;
377
+ this._currency = regionItem.currency;
380
378
  this._region = regionId;
381
379
 
382
380
  // Hold the current country or region
383
- this.settings.currentRegion = region;
381
+ this.settings.currentRegion = regionItem;
384
382
  }
385
383
 
386
384
  /**
@@ -391,6 +389,9 @@ export abstract class CoreApp<S extends IAppSettings, N>
391
389
  // Name
392
390
  const { name } = culture;
393
391
 
392
+ // Same?
393
+ if (this._culture === name) return;
394
+
394
395
  // Save the cultrue to local storage
395
396
  DomUtils.saveCulture(name);
396
397
 
@@ -476,7 +477,7 @@ export abstract class CoreApp<S extends IAppSettings, N>
476
477
  ) {
477
478
  return NumberUtils.formatMoney(
478
479
  input,
479
- this.region,
480
+ this.currency,
480
481
  this.culture,
481
482
  isInteger,
482
483
  options
@@ -542,33 +543,6 @@ export abstract class CoreApp<S extends IAppSettings, N>
542
543
  return refreshToken;
543
544
  }
544
545
 
545
- /**
546
- * Get region id
547
- * @param regionId Country or region id
548
- * @param culture Culture, like zh-CN
549
- * @returns Matched region id
550
- */
551
- getRegion(
552
- regionId: string | undefined | null,
553
- culture: string | undefined | null
554
- ) {
555
- // Exactly match
556
- if (regionId && this.settings.regions.includes(regionId)) {
557
- return regionId;
558
- }
559
-
560
- // Match with language
561
- if (culture) {
562
- const region = this.settings.regions
563
- .map((id) => AddressRegion.getById(id)!)
564
- .find((item) => item.languages.includes(culture));
565
- if (region) return region.id;
566
- }
567
-
568
- // Default
569
- return this.settings.regions[0];
570
- }
571
-
572
546
  /**
573
547
  * Get all regions
574
548
  * @returns Regions