@etsoo/shared 1.0.79 → 1.0.83
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 +3 -0
- package/__tests__/StorageUtils.ts +2 -1
- package/__tests__/Utils.ts +2 -0
- package/lib/cjs/StorageUtils.d.ts +13 -2
- package/lib/cjs/StorageUtils.js +7 -0
- package/lib/cjs/Utils.d.ts +8 -1
- package/lib/cjs/Utils.js +1 -0
- package/lib/cjs/index.d.ts +2 -0
- package/lib/cjs/index.js +2 -0
- package/lib/cjs/storage/IStorage.d.ts +33 -0
- package/lib/cjs/storage/IStorage.js +2 -0
- package/lib/cjs/storage/WindowStorage.d.ts +35 -0
- package/lib/cjs/storage/WindowStorage.js +67 -0
- package/lib/mjs/StorageUtils.d.ts +13 -2
- package/lib/mjs/StorageUtils.js +7 -0
- package/lib/mjs/Utils.d.ts +8 -1
- package/lib/mjs/Utils.js +1 -0
- package/lib/mjs/index.d.ts +2 -0
- package/lib/mjs/index.js +2 -0
- package/lib/mjs/storage/IStorage.d.ts +33 -0
- package/lib/mjs/storage/IStorage.js +1 -0
- package/lib/mjs/storage/WindowStorage.d.ts +35 -0
- package/lib/mjs/storage/WindowStorage.js +63 -0
- package/package.json +1 -1
- package/src/StorageUtils.ts +43 -4
- package/src/Utils.ts +26 -3
- package/src/index.ts +4 -0
- package/src/storage/IStorage.ts +37 -0
- package/src/storage/WindowStorage.ts +80 -0
package/README.md
CHANGED
|
@@ -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
|
@@ -101,8 +101,10 @@ test('Tests for objectUpdated', () => {
|
|
|
101
101
|
|
|
102
102
|
test('Tests for parseString', () => {
|
|
103
103
|
expect(Utils.parseString<string>('test')).toBe('test');
|
|
104
|
+
expect(Utils.parseString('test', '')).toBe('test');
|
|
104
105
|
expect(Utils.parseString('true', false)).toBe(true);
|
|
105
106
|
expect(Utils.parseString('', false)).toBeFalsy();
|
|
107
|
+
expect(Utils.parseString<boolean>('')).toBeUndefined();
|
|
106
108
|
expect(Utils.parseString<number>(undefined)).toBeUndefined();
|
|
107
109
|
expect(Utils.parseString('3.14', 0)).toBe(3.14);
|
|
108
110
|
expect(Utils.parseString('2021/4/13', new Date())).toStrictEqual(
|
|
@@ -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>(key: string, defaultValue
|
|
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>(key: string
|
|
38
|
+
function getSessionData<T>(key: string): T | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Get session storage data with default value
|
|
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
|
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
|
}
|
|
@@ -66,10 +69,14 @@ var StorageUtils;
|
|
|
66
69
|
/**
|
|
67
70
|
* Get session storage data
|
|
68
71
|
* @param key Key name
|
|
72
|
+
* @param defaultValue Default value
|
|
69
73
|
*/
|
|
70
74
|
function getSessionData(key, defaultValue) {
|
|
71
75
|
// Get storage
|
|
72
76
|
const data = sessionStorage.getItem(key);
|
|
77
|
+
// No default value
|
|
78
|
+
if (defaultValue == null)
|
|
79
|
+
return Utils_1.Utils.parseString(data);
|
|
73
80
|
// Return
|
|
74
81
|
return Utils_1.Utils.parseString(data, defaultValue);
|
|
75
82
|
}
|
package/lib/cjs/Utils.d.ts
CHANGED
|
@@ -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>(input: string | undefined | null, defaultValue
|
|
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
|
package/lib/cjs/index.d.ts
CHANGED
package/lib/cjs/index.js
CHANGED
|
@@ -11,6 +11,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
13
|
__exportStar(require("./types/FormData"), exports);
|
|
14
|
+
__exportStar(require("./storage/IStorage"), exports);
|
|
15
|
+
__exportStar(require("./storage/WindowStorage"), exports);
|
|
14
16
|
__exportStar(require("./DataTypes"), exports);
|
|
15
17
|
__exportStar(require("./DateUtils"), exports);
|
|
16
18
|
__exportStar(require("./DomUtils"), exports);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage interface
|
|
3
|
+
*/
|
|
4
|
+
export interface IStorage {
|
|
5
|
+
/**
|
|
6
|
+
* Get data
|
|
7
|
+
* @param key Key name
|
|
8
|
+
*/
|
|
9
|
+
getData<T>(key: string): T | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Get data with default value
|
|
12
|
+
* @param key Key name
|
|
13
|
+
* @param defaultValue Default value
|
|
14
|
+
*/
|
|
15
|
+
getData<T>(key: string, defaultValue: T): T;
|
|
16
|
+
/**
|
|
17
|
+
* Get session storage object data
|
|
18
|
+
* @param key Key name
|
|
19
|
+
*/
|
|
20
|
+
getObject<T extends {}>(key: string): T | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Set data
|
|
23
|
+
* @param key Key name
|
|
24
|
+
* @param data Data, null for removal
|
|
25
|
+
*/
|
|
26
|
+
setData(key: string, data: unknown): void;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Storage constructor interface
|
|
30
|
+
*/
|
|
31
|
+
export interface IStorageConstructor {
|
|
32
|
+
new (globalFields: string[]): IStorage;
|
|
33
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { IStorage } from './IStorage';
|
|
2
|
+
/**
|
|
3
|
+
* Window storage
|
|
4
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
|
|
5
|
+
*/
|
|
6
|
+
export declare class WindowStorage implements IStorage {
|
|
7
|
+
private globalFields;
|
|
8
|
+
/**
|
|
9
|
+
* Constructor
|
|
10
|
+
* @param globalFields Global fields
|
|
11
|
+
*/
|
|
12
|
+
constructor(globalFields: string[]);
|
|
13
|
+
/**
|
|
14
|
+
* Get data
|
|
15
|
+
* @param key Key name
|
|
16
|
+
*/
|
|
17
|
+
getData<T>(key: string): T | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Get data with default value
|
|
20
|
+
* @param key Key name
|
|
21
|
+
* @param defaultValue Default value
|
|
22
|
+
*/
|
|
23
|
+
getData<T>(key: string, defaultValue: T): T;
|
|
24
|
+
/**
|
|
25
|
+
* Get object data
|
|
26
|
+
* @param key Key name
|
|
27
|
+
*/
|
|
28
|
+
getObject<T extends {}>(key: string): T | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Set data
|
|
31
|
+
* @param key Key name
|
|
32
|
+
* @param data Data, null for removal
|
|
33
|
+
*/
|
|
34
|
+
setData(key: string, data: unknown): void;
|
|
35
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WindowStorage = void 0;
|
|
4
|
+
const StorageUtils_1 = require("../StorageUtils");
|
|
5
|
+
const Utils_1 = require("../Utils");
|
|
6
|
+
/**
|
|
7
|
+
* Window storage
|
|
8
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
|
|
9
|
+
*/
|
|
10
|
+
class WindowStorage {
|
|
11
|
+
/**
|
|
12
|
+
* Constructor
|
|
13
|
+
* @param globalFields Global fields
|
|
14
|
+
*/
|
|
15
|
+
constructor(globalFields) {
|
|
16
|
+
this.globalFields = globalFields;
|
|
17
|
+
if (globalFields.length == 0)
|
|
18
|
+
return;
|
|
19
|
+
// Copy global fields to session storage where first item does not exist
|
|
20
|
+
const firsItem = sessionStorage.getItem(globalFields[0]);
|
|
21
|
+
if (firsItem)
|
|
22
|
+
return;
|
|
23
|
+
globalFields.forEach((field) => {
|
|
24
|
+
const data = localStorage.getItem(field);
|
|
25
|
+
if (data != null) {
|
|
26
|
+
sessionStorage.setItem(field, data);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Get data
|
|
32
|
+
* @param key Key name
|
|
33
|
+
* @param defaultValue Default value
|
|
34
|
+
*/
|
|
35
|
+
getData(key, defaultValue) {
|
|
36
|
+
// Get storage
|
|
37
|
+
const data = sessionStorage.getItem(key);
|
|
38
|
+
// No default value
|
|
39
|
+
if (defaultValue == null)
|
|
40
|
+
return Utils_1.Utils.parseString(data);
|
|
41
|
+
// Return
|
|
42
|
+
return Utils_1.Utils.parseString(data, defaultValue);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Get object data
|
|
46
|
+
* @param key Key name
|
|
47
|
+
*/
|
|
48
|
+
getObject(key) {
|
|
49
|
+
// Get storage
|
|
50
|
+
const data = sessionStorage.getItem(key);
|
|
51
|
+
if (data == null)
|
|
52
|
+
return undefined;
|
|
53
|
+
return JSON.parse(data);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Set data
|
|
57
|
+
* @param key Key name
|
|
58
|
+
* @param data Data, null for removal
|
|
59
|
+
*/
|
|
60
|
+
setData(key, data) {
|
|
61
|
+
StorageUtils_1.StorageUtils.setSessionData(key, data);
|
|
62
|
+
if (this.globalFields.includes(key)) {
|
|
63
|
+
StorageUtils_1.StorageUtils.setLocalData(key, data);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.WindowStorage = WindowStorage;
|
|
@@ -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>(key: string, defaultValue
|
|
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>(key: string
|
|
38
|
+
function getSessionData<T>(key: string): T | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Get session storage data with default value
|
|
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
|
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
|
}
|
|
@@ -63,10 +66,14 @@ export var StorageUtils;
|
|
|
63
66
|
/**
|
|
64
67
|
* Get session storage data
|
|
65
68
|
* @param key Key name
|
|
69
|
+
* @param defaultValue Default value
|
|
66
70
|
*/
|
|
67
71
|
function getSessionData(key, defaultValue) {
|
|
68
72
|
// Get storage
|
|
69
73
|
const data = sessionStorage.getItem(key);
|
|
74
|
+
// No default value
|
|
75
|
+
if (defaultValue == null)
|
|
76
|
+
return Utils.parseString(data);
|
|
70
77
|
// Return
|
|
71
78
|
return Utils.parseString(data, defaultValue);
|
|
72
79
|
}
|
package/lib/mjs/Utils.d.ts
CHANGED
|
@@ -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>(input: string | undefined | null, defaultValue
|
|
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/lib/mjs/index.d.ts
CHANGED
package/lib/mjs/index.js
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage interface
|
|
3
|
+
*/
|
|
4
|
+
export interface IStorage {
|
|
5
|
+
/**
|
|
6
|
+
* Get data
|
|
7
|
+
* @param key Key name
|
|
8
|
+
*/
|
|
9
|
+
getData<T>(key: string): T | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Get data with default value
|
|
12
|
+
* @param key Key name
|
|
13
|
+
* @param defaultValue Default value
|
|
14
|
+
*/
|
|
15
|
+
getData<T>(key: string, defaultValue: T): T;
|
|
16
|
+
/**
|
|
17
|
+
* Get session storage object data
|
|
18
|
+
* @param key Key name
|
|
19
|
+
*/
|
|
20
|
+
getObject<T extends {}>(key: string): T | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Set data
|
|
23
|
+
* @param key Key name
|
|
24
|
+
* @param data Data, null for removal
|
|
25
|
+
*/
|
|
26
|
+
setData(key: string, data: unknown): void;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Storage constructor interface
|
|
30
|
+
*/
|
|
31
|
+
export interface IStorageConstructor {
|
|
32
|
+
new (globalFields: string[]): IStorage;
|
|
33
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { IStorage } from './IStorage';
|
|
2
|
+
/**
|
|
3
|
+
* Window storage
|
|
4
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
|
|
5
|
+
*/
|
|
6
|
+
export declare class WindowStorage implements IStorage {
|
|
7
|
+
private globalFields;
|
|
8
|
+
/**
|
|
9
|
+
* Constructor
|
|
10
|
+
* @param globalFields Global fields
|
|
11
|
+
*/
|
|
12
|
+
constructor(globalFields: string[]);
|
|
13
|
+
/**
|
|
14
|
+
* Get data
|
|
15
|
+
* @param key Key name
|
|
16
|
+
*/
|
|
17
|
+
getData<T>(key: string): T | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Get data with default value
|
|
20
|
+
* @param key Key name
|
|
21
|
+
* @param defaultValue Default value
|
|
22
|
+
*/
|
|
23
|
+
getData<T>(key: string, defaultValue: T): T;
|
|
24
|
+
/**
|
|
25
|
+
* Get object data
|
|
26
|
+
* @param key Key name
|
|
27
|
+
*/
|
|
28
|
+
getObject<T extends {}>(key: string): T | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Set data
|
|
31
|
+
* @param key Key name
|
|
32
|
+
* @param data Data, null for removal
|
|
33
|
+
*/
|
|
34
|
+
setData(key: string, data: unknown): void;
|
|
35
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { StorageUtils } from '../StorageUtils';
|
|
2
|
+
import { Utils } from '../Utils';
|
|
3
|
+
/**
|
|
4
|
+
* Window storage
|
|
5
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
|
|
6
|
+
*/
|
|
7
|
+
export class WindowStorage {
|
|
8
|
+
/**
|
|
9
|
+
* Constructor
|
|
10
|
+
* @param globalFields Global fields
|
|
11
|
+
*/
|
|
12
|
+
constructor(globalFields) {
|
|
13
|
+
this.globalFields = globalFields;
|
|
14
|
+
if (globalFields.length == 0)
|
|
15
|
+
return;
|
|
16
|
+
// Copy global fields to session storage where first item does not exist
|
|
17
|
+
const firsItem = sessionStorage.getItem(globalFields[0]);
|
|
18
|
+
if (firsItem)
|
|
19
|
+
return;
|
|
20
|
+
globalFields.forEach((field) => {
|
|
21
|
+
const data = localStorage.getItem(field);
|
|
22
|
+
if (data != null) {
|
|
23
|
+
sessionStorage.setItem(field, data);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Get data
|
|
29
|
+
* @param key Key name
|
|
30
|
+
* @param defaultValue Default value
|
|
31
|
+
*/
|
|
32
|
+
getData(key, defaultValue) {
|
|
33
|
+
// Get storage
|
|
34
|
+
const data = sessionStorage.getItem(key);
|
|
35
|
+
// No default value
|
|
36
|
+
if (defaultValue == null)
|
|
37
|
+
return Utils.parseString(data);
|
|
38
|
+
// Return
|
|
39
|
+
return Utils.parseString(data, defaultValue);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get object data
|
|
43
|
+
* @param key Key name
|
|
44
|
+
*/
|
|
45
|
+
getObject(key) {
|
|
46
|
+
// Get storage
|
|
47
|
+
const data = sessionStorage.getItem(key);
|
|
48
|
+
if (data == null)
|
|
49
|
+
return undefined;
|
|
50
|
+
return JSON.parse(data);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Set data
|
|
54
|
+
* @param key Key name
|
|
55
|
+
* @param data Data, null for removal
|
|
56
|
+
*/
|
|
57
|
+
setData(key, data) {
|
|
58
|
+
StorageUtils.setSessionData(key, data);
|
|
59
|
+
if (this.globalFields.includes(key)) {
|
|
60
|
+
StorageUtils.setLocalData(key, data);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
package/package.json
CHANGED
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,32 @@ 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 with default value
|
|
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
|
+
* @param defaultValue Default value
|
|
107
|
+
*/
|
|
108
|
+
export function getSessionData<T>(
|
|
109
|
+
key: string,
|
|
110
|
+
defaultValue?: T
|
|
111
|
+
): T | undefined {
|
|
76
112
|
// Get storage
|
|
77
113
|
const data = sessionStorage.getItem(key);
|
|
78
114
|
|
|
115
|
+
// No default value
|
|
116
|
+
if (defaultValue == null) return Utils.parseString<T>(data);
|
|
117
|
+
|
|
79
118
|
// Return
|
|
80
|
-
return Utils.parseString(data, defaultValue);
|
|
119
|
+
return Utils.parseString<T>(data, defaultValue);
|
|
81
120
|
}
|
|
82
121
|
|
|
83
122
|
/**
|
package/src/Utils.ts
CHANGED
|
@@ -365,6 +365,29 @@ 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
|
*/
|
|
@@ -373,7 +396,7 @@ export namespace Utils {
|
|
|
373
396
|
defaultValue?: T
|
|
374
397
|
): T | undefined {
|
|
375
398
|
// Undefined and empty case, return default value
|
|
376
|
-
if (input == null || input === '') return defaultValue;
|
|
399
|
+
if (input == null || input === '') return <T>defaultValue;
|
|
377
400
|
|
|
378
401
|
// String
|
|
379
402
|
if (typeof defaultValue === 'string') return <any>input;
|
|
@@ -382,7 +405,7 @@ export namespace Utils {
|
|
|
382
405
|
// Date
|
|
383
406
|
if (defaultValue instanceof Date) {
|
|
384
407
|
const date = new Date(input);
|
|
385
|
-
if (date == null) return defaultValue;
|
|
408
|
+
if (date == null) return <any>defaultValue;
|
|
386
409
|
return <any>date;
|
|
387
410
|
}
|
|
388
411
|
|
|
@@ -393,7 +416,7 @@ export namespace Utils {
|
|
|
393
416
|
return <T>json;
|
|
394
417
|
} catch {
|
|
395
418
|
if (defaultValue == null) return <any>input;
|
|
396
|
-
return defaultValue;
|
|
419
|
+
return <T>defaultValue;
|
|
397
420
|
}
|
|
398
421
|
}
|
|
399
422
|
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage interface
|
|
3
|
+
*/
|
|
4
|
+
export interface IStorage {
|
|
5
|
+
/**
|
|
6
|
+
* Get data
|
|
7
|
+
* @param key Key name
|
|
8
|
+
*/
|
|
9
|
+
getData<T>(key: string): T | undefined;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Get data with default value
|
|
13
|
+
* @param key Key name
|
|
14
|
+
* @param defaultValue Default value
|
|
15
|
+
*/
|
|
16
|
+
getData<T>(key: string, defaultValue: T): T;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Get session storage object data
|
|
20
|
+
* @param key Key name
|
|
21
|
+
*/
|
|
22
|
+
getObject<T extends {}>(key: string): T | undefined;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Set data
|
|
26
|
+
* @param key Key name
|
|
27
|
+
* @param data Data, null for removal
|
|
28
|
+
*/
|
|
29
|
+
setData(key: string, data: unknown): void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Storage constructor interface
|
|
34
|
+
*/
|
|
35
|
+
export interface IStorageConstructor {
|
|
36
|
+
new (globalFields: string[]): IStorage;
|
|
37
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { StorageUtils } from '../StorageUtils';
|
|
2
|
+
import { Utils } from '../Utils';
|
|
3
|
+
import { IStorage } from './IStorage';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Window storage
|
|
7
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
|
|
8
|
+
*/
|
|
9
|
+
export class WindowStorage implements IStorage {
|
|
10
|
+
/**
|
|
11
|
+
* Constructor
|
|
12
|
+
* @param globalFields Global fields
|
|
13
|
+
*/
|
|
14
|
+
constructor(private globalFields: string[]) {
|
|
15
|
+
if (globalFields.length == 0) return;
|
|
16
|
+
|
|
17
|
+
// Copy global fields to session storage where first item does not exist
|
|
18
|
+
const firsItem = sessionStorage.getItem(globalFields[0]);
|
|
19
|
+
if (firsItem) return;
|
|
20
|
+
|
|
21
|
+
globalFields.forEach((field) => {
|
|
22
|
+
const data = localStorage.getItem(field);
|
|
23
|
+
if (data != null) {
|
|
24
|
+
sessionStorage.setItem(field, data);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Get data
|
|
31
|
+
* @param key Key name
|
|
32
|
+
*/
|
|
33
|
+
getData<T>(key: string): T | undefined;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Get data with default value
|
|
37
|
+
* @param key Key name
|
|
38
|
+
* @param defaultValue Default value
|
|
39
|
+
*/
|
|
40
|
+
getData<T>(key: string, defaultValue: T): T;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Get data
|
|
44
|
+
* @param key Key name
|
|
45
|
+
* @param defaultValue Default value
|
|
46
|
+
*/
|
|
47
|
+
getData<T>(key: string, defaultValue?: T): T | undefined {
|
|
48
|
+
// Get storage
|
|
49
|
+
const data = sessionStorage.getItem(key);
|
|
50
|
+
|
|
51
|
+
// No default value
|
|
52
|
+
if (defaultValue == null) return Utils.parseString<T>(data);
|
|
53
|
+
|
|
54
|
+
// Return
|
|
55
|
+
return Utils.parseString<T>(data, defaultValue);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Get object data
|
|
60
|
+
* @param key Key name
|
|
61
|
+
*/
|
|
62
|
+
getObject<T extends {}>(key: string) {
|
|
63
|
+
// Get storage
|
|
64
|
+
const data = sessionStorage.getItem(key);
|
|
65
|
+
if (data == null) return undefined;
|
|
66
|
+
return <T>JSON.parse(data);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Set data
|
|
71
|
+
* @param key Key name
|
|
72
|
+
* @param data Data, null for removal
|
|
73
|
+
*/
|
|
74
|
+
setData(key: string, data: unknown) {
|
|
75
|
+
StorageUtils.setSessionData(key, data);
|
|
76
|
+
if (this.globalFields.includes(key)) {
|
|
77
|
+
StorageUtils.setLocalData(key, data);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|