@etsoo/shared 1.1.80 → 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|
@@ -229,11 +229,25 @@ test('Tests for getCulture', () => {
229
229
  { name: 'en', label: 'English', resources: {} }
230
230
  ];
231
231
 
232
- expect(DomUtils.getCulture(cultures, 'zh-CN')?.name).toBe('zh-Hans');
233
- expect(DomUtils.getCulture(cultures, 'zh-Hans-CN')?.name).toBe('zh-Hans');
234
- expect(DomUtils.getCulture(cultures, 'zh-Hans-HK')?.name).toBe('zh-Hans');
235
- expect(DomUtils.getCulture(cultures, 'zh-SG')?.name).toBe('zh-Hans');
236
- expect(DomUtils.getCulture(cultures, 'en-GB')?.name).toBe('en');
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);
237
251
  });
238
252
 
239
253
  test('Tests for getLocationKey', () => {
@@ -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
@@ -74,14 +83,14 @@ export declare namespace DomUtils {
74
83
  * Current detected culture
75
84
  */
76
85
  compatibleNames?: string[] | undefined;
77
- }>[], culture: string) => Readonly<{
86
+ }>[], culture: string) => [Readonly<{
78
87
  name: string;
79
88
  label: string;
80
89
  resources: T; /**
81
90
  * Current detected culture
82
91
  */
83
92
  compatibleNames?: string[] | undefined;
84
- }> | 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,12 +285,12 @@ 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
295
  const compatibleMatch = items.find((item) => {
286
296
  var _a;
@@ -288,14 +298,14 @@ var DomUtils;
288
298
  culture.startsWith(item + '-');
289
299
  });
290
300
  if (compatibleMatch)
291
- return compatibleMatch;
301
+ return [compatibleMatch, CultureMatch.Compatible];
292
302
  // Same part, like zh-CN and zh-HK
293
303
  const samePart = culture.split('-')[0];
294
304
  const samePartMatch = items.find((item) => item.name.startsWith(samePart));
295
305
  if (samePartMatch)
296
- return samePartMatch;
306
+ return [samePartMatch, CultureMatch.SamePart];
297
307
  // Default
298
- return items[0];
308
+ return [items[0], CultureMatch.Default];
299
309
  };
300
310
  /**
301
311
  * Get input value depending on its type
@@ -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
@@ -74,14 +83,14 @@ export declare namespace DomUtils {
74
83
  * Current detected culture
75
84
  */
76
85
  compatibleNames?: string[] | undefined;
77
- }>[], culture: string) => Readonly<{
86
+ }>[], culture: string) => [Readonly<{
78
87
  name: string;
79
88
  label: string;
80
89
  resources: T; /**
81
90
  * Current detected culture
82
91
  */
83
92
  compatibleNames?: string[] | undefined;
84
- }> | 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,12 +282,12 @@ 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
292
  const compatibleMatch = items.find((item) => {
283
293
  var _a;
@@ -285,14 +295,14 @@ export var DomUtils;
285
295
  culture.startsWith(item + '-');
286
296
  });
287
297
  if (compatibleMatch)
288
- return compatibleMatch;
298
+ return [compatibleMatch, CultureMatch.Compatible];
289
299
  // Same part, like zh-CN and zh-HK
290
300
  const samePart = culture.split('-')[0];
291
301
  const samePartMatch = items.find((item) => item.name.startsWith(samePart));
292
302
  if (samePartMatch)
293
- return samePartMatch;
303
+ return [samePartMatch, CultureMatch.SamePart];
294
304
  // Default
295
- return items[0];
305
+ return [items[0], CultureMatch.Default];
296
306
  };
297
307
  /**
298
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.80",
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",
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,14 +325,14 @@ 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
338
  const compatibleMatch = items.find(
@@ -330,17 +340,17 @@ export namespace DomUtils {
330
340
  item.compatibleNames?.includes(culture) ||
331
341
  culture.startsWith(item + '-')
332
342
  );
333
- if (compatibleMatch) return compatibleMatch;
343
+ if (compatibleMatch) return [compatibleMatch, CultureMatch.Compatible];
334
344
 
335
345
  // Same part, like zh-CN and zh-HK
336
346
  const samePart = culture.split('-')[0];
337
347
  const samePartMatch = items.find((item) =>
338
348
  item.name.startsWith(samePart)
339
349
  );
340
- if (samePartMatch) return samePartMatch;
350
+ if (samePartMatch) return [samePartMatch, CultureMatch.SamePart];
341
351
 
342
352
  // Default
343
- return items[0];
353
+ return [items[0], CultureMatch.Default];
344
354
  };
345
355
 
346
356
  /**