@etsoo/shared 1.0.79 → 1.0.80

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.
@@ -7,7 +7,7 @@ test('Tests for all', () => {
7
7
  StorageUtils.setSessionData('number', 3.14);
8
8
  StorageUtils.setSessionData('test', { id: 123, name: 'test' });
9
9
 
10
- expect(StorageUtils.getSessionData('string', '')).toBe('test');
10
+ expect(StorageUtils.getSessionData<string>('string')).toBe('test');
11
11
  expect(StorageUtils.getSessionData('boolean', false)).toBe(true);
12
12
  expect(StorageUtils.getSessionData('number', 0)).toBe(3.14);
13
13
  expect(StorageUtils.getSessionData('test', {})).toHaveProperty('id', 123);
@@ -103,6 +103,7 @@ test('Tests for parseString', () => {
103
103
  expect(Utils.parseString<string>('test')).toBe('test');
104
104
  expect(Utils.parseString('true', false)).toBe(true);
105
105
  expect(Utils.parseString('', false)).toBeFalsy();
106
+ expect(Utils.parseString<boolean>('')).toBeUndefined();
106
107
  expect(Utils.parseString<number>(undefined)).toBeUndefined();
107
108
  expect(Utils.parseString('3.14', 0)).toBe(3.14);
108
109
  expect(Utils.parseString('2021/4/13', new Date())).toStrictEqual(
@@ -20,7 +20,7 @@ export declare namespace StorageUtils {
20
20
  * @param key Key name
21
21
  * @param defaultValue Default value
22
22
  */
23
- function getLocalData<T>(key: string, defaultValue?: T): T | undefined;
23
+ function getLocalData<T, M = T | undefined>(key: string, defaultValue?: M): M extends T ? M : undefined;
24
24
  /**
25
25
  * Get local storage object data
26
26
  * @param key Key name
@@ -30,7 +30,7 @@ export declare namespace StorageUtils {
30
30
  * Get session storage data
31
31
  * @param key Key name
32
32
  */
33
- function getSessionData<T>(key: string, defaultValue?: T): T | undefined;
33
+ function getSessionData<T, M = T | undefined>(key: string, defaultValue?: M): M extends T ? M : undefined;
34
34
  /**
35
35
  * Get session storage object data
36
36
  * @param key Key name
@@ -141,7 +141,7 @@ export declare namespace Utils {
141
141
  * @param defaultValue Default value
142
142
  * @returns Parsed value
143
143
  */
144
- function parseString<T>(input: string | undefined | null, defaultValue?: T): T | undefined;
144
+ function parseString<T, M = T | undefined>(input: string | undefined | null, defaultValue?: M): M extends T ? M : undefined;
145
145
  /**
146
146
  * Remove non letters
147
147
  * @param input Input string
@@ -20,7 +20,7 @@ export declare namespace StorageUtils {
20
20
  * @param key Key name
21
21
  * @param defaultValue Default value
22
22
  */
23
- function getLocalData<T>(key: string, defaultValue?: T): T | undefined;
23
+ function getLocalData<T, M = T | undefined>(key: string, defaultValue?: M): M extends T ? M : undefined;
24
24
  /**
25
25
  * Get local storage object data
26
26
  * @param key Key name
@@ -30,7 +30,7 @@ export declare namespace StorageUtils {
30
30
  * Get session storage data
31
31
  * @param key Key name
32
32
  */
33
- function getSessionData<T>(key: string, defaultValue?: T): T | undefined;
33
+ function getSessionData<T, M = T | undefined>(key: string, defaultValue?: M): M extends T ? M : undefined;
34
34
  /**
35
35
  * Get session storage object data
36
36
  * @param key Key name
@@ -141,7 +141,7 @@ export declare namespace Utils {
141
141
  * @param defaultValue Default value
142
142
  * @returns Parsed value
143
143
  */
144
- function parseString<T>(input: string | undefined | null, defaultValue?: T): T | undefined;
144
+ function parseString<T, M = T | undefined>(input: string | undefined | null, defaultValue?: M): M extends T ? M : undefined;
145
145
  /**
146
146
  * Remove non letters
147
147
  * @param input Input string
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.79",
3
+ "version": "1.0.80",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -49,7 +49,10 @@ export namespace StorageUtils {
49
49
  * @param key Key name
50
50
  * @param defaultValue Default value
51
51
  */
52
- export function getLocalData<T>(key: string, defaultValue?: T) {
52
+ export function getLocalData<T, M = T | undefined>(
53
+ key: string,
54
+ defaultValue?: M
55
+ ): M extends T ? M : undefined {
53
56
  // Get storage
54
57
  const data = localStorage.getItem(key);
55
58
 
@@ -72,7 +75,10 @@ export namespace StorageUtils {
72
75
  * Get session storage data
73
76
  * @param key Key name
74
77
  */
75
- export function getSessionData<T>(key: string, defaultValue?: T) {
78
+ export function getSessionData<T, M = T | undefined>(
79
+ key: string,
80
+ defaultValue?: M
81
+ ): M extends T ? M : undefined {
76
82
  // Get storage
77
83
  const data = sessionStorage.getItem(key);
78
84
 
package/src/Utils.ts CHANGED
@@ -368,12 +368,12 @@ export namespace Utils {
368
368
  * @param defaultValue Default value
369
369
  * @returns Parsed value
370
370
  */
371
- export function parseString<T>(
371
+ export function parseString<T, M = T | undefined>(
372
372
  input: string | undefined | null,
373
- defaultValue?: T
374
- ): T | undefined {
373
+ defaultValue?: M
374
+ ): M extends T ? M : undefined {
375
375
  // Undefined and empty case, return default value
376
- if (input == null || input === '') return defaultValue;
376
+ if (input == null || input === '') return <any>defaultValue;
377
377
 
378
378
  // String
379
379
  if (typeof defaultValue === 'string') return <any>input;
@@ -382,7 +382,7 @@ export namespace Utils {
382
382
  // Date
383
383
  if (defaultValue instanceof Date) {
384
384
  const date = new Date(input);
385
- if (date == null) return defaultValue;
385
+ if (date == null) return <any>defaultValue;
386
386
  return <any>date;
387
387
  }
388
388
 
@@ -390,10 +390,10 @@ export namespace Utils {
390
390
  const json = JSON.parse(input);
391
391
 
392
392
  // Return
393
- return <T>json;
393
+ return json;
394
394
  } catch {
395
395
  if (defaultValue == null) return <any>input;
396
- return defaultValue;
396
+ return <any>defaultValue;
397
397
  }
398
398
  }
399
399