@etsoo/shared 1.0.90 → 1.0.94

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.
@@ -3,56 +3,65 @@
3
3
  */
4
4
  export interface IStorage {
5
5
  /**
6
- * Current instance index
6
+ * Clear keys
7
+ * @param keys Keys
8
+ * @param persisted Persisted or session data
7
9
  */
8
- readonly instanceIndex: number;
10
+ clear(keys: string[], persisted?: boolean): void;
11
+ /**
12
+ * Copy keys to session from persisted source
13
+ * @param keys Keys
14
+ * @param removeSource Remove from the source
15
+ */
16
+ copyFrom(keys: string[], removeSource?: boolean): void;
17
+ /**
18
+ * Copy keys to persisted source
19
+ * @param keys Keys
20
+ * @param removeSource Remove from the source
21
+ */
22
+ copyTo(keys: string[], removeSource?: boolean): void;
9
23
  /**
10
24
  * Get data
11
25
  * @param key Key name
12
- * @param persistance From the persisted data
13
26
  */
14
- getData<T>(key: string, persistance?: boolean): T | undefined;
27
+ getData<T>(key: string): T | undefined;
15
28
  /**
16
29
  * Get data with default value
17
30
  * @param key Key name
18
31
  * @param defaultValue Default value
19
- * @param persistance From the persisted data
20
32
  */
21
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
33
+ getData<T>(key: string, defaultValue: T): T;
34
+ /**
35
+ * Get persisted data
36
+ * @param key Key name
37
+ */
38
+ getPersistedData<T>(key: string): T | undefined;
39
+ /**
40
+ * Get persisted data with default value
41
+ * @param key Key name
42
+ * @param defaultValue Default value
43
+ */
44
+ getPersistedData<T>(key: string, defaultValue: T): T;
22
45
  /**
23
46
  * Get object data
24
47
  * @param key Key name
25
- * @param persistance From the persisted data
26
48
  */
27
- getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
49
+ getObject<T extends {}>(key: string): T | undefined;
28
50
  /**
29
- * Set data
51
+ * Get persisted object data
30
52
  * @param key Key name
31
- * @param data Data, null for removal
32
- * @param persistance Persist the data, false will stop persistance
33
53
  */
34
- setData(key: string, data: unknown, persistance?: boolean): void;
54
+ getPersistedObject<T extends {}>(key: string): T | undefined;
35
55
  /**
36
- * Get current instance count
37
- * @returns Current instance count
56
+ * Set data
57
+ * @param key Key name
58
+ * @param data Data, null for removal
38
59
  */
39
- getInstanceCount(): number;
60
+ setData(key: string, data: unknown): void;
40
61
  /**
41
- * Update instance count
42
- * @param removed Is removed?
43
- * @returns Current instance count
62
+ * Set persisted data
63
+ * @param key Key name
64
+ * @param data Data, null for removal
44
65
  */
45
- updateInstanceCount(removed: boolean): number;
46
- }
47
- /**
48
- * Storage init callback
49
- */
50
- export interface IStorageInitCallback {
51
- (field: string, data: string | null, instanceIndex: number): string | null;
52
- }
53
- /**
54
- * Storage constructor interface
55
- */
56
- export interface IStorageConstructor {
57
- new (globalFields: string[], callback: IStorageInitCallback): IStorage;
66
+ setPersistedData(key: string, data: unknown): void;
58
67
  }
@@ -1,60 +1,69 @@
1
- import { IStorage, IStorageInitCallback } from './IStorage';
1
+ import { IStorage } from './IStorage';
2
2
  /**
3
3
  * Window storage
4
4
  * https://developer.mozilla.org/en-US/docs/Web/API/Storage
5
5
  */
6
6
  export declare class WindowStorage implements IStorage {
7
- private globalFields;
8
7
  /**
9
- * Instance count field name
8
+ * Clear keys
9
+ * @param keys Keys
10
+ * @param persisted Persisted or session data
10
11
  */
11
- private readonly instanceCountField;
12
- private _instanceIndex;
12
+ clear(keys: string[], persisted?: boolean): void;
13
13
  /**
14
- * Current instance index
14
+ * Copy keys to session from persisted source
15
+ * @param keys Keys
16
+ * @param removeSource Remove from the source
15
17
  */
16
- get instanceIndex(): number;
18
+ copyFrom(keys: string[], removeSource?: boolean): void;
17
19
  /**
18
- * Constructor
19
- * @param globalFields Global fields
20
- * @param callback Field and data callback
20
+ * Copy keys to persisted source
21
+ * @param keys Keys
22
+ * @param removeSource Remove from the source
21
23
  */
22
- constructor(globalFields: string[], callback: IStorageInitCallback);
24
+ copyTo(keys: string[], removeSource?: boolean): void;
23
25
  /**
24
26
  * Get data
25
27
  * @param key Key name
26
- * @param persistance From the persisted data
27
28
  */
28
- getData<T>(key: string, persistance?: boolean): T | undefined;
29
+ getData<T>(key: string): T | undefined;
29
30
  /**
30
31
  * Get data with default value
31
32
  * @param key Key name
32
33
  * @param defaultValue Default value
33
- * @param persistance From the persisted data
34
34
  */
35
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
35
+ getData<T>(key: string, defaultValue: T): T;
36
+ /**
37
+ * Get persisted data
38
+ * @param key Key name
39
+ */
40
+ getPersistedData<T>(key: string): T | undefined;
41
+ /**
42
+ * Get persisted data with default value
43
+ * @param key Key name
44
+ * @param defaultValue Default value
45
+ */
46
+ getPersistedData<T>(key: string, defaultValue: T): T;
36
47
  /**
37
48
  * Get object data
38
49
  * @param key Key name
39
- * @param persistance From the persisted data
40
50
  */
41
- getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
51
+ getObject<T extends {}>(key: string): T | undefined;
42
52
  /**
43
- * Set data
53
+ * Get persisted object data
44
54
  * @param key Key name
45
- * @param data Data, null for removal
46
- * @param persistance To the persisted data, false will stop persistance
47
55
  */
48
- setData(key: string, data: unknown, persistance?: boolean): void;
56
+ getPersistedObject<T extends {}>(key: string): T | undefined;
49
57
  /**
50
- * Get current instance count
51
- * @returns Current instance count
58
+ * Set data
59
+ * @param key Key name
60
+ * @param data Data, null for removal
52
61
  */
53
- getInstanceCount(): number;
62
+ setData(key: string, data: unknown): void;
54
63
  /**
55
- * Update instance count
56
- * @param removed Is removed?
57
- * @returns Current instance count
64
+ * Set persisted data
65
+ * @param key Key name
66
+ * @param data Data, null for removal
58
67
  */
59
- updateInstanceCount(removed: boolean): number;
68
+ setPersistedData(key: string, data: unknown): void;
60
69
  }
@@ -9,44 +9,64 @@ const Utils_1 = require("../Utils");
9
9
  */
10
10
  class WindowStorage {
11
11
  /**
12
- * Constructor
13
- * @param globalFields Global fields
14
- * @param callback Field and data callback
12
+ * Clear keys
13
+ * @param keys Keys
14
+ * @param persisted Persisted or session data
15
15
  */
16
- constructor(globalFields, callback) {
17
- this.globalFields = globalFields;
18
- /**
19
- * Instance count field name
20
- */
21
- this.instanceCountField = 'EtsooSmartERPInstanceCount';
22
- // Init instance index
23
- this._instanceIndex = this.getInstanceCount();
24
- // Copy global fields to session storage
25
- globalFields.forEach((field) => {
26
- const data = callback(field, localStorage.getItem(field), this._instanceIndex);
27
- if (data == null)
28
- sessionStorage.removeItem(field);
16
+ clear(keys, persisted) {
17
+ keys.forEach((key) => {
18
+ if (persisted)
19
+ this.setPersistedData(key, undefined);
29
20
  else
30
- sessionStorage.setItem(field, data);
21
+ this.setData(key, undefined);
31
22
  });
32
23
  }
33
24
  /**
34
- * Current instance index
25
+ * Copy keys to session from persisted source
26
+ * @param keys Keys
27
+ * @param removeSource Remove from the source
35
28
  */
36
- get instanceIndex() {
37
- return this._instanceIndex;
29
+ copyFrom(keys, removeSource) {
30
+ keys.forEach((key) => {
31
+ this.setData(key, this.getPersistedData(key));
32
+ if (removeSource)
33
+ this.setPersistedData(key, undefined);
34
+ });
35
+ }
36
+ /**
37
+ * Copy keys to persisted source
38
+ * @param keys Keys
39
+ * @param removeSource Remove from the source
40
+ */
41
+ copyTo(keys, removeSource) {
42
+ keys.forEach((key) => {
43
+ this.setPersistedData(key, this.getData(key));
44
+ if (removeSource)
45
+ this.setData(key, undefined);
46
+ });
38
47
  }
39
48
  /**
40
49
  * Get data
41
50
  * @param key Key name
42
51
  * @param defaultValue Default value
43
- * @param persistance From the persisted data
44
52
  */
45
- getData(key, defaultValue, persistance) {
53
+ getData(key, defaultValue) {
46
54
  // Get storage
47
- const data = persistance
48
- ? localStorage.getItem(key)
49
- : sessionStorage.getItem(key);
55
+ const data = sessionStorage.getItem(key);
56
+ // No default value
57
+ if (defaultValue == null)
58
+ return Utils_1.Utils.parseString(data);
59
+ // Return
60
+ return Utils_1.Utils.parseString(data, defaultValue);
61
+ }
62
+ /**
63
+ * Get persisted data
64
+ * @param key Key name
65
+ * @param defaultValue Default value
66
+ */
67
+ getPersistedData(key, defaultValue) {
68
+ // Get storage
69
+ const data = localStorage.getItem(key);
50
70
  // No default value
51
71
  if (defaultValue == null)
52
72
  return Utils_1.Utils.parseString(data);
@@ -56,53 +76,40 @@ class WindowStorage {
56
76
  /**
57
77
  * Get object data
58
78
  * @param key Key name
59
- * @param persistance From the persisted data
60
79
  */
61
- getObject(key, persistance) {
80
+ getObject(key) {
62
81
  // Get storage
63
- const data = persistance
64
- ? localStorage.getItem(key)
65
- : sessionStorage.getItem(key);
82
+ const data = sessionStorage.getItem(key);
66
83
  if (data == null)
67
84
  return undefined;
68
85
  return JSON.parse(data);
69
86
  }
70
87
  /**
71
- * Set data
88
+ * Get persisted object data
72
89
  * @param key Key name
73
- * @param data Data, null for removal
74
- * @param persistance To the persisted data, false will stop persistance
75
90
  */
76
- setData(key, data, persistance) {
77
- if (persistance) {
78
- StorageUtils_1.StorageUtils.setLocalData(key, data);
79
- return;
80
- }
81
- StorageUtils_1.StorageUtils.setSessionData(key, data);
82
- if (persistance !== false && this.globalFields.includes(key)) {
83
- StorageUtils_1.StorageUtils.setLocalData(key, data);
84
- }
91
+ getPersistedObject(key) {
92
+ // Get storage
93
+ const data = localStorage.getItem(key);
94
+ if (data == null)
95
+ return undefined;
96
+ return JSON.parse(data);
85
97
  }
86
98
  /**
87
- * Get current instance count
88
- * @returns Current instance count
99
+ * Set data
100
+ * @param key Key name
101
+ * @param data Data, null for removal
89
102
  */
90
- getInstanceCount() {
91
- const count = this.getData(this.instanceCountField, 0, true);
92
- // Make sure starting from 0
93
- if (count < 0)
94
- return 0;
95
- return count;
103
+ setData(key, data) {
104
+ StorageUtils_1.StorageUtils.setSessionData(key, data);
96
105
  }
97
106
  /**
98
- * Update instance count
99
- * @param removed Is removed?
100
- * @returns Current instance count
107
+ * Set persisted data
108
+ * @param key Key name
109
+ * @param data Data, null for removal
101
110
  */
102
- updateInstanceCount(removed) {
103
- const count = this.getInstanceCount() + (removed ? -1 : 1);
104
- this.setData(this.instanceCountField, count, true);
105
- return count;
111
+ setPersistedData(key, data) {
112
+ StorageUtils_1.StorageUtils.setLocalData(key, data);
106
113
  }
107
114
  }
108
115
  exports.WindowStorage = WindowStorage;
@@ -3,56 +3,65 @@
3
3
  */
4
4
  export interface IStorage {
5
5
  /**
6
- * Current instance index
6
+ * Clear keys
7
+ * @param keys Keys
8
+ * @param persisted Persisted or session data
7
9
  */
8
- readonly instanceIndex: number;
10
+ clear(keys: string[], persisted?: boolean): void;
11
+ /**
12
+ * Copy keys to session from persisted source
13
+ * @param keys Keys
14
+ * @param removeSource Remove from the source
15
+ */
16
+ copyFrom(keys: string[], removeSource?: boolean): void;
17
+ /**
18
+ * Copy keys to persisted source
19
+ * @param keys Keys
20
+ * @param removeSource Remove from the source
21
+ */
22
+ copyTo(keys: string[], removeSource?: boolean): void;
9
23
  /**
10
24
  * Get data
11
25
  * @param key Key name
12
- * @param persistance From the persisted data
13
26
  */
14
- getData<T>(key: string, persistance?: boolean): T | undefined;
27
+ getData<T>(key: string): T | undefined;
15
28
  /**
16
29
  * Get data with default value
17
30
  * @param key Key name
18
31
  * @param defaultValue Default value
19
- * @param persistance From the persisted data
20
32
  */
21
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
33
+ getData<T>(key: string, defaultValue: T): T;
34
+ /**
35
+ * Get persisted data
36
+ * @param key Key name
37
+ */
38
+ getPersistedData<T>(key: string): T | undefined;
39
+ /**
40
+ * Get persisted data with default value
41
+ * @param key Key name
42
+ * @param defaultValue Default value
43
+ */
44
+ getPersistedData<T>(key: string, defaultValue: T): T;
22
45
  /**
23
46
  * Get object data
24
47
  * @param key Key name
25
- * @param persistance From the persisted data
26
48
  */
27
- getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
49
+ getObject<T extends {}>(key: string): T | undefined;
28
50
  /**
29
- * Set data
51
+ * Get persisted object data
30
52
  * @param key Key name
31
- * @param data Data, null for removal
32
- * @param persistance Persist the data, false will stop persistance
33
53
  */
34
- setData(key: string, data: unknown, persistance?: boolean): void;
54
+ getPersistedObject<T extends {}>(key: string): T | undefined;
35
55
  /**
36
- * Get current instance count
37
- * @returns Current instance count
56
+ * Set data
57
+ * @param key Key name
58
+ * @param data Data, null for removal
38
59
  */
39
- getInstanceCount(): number;
60
+ setData(key: string, data: unknown): void;
40
61
  /**
41
- * Update instance count
42
- * @param removed Is removed?
43
- * @returns Current instance count
62
+ * Set persisted data
63
+ * @param key Key name
64
+ * @param data Data, null for removal
44
65
  */
45
- updateInstanceCount(removed: boolean): number;
46
- }
47
- /**
48
- * Storage init callback
49
- */
50
- export interface IStorageInitCallback {
51
- (field: string, data: string | null, instanceIndex: number): string | null;
52
- }
53
- /**
54
- * Storage constructor interface
55
- */
56
- export interface IStorageConstructor {
57
- new (globalFields: string[], callback: IStorageInitCallback): IStorage;
66
+ setPersistedData(key: string, data: unknown): void;
58
67
  }
@@ -1,60 +1,69 @@
1
- import { IStorage, IStorageInitCallback } from './IStorage';
1
+ import { IStorage } from './IStorage';
2
2
  /**
3
3
  * Window storage
4
4
  * https://developer.mozilla.org/en-US/docs/Web/API/Storage
5
5
  */
6
6
  export declare class WindowStorage implements IStorage {
7
- private globalFields;
8
7
  /**
9
- * Instance count field name
8
+ * Clear keys
9
+ * @param keys Keys
10
+ * @param persisted Persisted or session data
10
11
  */
11
- private readonly instanceCountField;
12
- private _instanceIndex;
12
+ clear(keys: string[], persisted?: boolean): void;
13
13
  /**
14
- * Current instance index
14
+ * Copy keys to session from persisted source
15
+ * @param keys Keys
16
+ * @param removeSource Remove from the source
15
17
  */
16
- get instanceIndex(): number;
18
+ copyFrom(keys: string[], removeSource?: boolean): void;
17
19
  /**
18
- * Constructor
19
- * @param globalFields Global fields
20
- * @param callback Field and data callback
20
+ * Copy keys to persisted source
21
+ * @param keys Keys
22
+ * @param removeSource Remove from the source
21
23
  */
22
- constructor(globalFields: string[], callback: IStorageInitCallback);
24
+ copyTo(keys: string[], removeSource?: boolean): void;
23
25
  /**
24
26
  * Get data
25
27
  * @param key Key name
26
- * @param persistance From the persisted data
27
28
  */
28
- getData<T>(key: string, persistance?: boolean): T | undefined;
29
+ getData<T>(key: string): T | undefined;
29
30
  /**
30
31
  * Get data with default value
31
32
  * @param key Key name
32
33
  * @param defaultValue Default value
33
- * @param persistance From the persisted data
34
34
  */
35
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
35
+ getData<T>(key: string, defaultValue: T): T;
36
+ /**
37
+ * Get persisted data
38
+ * @param key Key name
39
+ */
40
+ getPersistedData<T>(key: string): T | undefined;
41
+ /**
42
+ * Get persisted data with default value
43
+ * @param key Key name
44
+ * @param defaultValue Default value
45
+ */
46
+ getPersistedData<T>(key: string, defaultValue: T): T;
36
47
  /**
37
48
  * Get object data
38
49
  * @param key Key name
39
- * @param persistance From the persisted data
40
50
  */
41
- getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
51
+ getObject<T extends {}>(key: string): T | undefined;
42
52
  /**
43
- * Set data
53
+ * Get persisted object data
44
54
  * @param key Key name
45
- * @param data Data, null for removal
46
- * @param persistance To the persisted data, false will stop persistance
47
55
  */
48
- setData(key: string, data: unknown, persistance?: boolean): void;
56
+ getPersistedObject<T extends {}>(key: string): T | undefined;
49
57
  /**
50
- * Get current instance count
51
- * @returns Current instance count
58
+ * Set data
59
+ * @param key Key name
60
+ * @param data Data, null for removal
52
61
  */
53
- getInstanceCount(): number;
62
+ setData(key: string, data: unknown): void;
54
63
  /**
55
- * Update instance count
56
- * @param removed Is removed?
57
- * @returns Current instance count
64
+ * Set persisted data
65
+ * @param key Key name
66
+ * @param data Data, null for removal
58
67
  */
59
- updateInstanceCount(removed: boolean): number;
68
+ setPersistedData(key: string, data: unknown): void;
60
69
  }
@@ -6,44 +6,64 @@ import { Utils } from '../Utils';
6
6
  */
7
7
  export class WindowStorage {
8
8
  /**
9
- * Constructor
10
- * @param globalFields Global fields
11
- * @param callback Field and data callback
9
+ * Clear keys
10
+ * @param keys Keys
11
+ * @param persisted Persisted or session data
12
12
  */
13
- constructor(globalFields, callback) {
14
- this.globalFields = globalFields;
15
- /**
16
- * Instance count field name
17
- */
18
- this.instanceCountField = 'EtsooSmartERPInstanceCount';
19
- // Init instance index
20
- this._instanceIndex = this.getInstanceCount();
21
- // Copy global fields to session storage
22
- globalFields.forEach((field) => {
23
- const data = callback(field, localStorage.getItem(field), this._instanceIndex);
24
- if (data == null)
25
- sessionStorage.removeItem(field);
13
+ clear(keys, persisted) {
14
+ keys.forEach((key) => {
15
+ if (persisted)
16
+ this.setPersistedData(key, undefined);
26
17
  else
27
- sessionStorage.setItem(field, data);
18
+ this.setData(key, undefined);
28
19
  });
29
20
  }
30
21
  /**
31
- * Current instance index
22
+ * Copy keys to session from persisted source
23
+ * @param keys Keys
24
+ * @param removeSource Remove from the source
32
25
  */
33
- get instanceIndex() {
34
- return this._instanceIndex;
26
+ copyFrom(keys, removeSource) {
27
+ keys.forEach((key) => {
28
+ this.setData(key, this.getPersistedData(key));
29
+ if (removeSource)
30
+ this.setPersistedData(key, undefined);
31
+ });
32
+ }
33
+ /**
34
+ * Copy keys to persisted source
35
+ * @param keys Keys
36
+ * @param removeSource Remove from the source
37
+ */
38
+ copyTo(keys, removeSource) {
39
+ keys.forEach((key) => {
40
+ this.setPersistedData(key, this.getData(key));
41
+ if (removeSource)
42
+ this.setData(key, undefined);
43
+ });
35
44
  }
36
45
  /**
37
46
  * Get data
38
47
  * @param key Key name
39
48
  * @param defaultValue Default value
40
- * @param persistance From the persisted data
41
49
  */
42
- getData(key, defaultValue, persistance) {
50
+ getData(key, defaultValue) {
43
51
  // Get storage
44
- const data = persistance
45
- ? localStorage.getItem(key)
46
- : sessionStorage.getItem(key);
52
+ const data = sessionStorage.getItem(key);
53
+ // No default value
54
+ if (defaultValue == null)
55
+ return Utils.parseString(data);
56
+ // Return
57
+ return Utils.parseString(data, defaultValue);
58
+ }
59
+ /**
60
+ * Get persisted data
61
+ * @param key Key name
62
+ * @param defaultValue Default value
63
+ */
64
+ getPersistedData(key, defaultValue) {
65
+ // Get storage
66
+ const data = localStorage.getItem(key);
47
67
  // No default value
48
68
  if (defaultValue == null)
49
69
  return Utils.parseString(data);
@@ -53,52 +73,39 @@ export class WindowStorage {
53
73
  /**
54
74
  * Get object data
55
75
  * @param key Key name
56
- * @param persistance From the persisted data
57
76
  */
58
- getObject(key, persistance) {
77
+ getObject(key) {
59
78
  // Get storage
60
- const data = persistance
61
- ? localStorage.getItem(key)
62
- : sessionStorage.getItem(key);
79
+ const data = sessionStorage.getItem(key);
63
80
  if (data == null)
64
81
  return undefined;
65
82
  return JSON.parse(data);
66
83
  }
67
84
  /**
68
- * Set data
85
+ * Get persisted object data
69
86
  * @param key Key name
70
- * @param data Data, null for removal
71
- * @param persistance To the persisted data, false will stop persistance
72
87
  */
73
- setData(key, data, persistance) {
74
- if (persistance) {
75
- StorageUtils.setLocalData(key, data);
76
- return;
77
- }
78
- StorageUtils.setSessionData(key, data);
79
- if (persistance !== false && this.globalFields.includes(key)) {
80
- StorageUtils.setLocalData(key, data);
81
- }
88
+ getPersistedObject(key) {
89
+ // Get storage
90
+ const data = localStorage.getItem(key);
91
+ if (data == null)
92
+ return undefined;
93
+ return JSON.parse(data);
82
94
  }
83
95
  /**
84
- * Get current instance count
85
- * @returns Current instance count
96
+ * Set data
97
+ * @param key Key name
98
+ * @param data Data, null for removal
86
99
  */
87
- getInstanceCount() {
88
- const count = this.getData(this.instanceCountField, 0, true);
89
- // Make sure starting from 0
90
- if (count < 0)
91
- return 0;
92
- return count;
100
+ setData(key, data) {
101
+ StorageUtils.setSessionData(key, data);
93
102
  }
94
103
  /**
95
- * Update instance count
96
- * @param removed Is removed?
97
- * @returns Current instance count
104
+ * Set persisted data
105
+ * @param key Key name
106
+ * @param data Data, null for removal
98
107
  */
99
- updateInstanceCount(removed) {
100
- const count = this.getInstanceCount() + (removed ? -1 : 1);
101
- this.setData(this.instanceCountField, count, true);
102
- return count;
108
+ setPersistedData(key, data) {
109
+ StorageUtils.setLocalData(key, data);
103
110
  }
104
111
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.90",
3
+ "version": "1.0.94",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -3,64 +3,75 @@
3
3
  */
4
4
  export interface IStorage {
5
5
  /**
6
- * Current instance index
6
+ * Clear keys
7
+ * @param keys Keys
8
+ * @param persisted Persisted or session data
7
9
  */
8
- readonly instanceIndex: number;
10
+ clear(keys: string[], persisted?: boolean): void;
11
+
12
+ /**
13
+ * Copy keys to session from persisted source
14
+ * @param keys Keys
15
+ * @param removeSource Remove from the source
16
+ */
17
+ copyFrom(keys: string[], removeSource?: boolean): void;
18
+
19
+ /**
20
+ * Copy keys to persisted source
21
+ * @param keys Keys
22
+ * @param removeSource Remove from the source
23
+ */
24
+ copyTo(keys: string[], removeSource?: boolean): void;
9
25
 
10
26
  /**
11
27
  * Get data
12
28
  * @param key Key name
13
- * @param persistance From the persisted data
14
29
  */
15
- getData<T>(key: string, persistance?: boolean): T | undefined;
30
+ getData<T>(key: string): T | undefined;
16
31
 
17
32
  /**
18
33
  * Get data with default value
19
34
  * @param key Key name
20
35
  * @param defaultValue Default value
21
- * @param persistance From the persisted data
22
36
  */
23
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
37
+ getData<T>(key: string, defaultValue: T): T;
24
38
 
25
39
  /**
26
- * Get object data
40
+ * Get persisted data
27
41
  * @param key Key name
28
- * @param persistance From the persisted data
29
42
  */
30
- getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
43
+ getPersistedData<T>(key: string): T | undefined;
31
44
 
32
45
  /**
33
- * Set data
46
+ * Get persisted data with default value
34
47
  * @param key Key name
35
- * @param data Data, null for removal
36
- * @param persistance Persist the data, false will stop persistance
48
+ * @param defaultValue Default value
37
49
  */
38
- setData(key: string, data: unknown, persistance?: boolean): void;
50
+ getPersistedData<T>(key: string, defaultValue: T): T;
39
51
 
40
52
  /**
41
- * Get current instance count
42
- * @returns Current instance count
53
+ * Get object data
54
+ * @param key Key name
43
55
  */
44
- getInstanceCount(): number;
56
+ getObject<T extends {}>(key: string): T | undefined;
45
57
 
46
58
  /**
47
- * Update instance count
48
- * @param removed Is removed?
49
- * @returns Current instance count
59
+ * Get persisted object data
60
+ * @param key Key name
50
61
  */
51
- updateInstanceCount(removed: boolean): number;
52
- }
62
+ getPersistedObject<T extends {}>(key: string): T | undefined;
53
63
 
54
- /**
55
- * Storage init callback
56
- */
57
- export interface IStorageInitCallback {
58
- (field: string, data: string | null, instanceIndex: number): string | null;
59
- }
64
+ /**
65
+ * Set data
66
+ * @param key Key name
67
+ * @param data Data, null for removal
68
+ */
69
+ setData(key: string, data: unknown): void;
60
70
 
61
- /**
62
- * Storage constructor interface
63
- */
64
- export interface IStorageConstructor {
65
- new (globalFields: string[], callback: IStorageInitCallback): IStorage;
71
+ /**
72
+ * Set persisted data
73
+ * @param key Key name
74
+ * @param data Data, null for removal
75
+ */
76
+ setPersistedData(key: string, data: unknown): void;
66
77
  }
@@ -1,6 +1,6 @@
1
1
  import { StorageUtils } from '../StorageUtils';
2
2
  import { Utils } from '../Utils';
3
- import { IStorage, IStorageInitCallback } from './IStorage';
3
+ import { IStorage } from './IStorage';
4
4
 
5
5
  /**
6
6
  * Window storage
@@ -8,72 +8,91 @@ import { IStorage, IStorageInitCallback } from './IStorage';
8
8
  */
9
9
  export class WindowStorage implements IStorage {
10
10
  /**
11
- * Instance count field name
11
+ * Clear keys
12
+ * @param keys Keys
13
+ * @param persisted Persisted or session data
12
14
  */
13
- private readonly instanceCountField = 'EtsooSmartERPInstanceCount';
15
+ clear(keys: string[], persisted?: boolean) {
16
+ keys.forEach((key) => {
17
+ if (persisted) this.setPersistedData(key, undefined);
18
+ else this.setData(key, undefined);
19
+ });
20
+ }
14
21
 
15
- private _instanceIndex: number;
16
22
  /**
17
- * Current instance index
23
+ * Copy keys to session from persisted source
24
+ * @param keys Keys
25
+ * @param removeSource Remove from the source
18
26
  */
19
- get instanceIndex() {
20
- return this._instanceIndex;
27
+ copyFrom(keys: string[], removeSource?: boolean) {
28
+ keys.forEach((key) => {
29
+ this.setData(key, this.getPersistedData(key));
30
+ if (removeSource) this.setPersistedData(key, undefined);
31
+ });
21
32
  }
22
33
 
23
34
  /**
24
- * Constructor
25
- * @param globalFields Global fields
26
- * @param callback Field and data callback
35
+ * Copy keys to persisted source
36
+ * @param keys Keys
37
+ * @param removeSource Remove from the source
27
38
  */
28
- constructor(
29
- private globalFields: string[],
30
- callback: IStorageInitCallback
31
- ) {
32
- // Init instance index
33
- this._instanceIndex = this.getInstanceCount();
34
-
35
- // Copy global fields to session storage
36
- globalFields.forEach((field) => {
37
- const data = callback(
38
- field,
39
- localStorage.getItem(field),
40
- this._instanceIndex
41
- );
42
- if (data == null) sessionStorage.removeItem(field);
43
- else sessionStorage.setItem(field, data);
39
+ copyTo(keys: string[], removeSource?: boolean) {
40
+ keys.forEach((key) => {
41
+ this.setPersistedData(key, this.getData(key));
42
+ if (removeSource) this.setData(key, undefined);
44
43
  });
45
44
  }
46
45
 
47
46
  /**
48
47
  * Get data
49
48
  * @param key Key name
50
- * @param persistance From the persisted data
51
49
  */
52
- getData<T>(key: string, persistance?: boolean): T | undefined;
50
+ getData<T>(key: string): T | undefined;
53
51
 
54
52
  /**
55
53
  * Get data with default value
56
54
  * @param key Key name
57
55
  * @param defaultValue Default value
58
- * @param persistance From the persisted data
59
56
  */
60
- getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
57
+ getData<T>(key: string, defaultValue: T): T;
61
58
 
62
59
  /**
63
60
  * Get data
64
61
  * @param key Key name
65
62
  * @param defaultValue Default value
66
- * @param persistance From the persisted data
67
63
  */
68
- getData<T>(
69
- key: string,
70
- defaultValue?: T,
71
- persistance?: boolean
72
- ): T | undefined {
64
+ getData<T>(key: string, defaultValue?: T): T | undefined {
65
+ // Get storage
66
+ const data = sessionStorage.getItem(key);
67
+
68
+ // No default value
69
+ if (defaultValue == null) return Utils.parseString<T>(data);
70
+
71
+ // Return
72
+ return Utils.parseString<T>(data, defaultValue);
73
+ }
74
+
75
+ /**
76
+ * Get persisted data
77
+ * @param key Key name
78
+ */
79
+ getPersistedData<T>(key: string): T | undefined;
80
+
81
+ /**
82
+ * Get persisted data with default value
83
+ * @param key Key name
84
+ * @param defaultValue Default value
85
+ */
86
+ getPersistedData<T>(key: string, defaultValue: T): T;
87
+
88
+ /**
89
+ * Get persisted data
90
+ * @param key Key name
91
+ * @param defaultValue Default value
92
+ */
93
+ getPersistedData<T>(key: string, defaultValue?: T): T | undefined {
73
94
  // Get storage
74
- const data = persistance
75
- ? localStorage.getItem(key)
76
- : sessionStorage.getItem(key);
95
+ const data = localStorage.getItem(key);
77
96
 
78
97
  // No default value
79
98
  if (defaultValue == null) return Utils.parseString<T>(data);
@@ -85,13 +104,10 @@ export class WindowStorage implements IStorage {
85
104
  /**
86
105
  * Get object data
87
106
  * @param key Key name
88
- * @param persistance From the persisted data
89
107
  */
90
- getObject<T extends {}>(key: string, persistance?: boolean) {
108
+ getObject<T extends {}>(key: string) {
91
109
  // Get storage
92
- const data = persistance
93
- ? localStorage.getItem(key)
94
- : sessionStorage.getItem(key);
110
+ const data = sessionStorage.getItem(key);
95
111
 
96
112
  if (data == null) return undefined;
97
113
 
@@ -99,42 +115,33 @@ export class WindowStorage implements IStorage {
99
115
  }
100
116
 
101
117
  /**
102
- * Set data
118
+ * Get persisted object data
103
119
  * @param key Key name
104
- * @param data Data, null for removal
105
- * @param persistance To the persisted data, false will stop persistance
106
120
  */
107
- setData(key: string, data: unknown, persistance?: boolean) {
108
- if (persistance) {
109
- StorageUtils.setLocalData(key, data);
110
- return;
111
- }
121
+ getPersistedObject<T extends {}>(key: string) {
122
+ // Get storage
123
+ const data = localStorage.getItem(key);
112
124
 
113
- StorageUtils.setSessionData(key, data);
114
- if (persistance !== false && this.globalFields.includes(key)) {
115
- StorageUtils.setLocalData(key, data);
116
- }
125
+ if (data == null) return undefined;
126
+
127
+ return <T>JSON.parse(data);
117
128
  }
118
129
 
119
130
  /**
120
- * Get current instance count
121
- * @returns Current instance count
131
+ * Set data
132
+ * @param key Key name
133
+ * @param data Data, null for removal
122
134
  */
123
- getInstanceCount() {
124
- const count = this.getData(this.instanceCountField, 0, true);
125
- // Make sure starting from 0
126
- if (count < 0) return 0;
127
- return count;
135
+ setData(key: string, data: unknown) {
136
+ StorageUtils.setSessionData(key, data);
128
137
  }
129
138
 
130
139
  /**
131
- * Update instance count
132
- * @param removed Is removed?
133
- * @returns Current instance count
140
+ * Set persisted data
141
+ * @param key Key name
142
+ * @param data Data, null for removal
134
143
  */
135
- updateInstanceCount(removed: boolean) {
136
- const count = this.getInstanceCount() + (removed ? -1 : 1);
137
- this.setData(this.instanceCountField, count, true);
138
- return count;
144
+ setPersistedData(key: string, data: unknown) {
145
+ StorageUtils.setLocalData(key, data);
139
146
  }
140
147
  }
@@ -1,16 +0,0 @@
1
- import { WindowStorage } from '../src/storage/WindowStorage';
2
-
3
- // Arrange
4
- const storage = new WindowStorage([], (_field, data, _index) => data);
5
-
6
- test('Tests for getInstanceCount', () => {
7
- expect(storage.getInstanceCount()).toBe(0);
8
- });
9
-
10
- test('Tests for updateInstanceCount / getInstanceCount', () => {
11
- // Current index -1
12
- storage.updateInstanceCount(true);
13
-
14
- // Always make sure it starts with 0
15
- expect(storage.getInstanceCount()).toBe(0);
16
- });