@etsoo/shared 1.0.91 → 1.0.92

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.
@@ -2,10 +2,6 @@
2
2
  * Storage interface
3
3
  */
4
4
  export interface IStorage {
5
- /**
6
- * Current instance index
7
- */
8
- readonly instanceIndex: number;
9
5
  /**
10
6
  * Get data
11
7
  * @param key Key name
@@ -50,27 +46,4 @@ export interface IStorage {
50
46
  * @param data Data, null for removal
51
47
  */
52
48
  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
49
  }
@@ -1,25 +1,9 @@
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
- /**
9
- * Instance count field name
10
- */
11
- private readonly instanceCountField;
12
- private _instanceIndex;
13
- /**
14
- * Current instance index
15
- */
16
- get instanceIndex(): number;
17
- /**
18
- * Constructor
19
- * @param persistedFields Persisted fields
20
- * @param callback Field and data callback
21
- */
22
- constructor(persistedFields: string[], callback: IStorageInitCallback);
23
7
  /**
24
8
  * Get data
25
9
  * @param key Key name
@@ -64,15 +48,4 @@ export declare class WindowStorage implements IStorage {
64
48
  * @param data Data, null for removal
65
49
  */
66
50
  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
51
  }
@@ -8,34 +8,6 @@ const Utils_1 = require("../Utils");
8
8
  * https://developer.mozilla.org/en-US/docs/Web/API/Storage
9
9
  */
10
10
  class WindowStorage {
11
- /**
12
- * Constructor
13
- * @param persistedFields Persisted fields
14
- * @param callback Field and data callback
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);
29
- else
30
- sessionStorage.setItem(field, data);
31
- });
32
- }
33
- /**
34
- * Current instance index
35
- */
36
- get instanceIndex() {
37
- return this._instanceIndex;
38
- }
39
11
  /**
40
12
  * Get data
41
13
  * @param key Key name
@@ -93,9 +65,6 @@ class WindowStorage {
93
65
  */
94
66
  setData(key, data) {
95
67
  StorageUtils_1.StorageUtils.setSessionData(key, data);
96
- if (this.persistedFields.includes(key)) {
97
- this.setPersistedData(key, data);
98
- }
99
68
  }
100
69
  /**
101
70
  * Set persisted data
@@ -105,26 +74,5 @@ class WindowStorage {
105
74
  setPersistedData(key, data) {
106
75
  StorageUtils_1.StorageUtils.setLocalData(key, data);
107
76
  }
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
77
  }
130
78
  exports.WindowStorage = WindowStorage;
@@ -2,10 +2,6 @@
2
2
  * Storage interface
3
3
  */
4
4
  export interface IStorage {
5
- /**
6
- * Current instance index
7
- */
8
- readonly instanceIndex: number;
9
5
  /**
10
6
  * Get data
11
7
  * @param key Key name
@@ -50,27 +46,4 @@ export interface IStorage {
50
46
  * @param data Data, null for removal
51
47
  */
52
48
  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
49
  }
@@ -1,25 +1,9 @@
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
- /**
9
- * Instance count field name
10
- */
11
- private readonly instanceCountField;
12
- private _instanceIndex;
13
- /**
14
- * Current instance index
15
- */
16
- get instanceIndex(): number;
17
- /**
18
- * Constructor
19
- * @param persistedFields Persisted fields
20
- * @param callback Field and data callback
21
- */
22
- constructor(persistedFields: string[], callback: IStorageInitCallback);
23
7
  /**
24
8
  * Get data
25
9
  * @param key Key name
@@ -64,15 +48,4 @@ export declare class WindowStorage implements IStorage {
64
48
  * @param data Data, null for removal
65
49
  */
66
50
  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
51
  }
@@ -5,34 +5,6 @@ import { Utils } from '../Utils';
5
5
  * https://developer.mozilla.org/en-US/docs/Web/API/Storage
6
6
  */
7
7
  export class WindowStorage {
8
- /**
9
- * Constructor
10
- * @param persistedFields Persisted fields
11
- * @param callback Field and data callback
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);
26
- else
27
- sessionStorage.setItem(field, data);
28
- });
29
- }
30
- /**
31
- * Current instance index
32
- */
33
- get instanceIndex() {
34
- return this._instanceIndex;
35
- }
36
8
  /**
37
9
  * Get data
38
10
  * @param key Key name
@@ -90,9 +62,6 @@ export class WindowStorage {
90
62
  */
91
63
  setData(key, data) {
92
64
  StorageUtils.setSessionData(key, data);
93
- if (this.persistedFields.includes(key)) {
94
- this.setPersistedData(key, data);
95
- }
96
65
  }
97
66
  /**
98
67
  * Set persisted data
@@ -102,25 +71,4 @@ export class WindowStorage {
102
71
  setPersistedData(key, data) {
103
72
  StorageUtils.setLocalData(key, data);
104
73
  }
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
74
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.91",
3
+ "version": "1.0.92",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -2,11 +2,6 @@
2
2
  * Storage interface
3
3
  */
4
4
  export interface IStorage {
5
- /**
6
- * Current instance index
7
- */
8
- readonly instanceIndex: number;
9
-
10
5
  /**
11
6
  * Get data
12
7
  * @param key Key name
@@ -58,31 +53,4 @@ export interface IStorage {
58
53
  * @param data Data, null for removal
59
54
  */
60
55
  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
56
  }
@@ -1,49 +1,12 @@
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
7
7
  * https://developer.mozilla.org/en-US/docs/Web/API/Storage
8
8
  */
9
9
  export class WindowStorage implements IStorage {
10
- /**
11
- * Instance count field name
12
- */
13
- private readonly instanceCountField = 'EtsooSmartERPInstanceCount';
14
-
15
- private _instanceIndex: number;
16
- /**
17
- * Current instance index
18
- */
19
- get instanceIndex() {
20
- return this._instanceIndex;
21
- }
22
-
23
- /**
24
- * Constructor
25
- * @param persistedFields Persisted fields
26
- * @param callback Field and data callback
27
- */
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);
44
- });
45
- }
46
-
47
10
  /**
48
11
  * Get data
49
12
  * @param key Key name
@@ -135,9 +98,6 @@ export class WindowStorage implements IStorage {
135
98
  */
136
99
  setData(key: string, data: unknown) {
137
100
  StorageUtils.setSessionData(key, data);
138
- if (this.persistedFields.includes(key)) {
139
- this.setPersistedData(key, data);
140
- }
141
101
  }
142
102
 
143
103
  /**
@@ -148,26 +108,4 @@ export class WindowStorage implements IStorage {
148
108
  setPersistedData(key: string, data: unknown) {
149
109
  StorageUtils.setLocalData(key, data);
150
110
  }
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
111
  }
@@ -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
- });