@etsoo/shared 1.0.91 → 1.0.95

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 CHANGED
@@ -38,6 +38,8 @@ Data type definitions and type safe functions
38
38
  |HAlign|Horizontal align|
39
39
  |HAlignEnum|Horizontal align enum|
40
40
  |IdType|Number and string combination id type|
41
+ |IdItem|Item with id or id generator|
42
+ |IdLabelItem|Item with id and label|
41
43
  |Simple|Basic or basic array type|
42
44
  |SimpleEnum|Simple type enum|
43
45
  |SimpleNames|Simple type names|
@@ -56,6 +58,8 @@ Data type definitions and type safe functions
56
58
  |getEnumByValue|Get enum item from value|
57
59
  |getEnumKey|Get enum string literal type value|
58
60
  |getEnumKeys|Get Enum keys|
61
+ |getItemId|Get item id|
62
+ |getItemLabel|Get item label|
59
63
  |isBasicName|Check the type is a basic type or not (type guard)|
60
64
  |isSimpleObject|Is the target a simple object, all values are simple type (Type guard)|
61
65
  |isSimpleType|Is the input value simple type, include null and undefined|
@@ -74,6 +74,32 @@ test('Tests for getEnumKeys', () => {
74
74
  expect(DataTypes.getEnumKeys(ProductUnit)).toContainEqual('GRAM');
75
75
  });
76
76
 
77
+ test('Tests for getItemId', () => {
78
+ // Arrange
79
+ const items: DataTypes.IdItem[] = [
80
+ { id: 1 },
81
+ { id: '123' },
82
+ { id: () => 'f123' }
83
+ ];
84
+
85
+ // Assert
86
+ expect(DataTypes.getItemId(items[0])).toBe('1');
87
+ expect(DataTypes.getItemId(items[2])).toBe('f123');
88
+ });
89
+
90
+ test('Tests for getItemLabel', () => {
91
+ // Arrange
92
+ const items: DataTypes.IdLabelItem[] = [
93
+ { id: 1, label: '123' },
94
+ { id: '123', label: () => 'f123' },
95
+ { id: () => 'f123', label: 'l123' }
96
+ ];
97
+
98
+ // Assert
99
+ expect(DataTypes.getItemLabel(items[0])).toBe('123');
100
+ expect(DataTypes.getItemLabel(items[1])).toBe('f123');
101
+ });
102
+
77
103
  test('Tests for isSimpleType', () => {
78
104
  expect(DataTypes.isSimpleType(1)).toBeTruthy();
79
105
  expect(DataTypes.isSimpleType(new Date())).toBeTruthy();
@@ -7,12 +7,17 @@ test('Tests for all', () => {
7
7
  StorageUtils.setSessionData('number', 3.14);
8
8
  StorageUtils.setSessionData('test', { id: 123, name: 'test' });
9
9
 
10
+ const array = StorageUtils.getSessionData<string[]>('array', []);
11
+ array.push('new');
12
+ StorageUtils.setSessionData('array', array);
13
+
10
14
  expect(StorageUtils.getSessionData<string>('string')).toBe('test');
11
15
  expect(StorageUtils.getSessionData('string1', '')).toBe('');
12
16
  expect(StorageUtils.getSessionData('boolean', false)).toBe(true);
13
17
  expect(StorageUtils.getSessionData('number', 0)).toBe(3.14);
14
18
  expect(StorageUtils.getSessionData('number1', 0)).toBe(0);
15
19
  expect(StorageUtils.getSessionData('test', {})).toHaveProperty('id', 123);
20
+ expect(StorageUtils.getSessionData<string[]>('array')).toEqual(array);
16
21
  });
17
22
 
18
23
  test('Tests for getLocalObject', () => {
@@ -133,6 +133,36 @@ export declare namespace DataTypes {
133
133
  * Simple object, string key, simple type and null value Record
134
134
  */
135
135
  type SimpleObject = Record<string, Simple | null | undefined>;
136
+ /**
137
+ * Item with id property
138
+ */
139
+ type IdItem = {
140
+ /**
141
+ * Id field or generator
142
+ */
143
+ id: IdType | (() => string);
144
+ };
145
+ /**
146
+ * Item with id and label property
147
+ */
148
+ type IdLabelItem = IdItem & {
149
+ /**
150
+ * label field or generator
151
+ */
152
+ label: string | (() => string);
153
+ };
154
+ /**
155
+ * Get item id
156
+ * @param item Item with id
157
+ * @returns string id
158
+ */
159
+ const getItemId: (item: IdItem) => string;
160
+ /**
161
+ * Get item label
162
+ * @param item Item with id
163
+ * @returns string id
164
+ */
165
+ const getItemLabel: (item: IdLabelItem) => string;
136
166
  /**
137
167
  * Culture definiton
138
168
  */
@@ -74,6 +74,28 @@ var DataTypes;
74
74
  VAlignEnum[VAlignEnum["Center"] = 2] = "Center";
75
75
  VAlignEnum[VAlignEnum["Bottom"] = 3] = "Bottom";
76
76
  })(VAlignEnum = DataTypes.VAlignEnum || (DataTypes.VAlignEnum = {}));
77
+ /**
78
+ * Get item id
79
+ * @param item Item with id
80
+ * @returns string id
81
+ */
82
+ DataTypes.getItemId = (item) => {
83
+ if (typeof item.id === 'function')
84
+ return item.id();
85
+ if (typeof item.id === 'number')
86
+ return item.id.toString();
87
+ return item.id;
88
+ };
89
+ /**
90
+ * Get item label
91
+ * @param item Item with id
92
+ * @returns string id
93
+ */
94
+ DataTypes.getItemLabel = (item) => {
95
+ if (typeof item.label === 'function')
96
+ return item.label();
97
+ return item.label;
98
+ };
77
99
  /**
78
100
  * Convert value to target type
79
101
  * @param input Input value
@@ -3,9 +3,23 @@
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
@@ -50,27 +64,4 @@ export interface IStorage {
50
64
  * @param data Data, null for removal
51
65
  */
52
66
  setPersistedData(key: string, data: unknown): void;
53
- /**
54
- * Get current instance count
55
- * @returns Current instance count
56
- */
57
- getInstanceCount(): number;
58
- /**
59
- * Update instance count
60
- * @param removed Is removed?
61
- * @returns Current instance count
62
- */
63
- updateInstanceCount(removed: boolean): number;
64
- }
65
- /**
66
- * Storage init callback
67
- */
68
- export interface IStorageInitCallback {
69
- (field: string, data: string | null, instanceIndex: number): string | null;
70
- }
71
- /**
72
- * Storage constructor interface
73
- */
74
- export interface IStorageConstructor {
75
- new (globalFields: string[], callback: IStorageInitCallback): IStorage;
76
67
  }
@@ -1,25 +1,27 @@
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
- protected persistedFields: string[];
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 persistedFields Persisted 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(persistedFields: string[], callback: IStorageInitCallback);
24
+ copyTo(keys: string[], removeSource?: boolean): void;
23
25
  /**
24
26
  * Get data
25
27
  * @param key Key name
@@ -64,15 +66,4 @@ export declare class WindowStorage implements IStorage {
64
66
  * @param data Data, null for removal
65
67
  */
66
68
  setPersistedData(key: string, data: unknown): void;
67
- /**
68
- * Get current instance count
69
- * @returns Current instance count
70
- */
71
- getInstanceCount(): number;
72
- /**
73
- * Update instance count
74
- * @param removed Is removed?
75
- * @returns Current instance count
76
- */
77
- updateInstanceCount(removed: boolean): number;
78
69
  }
@@ -9,32 +9,41 @@ const Utils_1 = require("../Utils");
9
9
  */
10
10
  class WindowStorage {
11
11
  /**
12
- * Constructor
13
- * @param persistedFields Persisted 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(persistedFields, callback) {
17
- this.persistedFields = persistedFields;
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
- persistedFields.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
@@ -93,9 +102,6 @@ class WindowStorage {
93
102
  */
94
103
  setData(key, data) {
95
104
  StorageUtils_1.StorageUtils.setSessionData(key, data);
96
- if (this.persistedFields.includes(key)) {
97
- this.setPersistedData(key, data);
98
- }
99
105
  }
100
106
  /**
101
107
  * Set persisted data
@@ -105,26 +111,5 @@ class WindowStorage {
105
111
  setPersistedData(key, data) {
106
112
  StorageUtils_1.StorageUtils.setLocalData(key, data);
107
113
  }
108
- /**
109
- * Get current instance count
110
- * @returns Current instance count
111
- */
112
- getInstanceCount() {
113
- const count = this.getPersistedData(this.instanceCountField, 0);
114
- // Make sure starting from 0
115
- if (count < 0)
116
- return 0;
117
- return count;
118
- }
119
- /**
120
- * Update instance count
121
- * @param removed Is removed?
122
- * @returns Current instance count
123
- */
124
- updateInstanceCount(removed) {
125
- const count = this.getInstanceCount() + (removed ? -1 : 1);
126
- this.setPersistedData(this.instanceCountField, count);
127
- return count;
128
- }
129
114
  }
130
115
  exports.WindowStorage = WindowStorage;
@@ -133,6 +133,36 @@ export declare namespace DataTypes {
133
133
  * Simple object, string key, simple type and null value Record
134
134
  */
135
135
  type SimpleObject = Record<string, Simple | null | undefined>;
136
+ /**
137
+ * Item with id property
138
+ */
139
+ type IdItem = {
140
+ /**
141
+ * Id field or generator
142
+ */
143
+ id: IdType | (() => string);
144
+ };
145
+ /**
146
+ * Item with id and label property
147
+ */
148
+ type IdLabelItem = IdItem & {
149
+ /**
150
+ * label field or generator
151
+ */
152
+ label: string | (() => string);
153
+ };
154
+ /**
155
+ * Get item id
156
+ * @param item Item with id
157
+ * @returns string id
158
+ */
159
+ const getItemId: (item: IdItem) => string;
160
+ /**
161
+ * Get item label
162
+ * @param item Item with id
163
+ * @returns string id
164
+ */
165
+ const getItemLabel: (item: IdLabelItem) => string;
136
166
  /**
137
167
  * Culture definiton
138
168
  */
@@ -71,6 +71,28 @@ export var DataTypes;
71
71
  VAlignEnum[VAlignEnum["Center"] = 2] = "Center";
72
72
  VAlignEnum[VAlignEnum["Bottom"] = 3] = "Bottom";
73
73
  })(VAlignEnum = DataTypes.VAlignEnum || (DataTypes.VAlignEnum = {}));
74
+ /**
75
+ * Get item id
76
+ * @param item Item with id
77
+ * @returns string id
78
+ */
79
+ DataTypes.getItemId = (item) => {
80
+ if (typeof item.id === 'function')
81
+ return item.id();
82
+ if (typeof item.id === 'number')
83
+ return item.id.toString();
84
+ return item.id;
85
+ };
86
+ /**
87
+ * Get item label
88
+ * @param item Item with id
89
+ * @returns string id
90
+ */
91
+ DataTypes.getItemLabel = (item) => {
92
+ if (typeof item.label === 'function')
93
+ return item.label();
94
+ return item.label;
95
+ };
74
96
  /**
75
97
  * Convert value to target type
76
98
  * @param input Input value
@@ -3,9 +3,23 @@
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
@@ -50,27 +64,4 @@ export interface IStorage {
50
64
  * @param data Data, null for removal
51
65
  */
52
66
  setPersistedData(key: string, data: unknown): void;
53
- /**
54
- * Get current instance count
55
- * @returns Current instance count
56
- */
57
- getInstanceCount(): number;
58
- /**
59
- * Update instance count
60
- * @param removed Is removed?
61
- * @returns Current instance count
62
- */
63
- updateInstanceCount(removed: boolean): number;
64
- }
65
- /**
66
- * Storage init callback
67
- */
68
- export interface IStorageInitCallback {
69
- (field: string, data: string | null, instanceIndex: number): string | null;
70
- }
71
- /**
72
- * Storage constructor interface
73
- */
74
- export interface IStorageConstructor {
75
- new (globalFields: string[], callback: IStorageInitCallback): IStorage;
76
67
  }
@@ -1,25 +1,27 @@
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
- protected persistedFields: string[];
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 persistedFields Persisted 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(persistedFields: string[], callback: IStorageInitCallback);
24
+ copyTo(keys: string[], removeSource?: boolean): void;
23
25
  /**
24
26
  * Get data
25
27
  * @param key Key name
@@ -64,15 +66,4 @@ export declare class WindowStorage implements IStorage {
64
66
  * @param data Data, null for removal
65
67
  */
66
68
  setPersistedData(key: string, data: unknown): void;
67
- /**
68
- * Get current instance count
69
- * @returns Current instance count
70
- */
71
- getInstanceCount(): number;
72
- /**
73
- * Update instance count
74
- * @param removed Is removed?
75
- * @returns Current instance count
76
- */
77
- updateInstanceCount(removed: boolean): number;
78
69
  }
@@ -6,32 +6,41 @@ import { Utils } from '../Utils';
6
6
  */
7
7
  export class WindowStorage {
8
8
  /**
9
- * Constructor
10
- * @param persistedFields Persisted 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(persistedFields, callback) {
14
- this.persistedFields = persistedFields;
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
- persistedFields.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
@@ -90,9 +99,6 @@ export class WindowStorage {
90
99
  */
91
100
  setData(key, data) {
92
101
  StorageUtils.setSessionData(key, data);
93
- if (this.persistedFields.includes(key)) {
94
- this.setPersistedData(key, data);
95
- }
96
102
  }
97
103
  /**
98
104
  * Set persisted data
@@ -102,25 +108,4 @@ export class WindowStorage {
102
108
  setPersistedData(key, data) {
103
109
  StorageUtils.setLocalData(key, data);
104
110
  }
105
- /**
106
- * Get current instance count
107
- * @returns Current instance count
108
- */
109
- getInstanceCount() {
110
- const count = this.getPersistedData(this.instanceCountField, 0);
111
- // Make sure starting from 0
112
- if (count < 0)
113
- return 0;
114
- return count;
115
- }
116
- /**
117
- * Update instance count
118
- * @param removed Is removed?
119
- * @returns Current instance count
120
- */
121
- updateInstanceCount(removed) {
122
- const count = this.getInstanceCount() + (removed ? -1 : 1);
123
- this.setPersistedData(this.instanceCountField, count);
124
- return count;
125
- }
126
111
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.91",
3
+ "version": "1.0.95",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
package/src/DataTypes.ts CHANGED
@@ -172,6 +172,47 @@ export namespace DataTypes {
172
172
  */
173
173
  export type SimpleObject = Record<string, Simple | null | undefined>;
174
174
 
175
+ /**
176
+ * Item with id property
177
+ */
178
+ export type IdItem = {
179
+ /**
180
+ * Id field or generator
181
+ */
182
+ id: IdType | (() => string);
183
+ };
184
+
185
+ /**
186
+ * Item with id and label property
187
+ */
188
+ export type IdLabelItem = IdItem & {
189
+ /**
190
+ * label field or generator
191
+ */
192
+ label: string | (() => string);
193
+ };
194
+
195
+ /**
196
+ * Get item id
197
+ * @param item Item with id
198
+ * @returns string id
199
+ */
200
+ export const getItemId = (item: IdItem) => {
201
+ if (typeof item.id === 'function') return item.id();
202
+ if (typeof item.id === 'number') return item.id.toString();
203
+ return item.id;
204
+ };
205
+
206
+ /**
207
+ * Get item label
208
+ * @param item Item with id
209
+ * @returns string id
210
+ */
211
+ export const getItemLabel = (item: IdLabelItem) => {
212
+ if (typeof item.label === 'function') return item.label();
213
+ return item.label;
214
+ };
215
+
175
216
  /**
176
217
  * Culture definiton
177
218
  */
@@ -3,9 +3,25 @@
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
@@ -58,31 +74,4 @@ export interface IStorage {
58
74
  * @param data Data, null for removal
59
75
  */
60
76
  setPersistedData(key: string, data: unknown): void;
61
-
62
- /**
63
- * Get current instance count
64
- * @returns Current instance count
65
- */
66
- getInstanceCount(): number;
67
-
68
- /**
69
- * Update instance count
70
- * @param removed Is removed?
71
- * @returns Current instance count
72
- */
73
- updateInstanceCount(removed: boolean): number;
74
- }
75
-
76
- /**
77
- * Storage init callback
78
- */
79
- export interface IStorageInitCallback {
80
- (field: string, data: string | null, instanceIndex: number): string | null;
81
- }
82
-
83
- /**
84
- * Storage constructor interface
85
- */
86
- export interface IStorageConstructor {
87
- new (globalFields: string[], callback: IStorageInitCallback): IStorage;
88
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,39 +8,38 @@ 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 persistedFields Persisted 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
- protected persistedFields: string[],
30
- callback: IStorageInitCallback
31
- ) {
32
- // Init instance index
33
- this._instanceIndex = this.getInstanceCount();
34
-
35
- // Copy global fields to session storage
36
- persistedFields.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
 
@@ -135,9 +134,6 @@ export class WindowStorage implements IStorage {
135
134
  */
136
135
  setData(key: string, data: unknown) {
137
136
  StorageUtils.setSessionData(key, data);
138
- if (this.persistedFields.includes(key)) {
139
- this.setPersistedData(key, data);
140
- }
141
137
  }
142
138
 
143
139
  /**
@@ -148,26 +144,4 @@ export class WindowStorage implements IStorage {
148
144
  setPersistedData(key: string, data: unknown) {
149
145
  StorageUtils.setLocalData(key, data);
150
146
  }
151
-
152
- /**
153
- * Get current instance count
154
- * @returns Current instance count
155
- */
156
- getInstanceCount() {
157
- const count = this.getPersistedData(this.instanceCountField, 0);
158
- // Make sure starting from 0
159
- if (count < 0) return 0;
160
- return count;
161
- }
162
-
163
- /**
164
- * Update instance count
165
- * @param removed Is removed?
166
- * @returns Current instance count
167
- */
168
- updateInstanceCount(removed: boolean) {
169
- const count = this.getInstanceCount() + (removed ? -1 : 1);
170
- this.setPersistedData(this.instanceCountField, count);
171
- return count;
172
- }
173
147
  }
@@ -1,35 +0,0 @@
1
- import { WindowStorage } from '../src/storage/WindowStorage';
2
-
3
- // Arrange
4
- localStorage.setItem('test', 'test');
5
- localStorage.setItem('a', 'a');
6
- const storage = new WindowStorage(['test', 'a'], (field, data, index) => {
7
- if (index === 0 && field === 'test') return null;
8
- return data;
9
- });
10
-
11
- test('Tests for getInstanceCount', () => {
12
- expect(storage.getInstanceCount()).toBe(0);
13
- });
14
-
15
- test('Tests for updateInstanceCount / getInstanceCount', () => {
16
- // Current index -1
17
- storage.updateInstanceCount(true);
18
-
19
- // Always make sure it starts with 0
20
- expect(storage.getInstanceCount()).toBe(0);
21
- });
22
-
23
- test('Tests for getData', () => {
24
- expect(storage.getData('test')).toBeUndefined();
25
- expect(storage.getPersistedData('test')).toBe('test');
26
-
27
- expect(storage.getData('a')).toBe('a');
28
- expect(storage.getPersistedData('a')).toBe('a');
29
- });
30
-
31
- test('Tests for setData', () => {
32
- storage.setData('test', 'test2');
33
- expect(storage.getData('test')).toBe('test2');
34
- expect(storage.getPersistedData('test')).toBe('test2');
35
- });