@etsoo/shared 1.0.81 → 1.0.82

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.
@@ -15,12 +15,17 @@ export declare namespace StorageUtils {
15
15
  * @param data Data, null for removal
16
16
  */
17
17
  function setSessionData(key: string, data: unknown): void;
18
+ /**
19
+ * Get local storage data
20
+ * @param key Key name
21
+ */
22
+ function getLocalData<T>(key: string): T | undefined;
18
23
  /**
19
24
  * Get local storage data
20
25
  * @param key Key name
21
26
  * @param defaultValue Default value
22
27
  */
23
- function getLocalData<T, M = T | undefined>(key: string, defaultValue?: M): M extends T ? M : undefined;
28
+ function getLocalData<T>(key: string, defaultValue: T): T;
24
29
  /**
25
30
  * Get local storage object data
26
31
  * @param key Key name
@@ -30,7 +35,13 @@ export declare namespace StorageUtils {
30
35
  * Get session storage data
31
36
  * @param key Key name
32
37
  */
33
- function getSessionData<T, M = T | undefined>(key: string, defaultValue?: M): M extends T ? M : undefined;
38
+ function getSessionData<T>(key: string): T | undefined;
39
+ /**
40
+ * Get session storage data
41
+ * @param key Key name
42
+ * @param defaultValue Default value
43
+ */
44
+ function getSessionData<T>(key: string, defaultValue: T): T;
34
45
  /**
35
46
  * Get session storage object data
36
47
  * @param key Key name
@@ -47,6 +47,9 @@ var StorageUtils;
47
47
  function getLocalData(key, defaultValue) {
48
48
  // Get storage
49
49
  const data = localStorage.getItem(key);
50
+ // No default value
51
+ if (defaultValue == null)
52
+ return Utils_1.Utils.parseString(data);
50
53
  // Return
51
54
  return Utils_1.Utils.parseString(data, defaultValue);
52
55
  }
@@ -70,6 +73,9 @@ var StorageUtils;
70
73
  function getSessionData(key, defaultValue) {
71
74
  // Get storage
72
75
  const data = sessionStorage.getItem(key);
76
+ // No default value
77
+ if (defaultValue == null)
78
+ return Utils_1.Utils.parseString(data);
73
79
  // Return
74
80
  return Utils_1.Utils.parseString(data, defaultValue);
75
81
  }
@@ -134,6 +134,13 @@ export declare namespace Utils {
134
134
  * @returns Updated fields
135
135
  */
136
136
  function objectUpdated(objNew: {}, objPrev: {}, ignoreFields?: string[], strict?: number): string[];
137
+ /**
138
+ * Parse string (JSON) to specific type, no type conversion
139
+ * For type conversion, please use DataTypes.convert
140
+ * @param input Input string
141
+ * @returns Parsed value
142
+ */
143
+ function parseString<T>(input: string | undefined | null): T | undefined;
137
144
  /**
138
145
  * Parse string (JSON) to specific type, no type conversion
139
146
  * For type conversion, please use DataTypes.convert
@@ -141,7 +148,7 @@ export declare namespace Utils {
141
148
  * @param defaultValue Default value
142
149
  * @returns Parsed value
143
150
  */
144
- function parseString<T, M = T | undefined>(input: string | undefined | null, defaultValue?: M): M extends T ? M : undefined;
151
+ function parseString<T>(input: string | undefined | null, defaultValue: T): T;
145
152
  /**
146
153
  * Remove non letters
147
154
  * @param input Input string
package/lib/cjs/Utils.js CHANGED
@@ -268,6 +268,7 @@ var Utils;
268
268
  Utils.objectUpdated = objectUpdated;
269
269
  /**
270
270
  * Parse string (JSON) to specific type, no type conversion
271
+ * When return type depends on parameter value, uses function overloading, otherwise uses conditional type
271
272
  * For type conversion, please use DataTypes.convert
272
273
  * @param input Input string
273
274
  * @param defaultValue Default value
@@ -15,12 +15,17 @@ export declare namespace StorageUtils {
15
15
  * @param data Data, null for removal
16
16
  */
17
17
  function setSessionData(key: string, data: unknown): void;
18
+ /**
19
+ * Get local storage data
20
+ * @param key Key name
21
+ */
22
+ function getLocalData<T>(key: string): T | undefined;
18
23
  /**
19
24
  * Get local storage data
20
25
  * @param key Key name
21
26
  * @param defaultValue Default value
22
27
  */
23
- function getLocalData<T, M = T | undefined>(key: string, defaultValue?: M): M extends T ? M : undefined;
28
+ function getLocalData<T>(key: string, defaultValue: T): T;
24
29
  /**
25
30
  * Get local storage object data
26
31
  * @param key Key name
@@ -30,7 +35,13 @@ export declare namespace StorageUtils {
30
35
  * Get session storage data
31
36
  * @param key Key name
32
37
  */
33
- function getSessionData<T, M = T | undefined>(key: string, defaultValue?: M): M extends T ? M : undefined;
38
+ function getSessionData<T>(key: string): T | undefined;
39
+ /**
40
+ * Get session storage data
41
+ * @param key Key name
42
+ * @param defaultValue Default value
43
+ */
44
+ function getSessionData<T>(key: string, defaultValue: T): T;
34
45
  /**
35
46
  * Get session storage object data
36
47
  * @param key Key name
@@ -44,6 +44,9 @@ export var StorageUtils;
44
44
  function getLocalData(key, defaultValue) {
45
45
  // Get storage
46
46
  const data = localStorage.getItem(key);
47
+ // No default value
48
+ if (defaultValue == null)
49
+ return Utils.parseString(data);
47
50
  // Return
48
51
  return Utils.parseString(data, defaultValue);
49
52
  }
@@ -67,6 +70,9 @@ export var StorageUtils;
67
70
  function getSessionData(key, defaultValue) {
68
71
  // Get storage
69
72
  const data = sessionStorage.getItem(key);
73
+ // No default value
74
+ if (defaultValue == null)
75
+ return Utils.parseString(data);
70
76
  // Return
71
77
  return Utils.parseString(data, defaultValue);
72
78
  }
@@ -134,6 +134,13 @@ export declare namespace Utils {
134
134
  * @returns Updated fields
135
135
  */
136
136
  function objectUpdated(objNew: {}, objPrev: {}, ignoreFields?: string[], strict?: number): string[];
137
+ /**
138
+ * Parse string (JSON) to specific type, no type conversion
139
+ * For type conversion, please use DataTypes.convert
140
+ * @param input Input string
141
+ * @returns Parsed value
142
+ */
143
+ function parseString<T>(input: string | undefined | null): T | undefined;
137
144
  /**
138
145
  * Parse string (JSON) to specific type, no type conversion
139
146
  * For type conversion, please use DataTypes.convert
@@ -141,7 +148,7 @@ export declare namespace Utils {
141
148
  * @param defaultValue Default value
142
149
  * @returns Parsed value
143
150
  */
144
- function parseString<T, M = T | undefined>(input: string | undefined | null, defaultValue?: M): M extends T ? M : undefined;
151
+ function parseString<T>(input: string | undefined | null, defaultValue: T): T;
145
152
  /**
146
153
  * Remove non letters
147
154
  * @param input Input string
package/lib/mjs/Utils.js CHANGED
@@ -265,6 +265,7 @@ export var Utils;
265
265
  Utils.objectUpdated = objectUpdated;
266
266
  /**
267
267
  * Parse string (JSON) to specific type, no type conversion
268
+ * When return type depends on parameter value, uses function overloading, otherwise uses conditional type
268
269
  * For type conversion, please use DataTypes.convert
269
270
  * @param input Input string
270
271
  * @param defaultValue Default value
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.81",
3
+ "version": "1.0.82",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -44,20 +44,36 @@ export namespace StorageUtils {
44
44
  );
45
45
  }
46
46
 
47
+ /**
48
+ * Get local storage data
49
+ * @param key Key name
50
+ */
51
+ export function getLocalData<T>(key: string): T | undefined;
52
+
53
+ /**
54
+ * Get local storage data
55
+ * @param key Key name
56
+ * @param defaultValue Default value
57
+ */
58
+ export function getLocalData<T>(key: string, defaultValue: T): T;
59
+
47
60
  /**
48
61
  * Get local storage data
49
62
  * @param key Key name
50
63
  * @param defaultValue Default value
51
64
  */
52
- export function getLocalData<T, M = T | undefined>(
65
+ export function getLocalData<T>(
53
66
  key: string,
54
- defaultValue?: M
55
- ): M extends T ? M : undefined {
67
+ defaultValue?: T
68
+ ): T | undefined {
56
69
  // Get storage
57
70
  const data = localStorage.getItem(key);
58
71
 
72
+ // No default value
73
+ if (defaultValue == null) return Utils.parseString<T>(data);
74
+
59
75
  // Return
60
- return Utils.parseString<T, M>(data, defaultValue);
76
+ return Utils.parseString<T>(data, defaultValue);
61
77
  }
62
78
 
63
79
  /**
@@ -75,15 +91,31 @@ export namespace StorageUtils {
75
91
  * Get session storage data
76
92
  * @param key Key name
77
93
  */
78
- export function getSessionData<T, M = T | undefined>(
94
+ export function getSessionData<T>(key: string): T | undefined;
95
+
96
+ /**
97
+ * Get session storage data
98
+ * @param key Key name
99
+ * @param defaultValue Default value
100
+ */
101
+ export function getSessionData<T>(key: string, defaultValue: T): T;
102
+
103
+ /**
104
+ * Get session storage data
105
+ * @param key Key name
106
+ */
107
+ export function getSessionData<T>(
79
108
  key: string,
80
- defaultValue?: M
81
- ): M extends T ? M : undefined {
109
+ defaultValue?: T
110
+ ): T | undefined {
82
111
  // Get storage
83
112
  const data = sessionStorage.getItem(key);
84
113
 
114
+ // No default value
115
+ if (defaultValue == null) return Utils.parseString<T>(data);
116
+
85
117
  // Return
86
- return Utils.parseString<T, M>(data, defaultValue);
118
+ return Utils.parseString<T>(data, defaultValue);
87
119
  }
88
120
 
89
121
  /**
package/src/Utils.ts CHANGED
@@ -365,15 +365,38 @@ export namespace Utils {
365
365
  * Parse string (JSON) to specific type, no type conversion
366
366
  * For type conversion, please use DataTypes.convert
367
367
  * @param input Input string
368
+ * @returns Parsed value
369
+ */
370
+ export function parseString<T>(
371
+ input: string | undefined | null
372
+ ): T | undefined;
373
+
374
+ /**
375
+ * Parse string (JSON) to specific type, no type conversion
376
+ * For type conversion, please use DataTypes.convert
377
+ * @param input Input string
378
+ * @param defaultValue Default value
379
+ * @returns Parsed value
380
+ */
381
+ export function parseString<T>(
382
+ input: string | undefined | null,
383
+ defaultValue: T
384
+ ): T;
385
+
386
+ /**
387
+ * Parse string (JSON) to specific type, no type conversion
388
+ * When return type depends on parameter value, uses function overloading, otherwise uses conditional type
389
+ * For type conversion, please use DataTypes.convert
390
+ * @param input Input string
368
391
  * @param defaultValue Default value
369
392
  * @returns Parsed value
370
393
  */
371
- export function parseString<T, M = T | undefined>(
394
+ export function parseString<T>(
372
395
  input: string | undefined | null,
373
- defaultValue?: M
374
- ): M extends T ? M : undefined {
396
+ defaultValue?: T
397
+ ): T | undefined {
375
398
  // Undefined and empty case, return default value
376
- if (input == null || input === '') return <any>defaultValue;
399
+ if (input == null || input === '') return <T>defaultValue;
377
400
 
378
401
  // String
379
402
  if (typeof defaultValue === 'string') return <any>input;
@@ -390,10 +413,10 @@ export namespace Utils {
390
413
  const json = JSON.parse(input);
391
414
 
392
415
  // Return
393
- return json;
416
+ return <T>json;
394
417
  } catch {
395
418
  if (defaultValue == null) return <any>input;
396
- return <any>defaultValue;
419
+ return <T>defaultValue;
397
420
  }
398
421
  }
399
422