@etsoo/shared 1.0.82 → 1.0.86

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
@@ -15,6 +15,9 @@ Using yarn:
15
15
  $ yarn add @etsoo/shared
16
16
  ```
17
17
 
18
+ ## storage
19
+ Storage interface and browser storage implementation
20
+
18
21
  ## DataTypes
19
22
  Data type definitions and type safe functions
20
23
 
@@ -92,8 +95,6 @@ DOM/window related utilities
92
95
  |isJSONContentType|Is JSON content type|
93
96
  |mergeFormData|Merge form data to primary one|
94
97
  |mergeURLSearchParams|Merge URL search parameters|
95
- |saveCountry|Save country name|
96
- |saveCulture|Save culture name|
97
98
  |setFocus|Set HTML element focus by name|
98
99
 
99
100
  ## ExtendUtils
@@ -8,11 +8,11 @@ export declare namespace DomUtils {
8
8
  /**
9
9
  * Language cache parameter name
10
10
  */
11
- const Culture = "culture";
11
+ const CultureField = "culture";
12
12
  /**
13
13
  * Country cache parameter name
14
14
  */
15
- const Country = "country";
15
+ const CountryField = "country";
16
16
  /**
17
17
  * Clear form data
18
18
  * @param data Form data
@@ -94,16 +94,6 @@ export declare namespace DomUtils {
94
94
  * @param data New simple object data to merge
95
95
  */
96
96
  function mergeURLSearchParams(base: URLSearchParams, data: DataTypes.SimpleObject): URLSearchParams;
97
- /**
98
- * Save country name
99
- * @param country Country name
100
- */
101
- function saveCountry(country: string): void;
102
- /**
103
- * Save culture name
104
- * @param culture Culture name
105
- */
106
- function saveCulture(culture: string): void;
107
97
  /**
108
98
  * Set HTML element focus by name
109
99
  * @param name Element name or first collection item
@@ -16,11 +16,11 @@ var DomUtils;
16
16
  /**
17
17
  * Language cache parameter name
18
18
  */
19
- DomUtils.Culture = 'culture';
19
+ DomUtils.CultureField = 'culture';
20
20
  /**
21
21
  * Country cache parameter name
22
22
  */
23
- DomUtils.Country = 'country';
23
+ DomUtils.CountryField = 'country';
24
24
  /**
25
25
  * Clear form data
26
26
  * @param data Form data
@@ -156,12 +156,12 @@ var DomUtils;
156
156
  * Current detected country
157
157
  */
158
158
  DomUtils.detectedCountry = (() => {
159
+ var _a, _b;
159
160
  // URL first, then local storage
160
161
  let country;
161
162
  try {
162
163
  country =
163
- new URL(location.href).searchParams.get(DomUtils.Country) ||
164
- localStorage.getItem(DomUtils.Country);
164
+ (_b = (_a = new URL(location.href).searchParams.get(DomUtils.CountryField)) !== null && _a !== void 0 ? _a : sessionStorage.getItem(DomUtils.CountryField)) !== null && _b !== void 0 ? _b : localStorage.getItem(DomUtils.CountryField);
165
165
  }
166
166
  catch {
167
167
  country = null;
@@ -173,12 +173,12 @@ var DomUtils;
173
173
  * Current detected culture
174
174
  */
175
175
  DomUtils.detectedCulture = (() => {
176
+ var _a, _b;
176
177
  // URL first, then local storage
177
178
  let culture;
178
179
  try {
179
180
  culture =
180
- new URL(location.href).searchParams.get(DomUtils.Culture) ||
181
- localStorage.getItem(DomUtils.Culture);
181
+ (_b = (_a = new URL(location.href).searchParams.get(DomUtils.CultureField)) !== null && _a !== void 0 ? _a : sessionStorage.getItem(DomUtils.CultureField)) !== null && _b !== void 0 ? _b : localStorage.getItem(DomUtils.CultureField);
182
182
  }
183
183
  catch {
184
184
  culture = null;
@@ -342,22 +342,6 @@ var DomUtils;
342
342
  return base;
343
343
  }
344
344
  DomUtils.mergeURLSearchParams = mergeURLSearchParams;
345
- /**
346
- * Save country name
347
- * @param country Country name
348
- */
349
- function saveCountry(country) {
350
- localStorage.setItem(DomUtils.Country, country);
351
- }
352
- DomUtils.saveCountry = saveCountry;
353
- /**
354
- * Save culture name
355
- * @param culture Culture name
356
- */
357
- function saveCulture(culture) {
358
- localStorage.setItem(DomUtils.Culture, culture);
359
- }
360
- DomUtils.saveCulture = saveCulture;
361
345
  /**
362
346
  * Set HTML element focus by name
363
347
  * @param name Element name or first collection item
@@ -37,7 +37,7 @@ export declare namespace StorageUtils {
37
37
  */
38
38
  function getSessionData<T>(key: string): T | undefined;
39
39
  /**
40
- * Get session storage data
40
+ * Get session storage data with default value
41
41
  * @param key Key name
42
42
  * @param defaultValue Default value
43
43
  */
@@ -69,6 +69,7 @@ var StorageUtils;
69
69
  /**
70
70
  * Get session storage data
71
71
  * @param key Key name
72
+ * @param defaultValue Default value
72
73
  */
73
74
  function getSessionData(key, defaultValue) {
74
75
  // Get storage
@@ -1,4 +1,6 @@
1
1
  export * from './types/FormData';
2
+ export * from './storage/IStorage';
3
+ export * from './storage/WindowStorage';
2
4
  export * from './DataTypes';
3
5
  export * from './DateUtils';
4
6
  export * from './DomUtils';
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,34 @@
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
+ * @param persistance Persist the data, false will stop persistance
26
+ */
27
+ setData(key: string, data: unknown, persistance?: boolean): void;
28
+ }
29
+ /**
30
+ * Storage constructor interface
31
+ */
32
+ export interface IStorageConstructor {
33
+ new (globalFields: string[], callback: (field: string, data: string | null) => string | null): IStorage;
34
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,37 @@
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
+ * @param callback Field and data callback
12
+ */
13
+ constructor(globalFields: string[], callback: (field: string, data: string | null) => string | null);
14
+ /**
15
+ * Get data
16
+ * @param key Key name
17
+ */
18
+ getData<T>(key: string): T | undefined;
19
+ /**
20
+ * Get data with default value
21
+ * @param key Key name
22
+ * @param defaultValue Default value
23
+ */
24
+ getData<T>(key: string, defaultValue: T): T;
25
+ /**
26
+ * Get object data
27
+ * @param key Key name
28
+ */
29
+ getObject<T extends {}>(key: string): T | undefined;
30
+ /**
31
+ * Set data
32
+ * @param key Key name
33
+ * @param data Data, null for removal
34
+ * @param persistance Persist the data, false will stop persistance
35
+ */
36
+ setData(key: string, data: unknown, persistance?: boolean): void;
37
+ }
@@ -0,0 +1,72 @@
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
+ * @param callback Field and data callback
15
+ */
16
+ constructor(globalFields, callback) {
17
+ this.globalFields = globalFields;
18
+ if (globalFields.length == 0)
19
+ return;
20
+ // Copy global fields to session storage where first item does not exist
21
+ // Duplicate browser tab would copy the session storage
22
+ const firsItem = sessionStorage.getItem(globalFields[0]);
23
+ if (firsItem)
24
+ return;
25
+ globalFields.forEach((field) => {
26
+ const data = callback(field, localStorage.getItem(field));
27
+ if (data == null)
28
+ sessionStorage.removeItem(field);
29
+ else
30
+ sessionStorage.setItem(field, data);
31
+ });
32
+ }
33
+ /**
34
+ * Get data
35
+ * @param key Key name
36
+ * @param defaultValue Default value
37
+ */
38
+ getData(key, defaultValue) {
39
+ // Get storage
40
+ const data = sessionStorage.getItem(key);
41
+ // No default value
42
+ if (defaultValue == null)
43
+ return Utils_1.Utils.parseString(data);
44
+ // Return
45
+ return Utils_1.Utils.parseString(data, defaultValue);
46
+ }
47
+ /**
48
+ * Get object data
49
+ * @param key Key name
50
+ */
51
+ getObject(key) {
52
+ // Get storage
53
+ const data = sessionStorage.getItem(key);
54
+ if (data == null)
55
+ return undefined;
56
+ return JSON.parse(data);
57
+ }
58
+ /**
59
+ * Set data
60
+ * @param key Key name
61
+ * @param data Data, null for removal
62
+ * @param persistance Persist the data, false will stop persistance
63
+ */
64
+ setData(key, data, persistance) {
65
+ StorageUtils_1.StorageUtils.setSessionData(key, data);
66
+ if (persistance ||
67
+ (persistance == null && this.globalFields.includes(key))) {
68
+ StorageUtils_1.StorageUtils.setLocalData(key, data);
69
+ }
70
+ }
71
+ }
72
+ exports.WindowStorage = WindowStorage;
@@ -8,11 +8,11 @@ export declare namespace DomUtils {
8
8
  /**
9
9
  * Language cache parameter name
10
10
  */
11
- const Culture = "culture";
11
+ const CultureField = "culture";
12
12
  /**
13
13
  * Country cache parameter name
14
14
  */
15
- const Country = "country";
15
+ const CountryField = "country";
16
16
  /**
17
17
  * Clear form data
18
18
  * @param data Form data
@@ -94,16 +94,6 @@ export declare namespace DomUtils {
94
94
  * @param data New simple object data to merge
95
95
  */
96
96
  function mergeURLSearchParams(base: URLSearchParams, data: DataTypes.SimpleObject): URLSearchParams;
97
- /**
98
- * Save country name
99
- * @param country Country name
100
- */
101
- function saveCountry(country: string): void;
102
- /**
103
- * Save culture name
104
- * @param culture Culture name
105
- */
106
- function saveCulture(culture: string): void;
107
97
  /**
108
98
  * Set HTML element focus by name
109
99
  * @param name Element name or first collection item
@@ -13,11 +13,11 @@ export var DomUtils;
13
13
  /**
14
14
  * Language cache parameter name
15
15
  */
16
- DomUtils.Culture = 'culture';
16
+ DomUtils.CultureField = 'culture';
17
17
  /**
18
18
  * Country cache parameter name
19
19
  */
20
- DomUtils.Country = 'country';
20
+ DomUtils.CountryField = 'country';
21
21
  /**
22
22
  * Clear form data
23
23
  * @param data Form data
@@ -153,12 +153,12 @@ export var DomUtils;
153
153
  * Current detected country
154
154
  */
155
155
  DomUtils.detectedCountry = (() => {
156
+ var _a, _b;
156
157
  // URL first, then local storage
157
158
  let country;
158
159
  try {
159
160
  country =
160
- new URL(location.href).searchParams.get(DomUtils.Country) ||
161
- localStorage.getItem(DomUtils.Country);
161
+ (_b = (_a = new URL(location.href).searchParams.get(DomUtils.CountryField)) !== null && _a !== void 0 ? _a : sessionStorage.getItem(DomUtils.CountryField)) !== null && _b !== void 0 ? _b : localStorage.getItem(DomUtils.CountryField);
162
162
  }
163
163
  catch {
164
164
  country = null;
@@ -170,12 +170,12 @@ export var DomUtils;
170
170
  * Current detected culture
171
171
  */
172
172
  DomUtils.detectedCulture = (() => {
173
+ var _a, _b;
173
174
  // URL first, then local storage
174
175
  let culture;
175
176
  try {
176
177
  culture =
177
- new URL(location.href).searchParams.get(DomUtils.Culture) ||
178
- localStorage.getItem(DomUtils.Culture);
178
+ (_b = (_a = new URL(location.href).searchParams.get(DomUtils.CultureField)) !== null && _a !== void 0 ? _a : sessionStorage.getItem(DomUtils.CultureField)) !== null && _b !== void 0 ? _b : localStorage.getItem(DomUtils.CultureField);
179
179
  }
180
180
  catch {
181
181
  culture = null;
@@ -339,22 +339,6 @@ export var DomUtils;
339
339
  return base;
340
340
  }
341
341
  DomUtils.mergeURLSearchParams = mergeURLSearchParams;
342
- /**
343
- * Save country name
344
- * @param country Country name
345
- */
346
- function saveCountry(country) {
347
- localStorage.setItem(DomUtils.Country, country);
348
- }
349
- DomUtils.saveCountry = saveCountry;
350
- /**
351
- * Save culture name
352
- * @param culture Culture name
353
- */
354
- function saveCulture(culture) {
355
- localStorage.setItem(DomUtils.Culture, culture);
356
- }
357
- DomUtils.saveCulture = saveCulture;
358
342
  /**
359
343
  * Set HTML element focus by name
360
344
  * @param name Element name or first collection item
@@ -37,7 +37,7 @@ export declare namespace StorageUtils {
37
37
  */
38
38
  function getSessionData<T>(key: string): T | undefined;
39
39
  /**
40
- * Get session storage data
40
+ * Get session storage data with default value
41
41
  * @param key Key name
42
42
  * @param defaultValue Default value
43
43
  */
@@ -66,6 +66,7 @@ export var StorageUtils;
66
66
  /**
67
67
  * Get session storage data
68
68
  * @param key Key name
69
+ * @param defaultValue Default value
69
70
  */
70
71
  function getSessionData(key, defaultValue) {
71
72
  // Get storage
@@ -1,4 +1,6 @@
1
1
  export * from './types/FormData';
2
+ export * from './storage/IStorage';
3
+ export * from './storage/WindowStorage';
2
4
  export * from './DataTypes';
3
5
  export * from './DateUtils';
4
6
  export * from './DomUtils';
package/lib/mjs/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from './types/FormData';
2
+ export * from './storage/IStorage';
3
+ export * from './storage/WindowStorage';
2
4
  export * from './DataTypes';
3
5
  export * from './DateUtils';
4
6
  export * from './DomUtils';
@@ -0,0 +1,34 @@
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
+ * @param persistance Persist the data, false will stop persistance
26
+ */
27
+ setData(key: string, data: unknown, persistance?: boolean): void;
28
+ }
29
+ /**
30
+ * Storage constructor interface
31
+ */
32
+ export interface IStorageConstructor {
33
+ new (globalFields: string[], callback: (field: string, data: string | null) => string | null): IStorage;
34
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,37 @@
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
+ * @param callback Field and data callback
12
+ */
13
+ constructor(globalFields: string[], callback: (field: string, data: string | null) => string | null);
14
+ /**
15
+ * Get data
16
+ * @param key Key name
17
+ */
18
+ getData<T>(key: string): T | undefined;
19
+ /**
20
+ * Get data with default value
21
+ * @param key Key name
22
+ * @param defaultValue Default value
23
+ */
24
+ getData<T>(key: string, defaultValue: T): T;
25
+ /**
26
+ * Get object data
27
+ * @param key Key name
28
+ */
29
+ getObject<T extends {}>(key: string): T | undefined;
30
+ /**
31
+ * Set data
32
+ * @param key Key name
33
+ * @param data Data, null for removal
34
+ * @param persistance Persist the data, false will stop persistance
35
+ */
36
+ setData(key: string, data: unknown, persistance?: boolean): void;
37
+ }
@@ -0,0 +1,68 @@
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
+ * @param callback Field and data callback
12
+ */
13
+ constructor(globalFields, callback) {
14
+ this.globalFields = globalFields;
15
+ if (globalFields.length == 0)
16
+ return;
17
+ // Copy global fields to session storage where first item does not exist
18
+ // Duplicate browser tab would copy the session storage
19
+ const firsItem = sessionStorage.getItem(globalFields[0]);
20
+ if (firsItem)
21
+ return;
22
+ globalFields.forEach((field) => {
23
+ const data = callback(field, localStorage.getItem(field));
24
+ if (data == null)
25
+ sessionStorage.removeItem(field);
26
+ else
27
+ sessionStorage.setItem(field, data);
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.parseString(data);
41
+ // Return
42
+ return 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
+ * @param persistance Persist the data, false will stop persistance
60
+ */
61
+ setData(key, data, persistance) {
62
+ StorageUtils.setSessionData(key, data);
63
+ if (persistance ||
64
+ (persistance == null && this.globalFields.includes(key))) {
65
+ StorageUtils.setLocalData(key, data);
66
+ }
67
+ }
68
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.82",
3
+ "version": "1.0.86",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
package/src/DomUtils.ts CHANGED
@@ -15,12 +15,12 @@ export namespace DomUtils {
15
15
  /**
16
16
  * Language cache parameter name
17
17
  */
18
- export const Culture = 'culture';
18
+ export const CultureField = 'culture';
19
19
 
20
20
  /**
21
21
  * Country cache parameter name
22
22
  */
23
- export const Country = 'country';
23
+ export const CountryField = 'country';
24
24
 
25
25
  /**
26
26
  * Clear form data
@@ -198,8 +198,9 @@ export namespace DomUtils {
198
198
  let country: string | null;
199
199
  try {
200
200
  country =
201
- new URL(location.href).searchParams.get(Country) ||
202
- localStorage.getItem(Country);
201
+ new URL(location.href).searchParams.get(CountryField) ??
202
+ sessionStorage.getItem(CountryField) ??
203
+ localStorage.getItem(CountryField);
203
204
  } catch {
204
205
  country = null;
205
206
  }
@@ -216,8 +217,9 @@ export namespace DomUtils {
216
217
  let culture: string | null;
217
218
  try {
218
219
  culture =
219
- new URL(location.href).searchParams.get(Culture) ||
220
- localStorage.getItem(Culture);
220
+ new URL(location.href).searchParams.get(CultureField) ??
221
+ sessionStorage.getItem(CultureField) ??
222
+ localStorage.getItem(CultureField);
221
223
  } catch {
222
224
  culture = null;
223
225
  }
@@ -415,22 +417,6 @@ export namespace DomUtils {
415
417
  return base;
416
418
  }
417
419
 
418
- /**
419
- * Save country name
420
- * @param country Country name
421
- */
422
- export function saveCountry(country: string) {
423
- localStorage.setItem(Country, country);
424
- }
425
-
426
- /**
427
- * Save culture name
428
- * @param culture Culture name
429
- */
430
- export function saveCulture(culture: string) {
431
- localStorage.setItem(Culture, culture);
432
- }
433
-
434
420
  /**
435
421
  * Set HTML element focus by name
436
422
  * @param name Element name or first collection item
@@ -94,7 +94,7 @@ export namespace StorageUtils {
94
94
  export function getSessionData<T>(key: string): T | undefined;
95
95
 
96
96
  /**
97
- * Get session storage data
97
+ * Get session storage data with default value
98
98
  * @param key Key name
99
99
  * @param defaultValue Default value
100
100
  */
@@ -103,6 +103,7 @@ export namespace StorageUtils {
103
103
  /**
104
104
  * Get session storage data
105
105
  * @param key Key name
106
+ * @param defaultValue Default value
106
107
  */
107
108
  export function getSessionData<T>(
108
109
  key: string,
package/src/index.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  export * from './types/FormData';
2
+
3
+ export * from './storage/IStorage';
4
+ export * from './storage/WindowStorage';
5
+
2
6
  export * from './DataTypes';
3
7
  export * from './DateUtils';
4
8
  export * from './DomUtils';
@@ -0,0 +1,41 @@
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
+ * @param persistance Persist the data, false will stop persistance
29
+ */
30
+ setData(key: string, data: unknown, persistance?: boolean): void;
31
+ }
32
+
33
+ /**
34
+ * Storage constructor interface
35
+ */
36
+ export interface IStorageConstructor {
37
+ new (
38
+ globalFields: string[],
39
+ callback: (field: string, data: string | null) => string | null
40
+ ): IStorage;
41
+ }
@@ -0,0 +1,88 @@
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
+ * @param callback Field and data callback
14
+ */
15
+ constructor(
16
+ private globalFields: string[],
17
+ callback: (field: string, data: string | null) => string | null
18
+ ) {
19
+ if (globalFields.length == 0) return;
20
+
21
+ // Copy global fields to session storage where first item does not exist
22
+ // Duplicate browser tab would copy the session storage
23
+ const firsItem = sessionStorage.getItem(globalFields[0]);
24
+ if (firsItem) return;
25
+
26
+ globalFields.forEach((field) => {
27
+ const data = callback(field, localStorage.getItem(field));
28
+ if (data == null) sessionStorage.removeItem(field);
29
+ else sessionStorage.setItem(field, data);
30
+ });
31
+ }
32
+
33
+ /**
34
+ * Get data
35
+ * @param key Key name
36
+ */
37
+ getData<T>(key: string): T | undefined;
38
+
39
+ /**
40
+ * Get data with default value
41
+ * @param key Key name
42
+ * @param defaultValue Default value
43
+ */
44
+ getData<T>(key: string, defaultValue: T): T;
45
+
46
+ /**
47
+ * Get data
48
+ * @param key Key name
49
+ * @param defaultValue Default value
50
+ */
51
+ getData<T>(key: string, defaultValue?: T): T | undefined {
52
+ // Get storage
53
+ const data = sessionStorage.getItem(key);
54
+
55
+ // No default value
56
+ if (defaultValue == null) return Utils.parseString<T>(data);
57
+
58
+ // Return
59
+ return Utils.parseString<T>(data, defaultValue);
60
+ }
61
+
62
+ /**
63
+ * Get object data
64
+ * @param key Key name
65
+ */
66
+ getObject<T extends {}>(key: string) {
67
+ // Get storage
68
+ const data = sessionStorage.getItem(key);
69
+ if (data == null) return undefined;
70
+ return <T>JSON.parse(data);
71
+ }
72
+
73
+ /**
74
+ * Set data
75
+ * @param key Key name
76
+ * @param data Data, null for removal
77
+ * @param persistance Persist the data, false will stop persistance
78
+ */
79
+ setData(key: string, data: unknown, persistance?: boolean) {
80
+ StorageUtils.setSessionData(key, data);
81
+ if (
82
+ persistance ||
83
+ (persistance == null && this.globalFields.includes(key))
84
+ ) {
85
+ StorageUtils.setLocalData(key, data);
86
+ }
87
+ }
88
+ }