@etsoo/shared 1.0.78 → 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.
- package/__tests__/StorageUtils.ts +2 -1
- package/__tests__/Utils.ts +4 -0
- package/lib/cjs/StorageUtils.d.ts +11 -0
- package/lib/cjs/StorageUtils.js +6 -0
- package/lib/cjs/Utils.d.ts +9 -1
- package/lib/cjs/Utils.js +8 -11
- package/lib/mjs/StorageUtils.d.ts +11 -0
- package/lib/mjs/StorageUtils.js +6 -0
- package/lib/mjs/Utils.d.ts +9 -1
- package/lib/mjs/Utils.js +8 -11
- package/package.json +7 -7
- package/src/StorageUtils.ts +42 -4
- package/src/Utils.ts +32 -14
|
@@ -7,7 +7,8 @@ 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'
|
|
10
|
+
expect(StorageUtils.getSessionData<string>('string')).toBe('test');
|
|
11
|
+
expect(StorageUtils.getSessionData('string1', '')).toBe('');
|
|
11
12
|
expect(StorageUtils.getSessionData('boolean', false)).toBe(true);
|
|
12
13
|
expect(StorageUtils.getSessionData('number', 0)).toBe(3.14);
|
|
13
14
|
expect(StorageUtils.getSessionData('test', {})).toHaveProperty('id', 123);
|
package/__tests__/Utils.ts
CHANGED
|
@@ -100,8 +100,12 @@ test('Tests for objectUpdated', () => {
|
|
|
100
100
|
});
|
|
101
101
|
|
|
102
102
|
test('Tests for parseString', () => {
|
|
103
|
+
expect(Utils.parseString<string>('test')).toBe('test');
|
|
103
104
|
expect(Utils.parseString('test', '')).toBe('test');
|
|
104
105
|
expect(Utils.parseString('true', false)).toBe(true);
|
|
106
|
+
expect(Utils.parseString('', false)).toBeFalsy();
|
|
107
|
+
expect(Utils.parseString<boolean>('')).toBeUndefined();
|
|
108
|
+
expect(Utils.parseString<number>(undefined)).toBeUndefined();
|
|
105
109
|
expect(Utils.parseString('3.14', 0)).toBe(3.14);
|
|
106
110
|
expect(Utils.parseString('2021/4/13', new Date())).toStrictEqual(
|
|
107
111
|
new Date('2021/4/13')
|
|
@@ -15,6 +15,11 @@ 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
|
|
@@ -30,6 +35,12 @@ export declare namespace StorageUtils {
|
|
|
30
35
|
* Get session storage data
|
|
31
36
|
* @param key Key name
|
|
32
37
|
*/
|
|
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
|
+
*/
|
|
33
44
|
function getSessionData<T>(key: string, defaultValue: T): T;
|
|
34
45
|
/**
|
|
35
46
|
* Get session storage object data
|
package/lib/cjs/StorageUtils.js
CHANGED
|
@@ -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
|
}
|
package/lib/cjs/Utils.d.ts
CHANGED
|
@@ -135,7 +135,15 @@ export declare namespace Utils {
|
|
|
135
135
|
*/
|
|
136
136
|
function objectUpdated(objNew: {}, objPrev: {}, ignoreFields?: string[], strict?: number): string[];
|
|
137
137
|
/**
|
|
138
|
-
* Parse string (JSON) to specific type
|
|
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;
|
|
144
|
+
/**
|
|
145
|
+
* Parse string (JSON) to specific type, no type conversion
|
|
146
|
+
* For type conversion, please use DataTypes.convert
|
|
139
147
|
* @param input Input string
|
|
140
148
|
* @param defaultValue Default value
|
|
141
149
|
* @returns Parsed value
|
package/lib/cjs/Utils.js
CHANGED
|
@@ -267,14 +267,16 @@ var Utils;
|
|
|
267
267
|
}
|
|
268
268
|
Utils.objectUpdated = objectUpdated;
|
|
269
269
|
/**
|
|
270
|
-
* Parse string (JSON) to specific type
|
|
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
|
|
272
|
+
* For type conversion, please use DataTypes.convert
|
|
271
273
|
* @param input Input string
|
|
272
274
|
* @param defaultValue Default value
|
|
273
275
|
* @returns Parsed value
|
|
274
276
|
*/
|
|
275
277
|
function parseString(input, defaultValue) {
|
|
276
|
-
// Undefined case, return default value
|
|
277
|
-
if (input == null)
|
|
278
|
+
// Undefined and empty case, return default value
|
|
279
|
+
if (input == null || input === '')
|
|
278
280
|
return defaultValue;
|
|
279
281
|
// String
|
|
280
282
|
if (typeof defaultValue === 'string')
|
|
@@ -292,16 +294,11 @@ var Utils;
|
|
|
292
294
|
// Return
|
|
293
295
|
return json;
|
|
294
296
|
}
|
|
295
|
-
catch
|
|
296
|
-
|
|
297
|
+
catch {
|
|
298
|
+
if (defaultValue == null)
|
|
299
|
+
return input;
|
|
297
300
|
return defaultValue;
|
|
298
301
|
}
|
|
299
|
-
/*
|
|
300
|
-
finally part will still return the boolean value
|
|
301
|
-
finally {
|
|
302
|
-
return defaultValue
|
|
303
|
-
}
|
|
304
|
-
*/
|
|
305
302
|
}
|
|
306
303
|
Utils.parseString = parseString;
|
|
307
304
|
/**
|
|
@@ -15,6 +15,11 @@ 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
|
|
@@ -30,6 +35,12 @@ export declare namespace StorageUtils {
|
|
|
30
35
|
* Get session storage data
|
|
31
36
|
* @param key Key name
|
|
32
37
|
*/
|
|
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
|
+
*/
|
|
33
44
|
function getSessionData<T>(key: string, defaultValue: T): T;
|
|
34
45
|
/**
|
|
35
46
|
* Get session storage object data
|
package/lib/mjs/StorageUtils.js
CHANGED
|
@@ -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
|
}
|
package/lib/mjs/Utils.d.ts
CHANGED
|
@@ -135,7 +135,15 @@ export declare namespace Utils {
|
|
|
135
135
|
*/
|
|
136
136
|
function objectUpdated(objNew: {}, objPrev: {}, ignoreFields?: string[], strict?: number): string[];
|
|
137
137
|
/**
|
|
138
|
-
* Parse string (JSON) to specific type
|
|
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;
|
|
144
|
+
/**
|
|
145
|
+
* Parse string (JSON) to specific type, no type conversion
|
|
146
|
+
* For type conversion, please use DataTypes.convert
|
|
139
147
|
* @param input Input string
|
|
140
148
|
* @param defaultValue Default value
|
|
141
149
|
* @returns Parsed value
|
package/lib/mjs/Utils.js
CHANGED
|
@@ -264,14 +264,16 @@ export var Utils;
|
|
|
264
264
|
}
|
|
265
265
|
Utils.objectUpdated = objectUpdated;
|
|
266
266
|
/**
|
|
267
|
-
* Parse string (JSON) to specific type
|
|
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
|
|
269
|
+
* For type conversion, please use DataTypes.convert
|
|
268
270
|
* @param input Input string
|
|
269
271
|
* @param defaultValue Default value
|
|
270
272
|
* @returns Parsed value
|
|
271
273
|
*/
|
|
272
274
|
function parseString(input, defaultValue) {
|
|
273
|
-
// Undefined case, return default value
|
|
274
|
-
if (input == null)
|
|
275
|
+
// Undefined and empty case, return default value
|
|
276
|
+
if (input == null || input === '')
|
|
275
277
|
return defaultValue;
|
|
276
278
|
// String
|
|
277
279
|
if (typeof defaultValue === 'string')
|
|
@@ -289,16 +291,11 @@ export var Utils;
|
|
|
289
291
|
// Return
|
|
290
292
|
return json;
|
|
291
293
|
}
|
|
292
|
-
catch
|
|
293
|
-
|
|
294
|
+
catch {
|
|
295
|
+
if (defaultValue == null)
|
|
296
|
+
return input;
|
|
294
297
|
return defaultValue;
|
|
295
298
|
}
|
|
296
|
-
/*
|
|
297
|
-
finally part will still return the boolean value
|
|
298
|
-
finally {
|
|
299
|
-
return defaultValue
|
|
300
|
-
}
|
|
301
|
-
*/
|
|
302
299
|
}
|
|
303
300
|
Utils.parseString = parseString;
|
|
304
301
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/shared",
|
|
3
|
-
"version": "1.0.
|
|
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",
|
|
@@ -55,13 +55,13 @@
|
|
|
55
55
|
"dependencies": {},
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"@types/jest": "^27.0.3",
|
|
58
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
59
|
-
"@typescript-eslint/parser": "^5.
|
|
60
|
-
"eslint": "^8.
|
|
58
|
+
"@typescript-eslint/eslint-plugin": "^5.8.0",
|
|
59
|
+
"@typescript-eslint/parser": "^5.8.0",
|
|
60
|
+
"eslint": "^8.5.0",
|
|
61
61
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
62
62
|
"eslint-plugin-import": "^2.25.3",
|
|
63
|
-
"jest": "^27.
|
|
64
|
-
"ts-jest": "^27.
|
|
65
|
-
"typescript": "^4.5.
|
|
63
|
+
"jest": "^27.4.5",
|
|
64
|
+
"ts-jest": "^27.1.2",
|
|
65
|
+
"typescript": "^4.5.4"
|
|
66
66
|
}
|
|
67
67
|
}
|
package/src/StorageUtils.ts
CHANGED
|
@@ -44,17 +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>(
|
|
65
|
+
export function getLocalData<T>(
|
|
66
|
+
key: string,
|
|
67
|
+
defaultValue?: T
|
|
68
|
+
): T | undefined {
|
|
53
69
|
// Get storage
|
|
54
70
|
const data = localStorage.getItem(key);
|
|
55
71
|
|
|
72
|
+
// No default value
|
|
73
|
+
if (defaultValue == null) return Utils.parseString<T>(data);
|
|
74
|
+
|
|
56
75
|
// Return
|
|
57
|
-
return Utils.parseString(data, defaultValue);
|
|
76
|
+
return Utils.parseString<T>(data, defaultValue);
|
|
58
77
|
}
|
|
59
78
|
|
|
60
79
|
/**
|
|
@@ -72,12 +91,31 @@ export namespace StorageUtils {
|
|
|
72
91
|
* Get session storage data
|
|
73
92
|
* @param key Key name
|
|
74
93
|
*/
|
|
75
|
-
export function getSessionData<T>(key: string
|
|
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>(
|
|
108
|
+
key: string,
|
|
109
|
+
defaultValue?: T
|
|
110
|
+
): T | undefined {
|
|
76
111
|
// Get storage
|
|
77
112
|
const data = sessionStorage.getItem(key);
|
|
78
113
|
|
|
114
|
+
// No default value
|
|
115
|
+
if (defaultValue == null) return Utils.parseString<T>(data);
|
|
116
|
+
|
|
79
117
|
// Return
|
|
80
|
-
return Utils.parseString(data, defaultValue);
|
|
118
|
+
return Utils.parseString<T>(data, defaultValue);
|
|
81
119
|
}
|
|
82
120
|
|
|
83
121
|
/**
|
package/src/Utils.ts
CHANGED
|
@@ -362,7 +362,18 @@ export namespace Utils {
|
|
|
362
362
|
}
|
|
363
363
|
|
|
364
364
|
/**
|
|
365
|
-
* Parse string (JSON) to specific type
|
|
365
|
+
* Parse string (JSON) to specific type, no type conversion
|
|
366
|
+
* For type conversion, please use DataTypes.convert
|
|
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
|
|
366
377
|
* @param input Input string
|
|
367
378
|
* @param defaultValue Default value
|
|
368
379
|
* @returns Parsed value
|
|
@@ -370,9 +381,22 @@ export namespace Utils {
|
|
|
370
381
|
export function parseString<T>(
|
|
371
382
|
input: string | undefined | null,
|
|
372
383
|
defaultValue: T
|
|
373
|
-
): T
|
|
374
|
-
|
|
375
|
-
|
|
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
|
|
391
|
+
* @param defaultValue Default value
|
|
392
|
+
* @returns Parsed value
|
|
393
|
+
*/
|
|
394
|
+
export function parseString<T>(
|
|
395
|
+
input: string | undefined | null,
|
|
396
|
+
defaultValue?: T
|
|
397
|
+
): T | undefined {
|
|
398
|
+
// Undefined and empty case, return default value
|
|
399
|
+
if (input == null || input === '') return <T>defaultValue;
|
|
376
400
|
|
|
377
401
|
// String
|
|
378
402
|
if (typeof defaultValue === 'string') return <any>input;
|
|
@@ -381,7 +405,7 @@ export namespace Utils {
|
|
|
381
405
|
// Date
|
|
382
406
|
if (defaultValue instanceof Date) {
|
|
383
407
|
const date = new Date(input);
|
|
384
|
-
if (date == null) return defaultValue;
|
|
408
|
+
if (date == null) return <any>defaultValue;
|
|
385
409
|
return <any>date;
|
|
386
410
|
}
|
|
387
411
|
|
|
@@ -390,16 +414,10 @@ export namespace Utils {
|
|
|
390
414
|
|
|
391
415
|
// Return
|
|
392
416
|
return <T>json;
|
|
393
|
-
} catch
|
|
394
|
-
|
|
395
|
-
return defaultValue;
|
|
396
|
-
}
|
|
397
|
-
/*
|
|
398
|
-
finally part will still return the boolean value
|
|
399
|
-
finally {
|
|
400
|
-
return defaultValue
|
|
417
|
+
} catch {
|
|
418
|
+
if (defaultValue == null) return <any>input;
|
|
419
|
+
return <T>defaultValue;
|
|
401
420
|
}
|
|
402
|
-
*/
|
|
403
421
|
}
|
|
404
422
|
|
|
405
423
|
/**
|