@etsoo/shared 1.1.79 → 1.1.81

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -161,6 +161,7 @@ DOM/window related utilities
161
161
  |Name|Description|
162
162
  |---:|---|
163
163
  |clearFormData|Clear form data|
164
+ |CultureMatch|Culture match case Enum|
164
165
  |dataAs|Cast data as template format|
165
166
  |detectedCountry|Current detected country|
166
167
  |detectedCulture|Current detected culture|
@@ -221,17 +221,33 @@ test('Tests for detectedCulture', () => {
221
221
  test('Tests for getCulture', () => {
222
222
  const cultures: DataTypes.CultureDefinition[] = [
223
223
  {
224
- name: 'zh-CN',
224
+ name: 'zh-Hans',
225
225
  label: '简体中文',
226
226
  resources: {},
227
- compatibleName: ['zh-SG']
227
+ compatibleNames: ['zh-CN', 'zh-SG']
228
228
  },
229
- { name: 'en-US', label: 'English', resources: {} }
229
+ { name: 'en', label: 'English', resources: {} }
230
230
  ];
231
231
 
232
- expect(DomUtils.getCulture(cultures, 'zh-CN')?.name).toBe('zh-CN');
233
- expect(DomUtils.getCulture(cultures, 'zh-SG')?.name).toBe('zh-CN');
234
- expect(DomUtils.getCulture(cultures, 'en-GB')?.name).toBe('en-US');
232
+ const [culture1, match1] = DomUtils.getCulture(cultures, 'zh-CN');
233
+ expect(culture1?.name).toBe('zh-Hans');
234
+ expect(match1).toBe(DomUtils.CultureMatch.Compatible);
235
+
236
+ const [culture2] = DomUtils.getCulture(cultures, 'zh-Hans-CN');
237
+ expect(culture2?.name).toBe('zh-Hans');
238
+
239
+ const [culture3] = DomUtils.getCulture(cultures, 'zh-Hans-HK');
240
+ expect(culture3?.name).toBe('zh-Hans');
241
+
242
+ const [culture4] = DomUtils.getCulture(cultures, 'zh-SG');
243
+ expect(culture4?.name).toBe('zh-Hans');
244
+
245
+ const [culture5] = DomUtils.getCulture(cultures, 'en-GB');
246
+ expect(culture5?.name).toBe('en');
247
+
248
+ const [culture6, match6] = DomUtils.getCulture(cultures, 'fr-CA');
249
+ expect(culture6?.name).toBe('zh-Hans');
250
+ expect(match6).toBe(DomUtils.CultureMatch.Default);
235
251
  });
236
252
 
237
253
  test('Tests for getLocationKey', () => {
@@ -240,7 +240,7 @@ export declare namespace DataTypes {
240
240
  /**
241
241
  * Compatible names
242
242
  */
243
- compatibleName?: string[];
243
+ compatibleNames?: string[];
244
244
  }>;
245
245
  /**
246
246
  * Dynamic interface with multiple properties
@@ -62,6 +62,15 @@ export declare namespace DomUtils {
62
62
  * @returns Object
63
63
  */
64
64
  function formDataToObject(form: IFormData): Record<string, FormDataFieldValue | FormDataFieldValue[]>;
65
+ /**
66
+ * Culture match case Enum
67
+ */
68
+ enum CultureMatch {
69
+ Exact = 0,
70
+ Compatible = 1,
71
+ SamePart = 2,
72
+ Default = 3
73
+ }
65
74
  /**
66
75
  * Get the available culture definition
67
76
  * @param items Available cultures
@@ -73,15 +82,15 @@ export declare namespace DomUtils {
73
82
  resources: T; /**
74
83
  * Current detected culture
75
84
  */
76
- compatibleName?: string[] | undefined;
77
- }>[], culture: string) => Readonly<{
85
+ compatibleNames?: string[] | undefined;
86
+ }>[], culture: string) => [Readonly<{
78
87
  name: string;
79
88
  label: string;
80
89
  resources: T; /**
81
90
  * Current detected culture
82
91
  */
83
- compatibleName?: string[] | undefined;
84
- }> | undefined;
92
+ compatibleNames?: string[] | undefined;
93
+ }> | undefined, CultureMatch];
85
94
  /**
86
95
  * Get input value depending on its type
87
96
  * @param input HTML input
@@ -268,6 +268,16 @@ var DomUtils;
268
268
  return dic;
269
269
  }
270
270
  DomUtils.formDataToObject = formDataToObject;
271
+ /**
272
+ * Culture match case Enum
273
+ */
274
+ let CultureMatch;
275
+ (function (CultureMatch) {
276
+ CultureMatch[CultureMatch["Exact"] = 0] = "Exact";
277
+ CultureMatch[CultureMatch["Compatible"] = 1] = "Compatible";
278
+ CultureMatch[CultureMatch["SamePart"] = 2] = "SamePart";
279
+ CultureMatch[CultureMatch["Default"] = 3] = "Default";
280
+ })(CultureMatch = DomUtils.CultureMatch || (DomUtils.CultureMatch = {}));
271
281
  /**
272
282
  * Get the available culture definition
273
283
  * @param items Available cultures
@@ -275,23 +285,27 @@ var DomUtils;
275
285
  */
276
286
  DomUtils.getCulture = (items, culture) => {
277
287
  if (items.length === 0) {
278
- return undefined;
288
+ return [undefined, CultureMatch.Exact];
279
289
  }
280
290
  // Exact match
281
291
  const exactMatch = items.find((item) => item.name === culture);
282
292
  if (exactMatch)
283
- return exactMatch;
293
+ return [exactMatch, CultureMatch.Exact];
284
294
  // Compatible match
285
- const compatibleMatch = items.find((item) => { var _a; return (_a = item.compatibleName) === null || _a === void 0 ? void 0 : _a.includes(culture); });
295
+ const compatibleMatch = items.find((item) => {
296
+ var _a;
297
+ return ((_a = item.compatibleNames) === null || _a === void 0 ? void 0 : _a.includes(culture)) ||
298
+ culture.startsWith(item + '-');
299
+ });
286
300
  if (compatibleMatch)
287
- return compatibleMatch;
301
+ return [compatibleMatch, CultureMatch.Compatible];
288
302
  // Same part, like zh-CN and zh-HK
289
303
  const samePart = culture.split('-')[0];
290
304
  const samePartMatch = items.find((item) => item.name.startsWith(samePart));
291
305
  if (samePartMatch)
292
- return samePartMatch;
306
+ return [samePartMatch, CultureMatch.SamePart];
293
307
  // Default
294
- return items[0];
308
+ return [items[0], CultureMatch.Default];
295
309
  };
296
310
  /**
297
311
  * Get input value depending on its type
@@ -240,7 +240,7 @@ export declare namespace DataTypes {
240
240
  /**
241
241
  * Compatible names
242
242
  */
243
- compatibleName?: string[];
243
+ compatibleNames?: string[];
244
244
  }>;
245
245
  /**
246
246
  * Dynamic interface with multiple properties
@@ -62,6 +62,15 @@ export declare namespace DomUtils {
62
62
  * @returns Object
63
63
  */
64
64
  function formDataToObject(form: IFormData): Record<string, FormDataFieldValue | FormDataFieldValue[]>;
65
+ /**
66
+ * Culture match case Enum
67
+ */
68
+ enum CultureMatch {
69
+ Exact = 0,
70
+ Compatible = 1,
71
+ SamePart = 2,
72
+ Default = 3
73
+ }
65
74
  /**
66
75
  * Get the available culture definition
67
76
  * @param items Available cultures
@@ -73,15 +82,15 @@ export declare namespace DomUtils {
73
82
  resources: T; /**
74
83
  * Current detected culture
75
84
  */
76
- compatibleName?: string[] | undefined;
77
- }>[], culture: string) => Readonly<{
85
+ compatibleNames?: string[] | undefined;
86
+ }>[], culture: string) => [Readonly<{
78
87
  name: string;
79
88
  label: string;
80
89
  resources: T; /**
81
90
  * Current detected culture
82
91
  */
83
- compatibleName?: string[] | undefined;
84
- }> | undefined;
92
+ compatibleNames?: string[] | undefined;
93
+ }> | undefined, CultureMatch];
85
94
  /**
86
95
  * Get input value depending on its type
87
96
  * @param input HTML input
@@ -265,6 +265,16 @@ export var DomUtils;
265
265
  return dic;
266
266
  }
267
267
  DomUtils.formDataToObject = formDataToObject;
268
+ /**
269
+ * Culture match case Enum
270
+ */
271
+ let CultureMatch;
272
+ (function (CultureMatch) {
273
+ CultureMatch[CultureMatch["Exact"] = 0] = "Exact";
274
+ CultureMatch[CultureMatch["Compatible"] = 1] = "Compatible";
275
+ CultureMatch[CultureMatch["SamePart"] = 2] = "SamePart";
276
+ CultureMatch[CultureMatch["Default"] = 3] = "Default";
277
+ })(CultureMatch = DomUtils.CultureMatch || (DomUtils.CultureMatch = {}));
268
278
  /**
269
279
  * Get the available culture definition
270
280
  * @param items Available cultures
@@ -272,23 +282,27 @@ export var DomUtils;
272
282
  */
273
283
  DomUtils.getCulture = (items, culture) => {
274
284
  if (items.length === 0) {
275
- return undefined;
285
+ return [undefined, CultureMatch.Exact];
276
286
  }
277
287
  // Exact match
278
288
  const exactMatch = items.find((item) => item.name === culture);
279
289
  if (exactMatch)
280
- return exactMatch;
290
+ return [exactMatch, CultureMatch.Exact];
281
291
  // Compatible match
282
- const compatibleMatch = items.find((item) => { var _a; return (_a = item.compatibleName) === null || _a === void 0 ? void 0 : _a.includes(culture); });
292
+ const compatibleMatch = items.find((item) => {
293
+ var _a;
294
+ return ((_a = item.compatibleNames) === null || _a === void 0 ? void 0 : _a.includes(culture)) ||
295
+ culture.startsWith(item + '-');
296
+ });
283
297
  if (compatibleMatch)
284
- return compatibleMatch;
298
+ return [compatibleMatch, CultureMatch.Compatible];
285
299
  // Same part, like zh-CN and zh-HK
286
300
  const samePart = culture.split('-')[0];
287
301
  const samePartMatch = items.find((item) => item.name.startsWith(samePart));
288
302
  if (samePartMatch)
289
- return samePartMatch;
303
+ return [samePartMatch, CultureMatch.SamePart];
290
304
  // Default
291
- return items[0];
305
+ return [items[0], CultureMatch.Default];
292
306
  };
293
307
  /**
294
308
  * Get input value depending on its type
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.79",
3
+ "version": "1.1.81",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -58,7 +58,7 @@
58
58
  "@types/lodash.isequal": "^4.5.6",
59
59
  "@typescript-eslint/eslint-plugin": "^5.45.0",
60
60
  "@typescript-eslint/parser": "^5.45.0",
61
- "eslint": "^8.28.0",
61
+ "eslint": "^8.29.0",
62
62
  "eslint-config-airbnb-base": "^15.0.0",
63
63
  "eslint-plugin-import": "^2.26.0",
64
64
  "jest": "^29.3.1",
package/src/DataTypes.ts CHANGED
@@ -305,7 +305,7 @@ export namespace DataTypes {
305
305
  /**
306
306
  * Compatible names
307
307
  */
308
- compatibleName?: string[];
308
+ compatibleNames?: string[];
309
309
  }>;
310
310
 
311
311
  /**
package/src/DomUtils.ts CHANGED
@@ -307,6 +307,16 @@ export namespace DomUtils {
307
307
  return dic;
308
308
  }
309
309
 
310
+ /**
311
+ * Culture match case Enum
312
+ */
313
+ export enum CultureMatch {
314
+ Exact,
315
+ Compatible,
316
+ SamePart,
317
+ Default
318
+ }
319
+
310
320
  /**
311
321
  * Get the available culture definition
312
322
  * @param items Available cultures
@@ -315,30 +325,32 @@ export namespace DomUtils {
315
325
  export const getCulture = <T extends DataTypes.StringRecord>(
316
326
  items: DataTypes.CultureDefinition<T>[],
317
327
  culture: string
318
- ) => {
328
+ ): [DataTypes.CultureDefinition<T> | undefined, CultureMatch] => {
319
329
  if (items.length === 0) {
320
- return undefined;
330
+ return [undefined, CultureMatch.Exact];
321
331
  }
322
332
 
323
333
  // Exact match
324
334
  const exactMatch = items.find((item) => item.name === culture);
325
- if (exactMatch) return exactMatch;
335
+ if (exactMatch) return [exactMatch, CultureMatch.Exact];
326
336
 
327
337
  // Compatible match
328
- const compatibleMatch = items.find((item) =>
329
- item.compatibleName?.includes(culture)
338
+ const compatibleMatch = items.find(
339
+ (item) =>
340
+ item.compatibleNames?.includes(culture) ||
341
+ culture.startsWith(item + '-')
330
342
  );
331
- if (compatibleMatch) return compatibleMatch;
343
+ if (compatibleMatch) return [compatibleMatch, CultureMatch.Compatible];
332
344
 
333
345
  // Same part, like zh-CN and zh-HK
334
346
  const samePart = culture.split('-')[0];
335
347
  const samePartMatch = items.find((item) =>
336
348
  item.name.startsWith(samePart)
337
349
  );
338
- if (samePartMatch) return samePartMatch;
350
+ if (samePartMatch) return [samePartMatch, CultureMatch.SamePart];
339
351
 
340
352
  // Default
341
- return items[0];
353
+ return [items[0], CultureMatch.Default];
342
354
  };
343
355
 
344
356
  /**