@etsoo/shared 1.0.83 → 1.0.87

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
@@ -95,8 +95,6 @@ DOM/window related utilities
95
95
  |isJSONContentType|Is JSON content type|
96
96
  |mergeFormData|Merge form data to primary one|
97
97
  |mergeURLSearchParams|Merge URL search parameters|
98
- |saveCountry|Save country name|
99
- |saveCulture|Save culture name|
100
98
  |setFocus|Set HTML element focus by name|
101
99
 
102
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
@@ -5,29 +5,33 @@ export interface IStorage {
5
5
  /**
6
6
  * Get data
7
7
  * @param key Key name
8
+ * @param persistance From the persisted data
8
9
  */
9
- getData<T>(key: string): T | undefined;
10
+ getData<T>(key: string, persistance?: boolean): T | undefined;
10
11
  /**
11
12
  * Get data with default value
12
13
  * @param key Key name
13
14
  * @param defaultValue Default value
15
+ * @param persistance From the persisted data
14
16
  */
15
- getData<T>(key: string, defaultValue: T): T;
17
+ getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
16
18
  /**
17
- * Get session storage object data
19
+ * Get object data
18
20
  * @param key Key name
21
+ * @param persistance From the persisted data
19
22
  */
20
- getObject<T extends {}>(key: string): T | undefined;
23
+ getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
21
24
  /**
22
25
  * Set data
23
26
  * @param key Key name
24
27
  * @param data Data, null for removal
28
+ * @param persistance Persist the data, false will stop persistance
25
29
  */
26
- setData(key: string, data: unknown): void;
30
+ setData(key: string, data: unknown, persistance?: boolean): void;
27
31
  }
28
32
  /**
29
33
  * Storage constructor interface
30
34
  */
31
35
  export interface IStorageConstructor {
32
- new (globalFields: string[]): IStorage;
36
+ new (globalFields: string[], callback: (field: string, data: string | null) => string | null): IStorage;
33
37
  }
@@ -8,28 +8,33 @@ export declare class WindowStorage implements IStorage {
8
8
  /**
9
9
  * Constructor
10
10
  * @param globalFields Global fields
11
+ * @param callback Field and data callback
11
12
  */
12
- constructor(globalFields: string[]);
13
+ constructor(globalFields: string[], callback: (field: string, data: string | null) => string | null);
13
14
  /**
14
15
  * Get data
15
16
  * @param key Key name
17
+ * @param persistance From the persisted data
16
18
  */
17
- getData<T>(key: string): T | undefined;
19
+ getData<T>(key: string, persistance?: boolean): T | undefined;
18
20
  /**
19
21
  * Get data with default value
20
22
  * @param key Key name
21
23
  * @param defaultValue Default value
24
+ * @param persistance From the persisted data
22
25
  */
23
- getData<T>(key: string, defaultValue: T): T;
26
+ getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
24
27
  /**
25
28
  * Get object data
26
29
  * @param key Key name
30
+ * @param persistance From the persisted data
27
31
  */
28
- getObject<T extends {}>(key: string): T | undefined;
32
+ getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
29
33
  /**
30
34
  * Set data
31
35
  * @param key Key name
32
36
  * @param data Data, null for removal
37
+ * @param persistance To the persisted data, false will stop persistance
33
38
  */
34
- setData(key: string, data: unknown): void;
39
+ setData(key: string, data: unknown, persistance?: boolean): void;
35
40
  }
@@ -11,30 +11,36 @@ class WindowStorage {
11
11
  /**
12
12
  * Constructor
13
13
  * @param globalFields Global fields
14
+ * @param callback Field and data callback
14
15
  */
15
- constructor(globalFields) {
16
+ constructor(globalFields, callback) {
16
17
  this.globalFields = globalFields;
17
18
  if (globalFields.length == 0)
18
19
  return;
19
20
  // Copy global fields to session storage where first item does not exist
21
+ // Duplicate browser tab would copy the session storage
20
22
  const firsItem = sessionStorage.getItem(globalFields[0]);
21
23
  if (firsItem)
22
24
  return;
23
25
  globalFields.forEach((field) => {
24
- const data = localStorage.getItem(field);
25
- if (data != null) {
26
+ const data = callback(field, localStorage.getItem(field));
27
+ if (data == null)
28
+ sessionStorage.removeItem(field);
29
+ else
26
30
  sessionStorage.setItem(field, data);
27
- }
28
31
  });
29
32
  }
30
33
  /**
31
34
  * Get data
32
35
  * @param key Key name
33
36
  * @param defaultValue Default value
37
+ * @param persistance From the persisted data
34
38
  */
35
- getData(key, defaultValue) {
39
+ getData(key, defaultValue, persistance) {
36
40
  // Get storage
37
- const data = sessionStorage.getItem(key);
41
+ const data = persistance
42
+ ? localStorage.getItem(key)
43
+ : sessionStorage.getItem(key);
38
44
  // No default value
39
45
  if (defaultValue == null)
40
46
  return Utils_1.Utils.parseString(data);
@@ -44,10 +50,13 @@ class WindowStorage {
44
50
  /**
45
51
  * Get object data
46
52
  * @param key Key name
53
+ * @param persistance From the persisted data
47
54
  */
48
- getObject(key) {
55
+ getObject(key, persistance) {
49
56
  // Get storage
50
- const data = sessionStorage.getItem(key);
57
+ const data = persistance
58
+ ? localStorage.getItem(key)
59
+ : sessionStorage.getItem(key);
51
60
  if (data == null)
52
61
  return undefined;
53
62
  return JSON.parse(data);
@@ -56,10 +65,15 @@ class WindowStorage {
56
65
  * Set data
57
66
  * @param key Key name
58
67
  * @param data Data, null for removal
68
+ * @param persistance To the persisted data, false will stop persistance
59
69
  */
60
- setData(key, data) {
70
+ setData(key, data, persistance) {
71
+ if (persistance) {
72
+ StorageUtils_1.StorageUtils.setLocalData(key, data);
73
+ return;
74
+ }
61
75
  StorageUtils_1.StorageUtils.setSessionData(key, data);
62
- if (this.globalFields.includes(key)) {
76
+ if (persistance !== false && this.globalFields.includes(key)) {
63
77
  StorageUtils_1.StorageUtils.setLocalData(key, data);
64
78
  }
65
79
  }
@@ -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
@@ -5,29 +5,33 @@ export interface IStorage {
5
5
  /**
6
6
  * Get data
7
7
  * @param key Key name
8
+ * @param persistance From the persisted data
8
9
  */
9
- getData<T>(key: string): T | undefined;
10
+ getData<T>(key: string, persistance?: boolean): T | undefined;
10
11
  /**
11
12
  * Get data with default value
12
13
  * @param key Key name
13
14
  * @param defaultValue Default value
15
+ * @param persistance From the persisted data
14
16
  */
15
- getData<T>(key: string, defaultValue: T): T;
17
+ getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
16
18
  /**
17
- * Get session storage object data
19
+ * Get object data
18
20
  * @param key Key name
21
+ * @param persistance From the persisted data
19
22
  */
20
- getObject<T extends {}>(key: string): T | undefined;
23
+ getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
21
24
  /**
22
25
  * Set data
23
26
  * @param key Key name
24
27
  * @param data Data, null for removal
28
+ * @param persistance Persist the data, false will stop persistance
25
29
  */
26
- setData(key: string, data: unknown): void;
30
+ setData(key: string, data: unknown, persistance?: boolean): void;
27
31
  }
28
32
  /**
29
33
  * Storage constructor interface
30
34
  */
31
35
  export interface IStorageConstructor {
32
- new (globalFields: string[]): IStorage;
36
+ new (globalFields: string[], callback: (field: string, data: string | null) => string | null): IStorage;
33
37
  }
@@ -8,28 +8,33 @@ export declare class WindowStorage implements IStorage {
8
8
  /**
9
9
  * Constructor
10
10
  * @param globalFields Global fields
11
+ * @param callback Field and data callback
11
12
  */
12
- constructor(globalFields: string[]);
13
+ constructor(globalFields: string[], callback: (field: string, data: string | null) => string | null);
13
14
  /**
14
15
  * Get data
15
16
  * @param key Key name
17
+ * @param persistance From the persisted data
16
18
  */
17
- getData<T>(key: string): T | undefined;
19
+ getData<T>(key: string, persistance?: boolean): T | undefined;
18
20
  /**
19
21
  * Get data with default value
20
22
  * @param key Key name
21
23
  * @param defaultValue Default value
24
+ * @param persistance From the persisted data
22
25
  */
23
- getData<T>(key: string, defaultValue: T): T;
26
+ getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
24
27
  /**
25
28
  * Get object data
26
29
  * @param key Key name
30
+ * @param persistance From the persisted data
27
31
  */
28
- getObject<T extends {}>(key: string): T | undefined;
32
+ getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
29
33
  /**
30
34
  * Set data
31
35
  * @param key Key name
32
36
  * @param data Data, null for removal
37
+ * @param persistance To the persisted data, false will stop persistance
33
38
  */
34
- setData(key: string, data: unknown): void;
39
+ setData(key: string, data: unknown, persistance?: boolean): void;
35
40
  }
@@ -8,30 +8,36 @@ export class WindowStorage {
8
8
  /**
9
9
  * Constructor
10
10
  * @param globalFields Global fields
11
+ * @param callback Field and data callback
11
12
  */
12
- constructor(globalFields) {
13
+ constructor(globalFields, callback) {
13
14
  this.globalFields = globalFields;
14
15
  if (globalFields.length == 0)
15
16
  return;
16
17
  // Copy global fields to session storage where first item does not exist
18
+ // Duplicate browser tab would copy the session storage
17
19
  const firsItem = sessionStorage.getItem(globalFields[0]);
18
20
  if (firsItem)
19
21
  return;
20
22
  globalFields.forEach((field) => {
21
- const data = localStorage.getItem(field);
22
- if (data != null) {
23
+ const data = callback(field, localStorage.getItem(field));
24
+ if (data == null)
25
+ sessionStorage.removeItem(field);
26
+ else
23
27
  sessionStorage.setItem(field, data);
24
- }
25
28
  });
26
29
  }
27
30
  /**
28
31
  * Get data
29
32
  * @param key Key name
30
33
  * @param defaultValue Default value
34
+ * @param persistance From the persisted data
31
35
  */
32
- getData(key, defaultValue) {
36
+ getData(key, defaultValue, persistance) {
33
37
  // Get storage
34
- const data = sessionStorage.getItem(key);
38
+ const data = persistance
39
+ ? localStorage.getItem(key)
40
+ : sessionStorage.getItem(key);
35
41
  // No default value
36
42
  if (defaultValue == null)
37
43
  return Utils.parseString(data);
@@ -41,10 +47,13 @@ export class WindowStorage {
41
47
  /**
42
48
  * Get object data
43
49
  * @param key Key name
50
+ * @param persistance From the persisted data
44
51
  */
45
- getObject(key) {
52
+ getObject(key, persistance) {
46
53
  // Get storage
47
- const data = sessionStorage.getItem(key);
54
+ const data = persistance
55
+ ? localStorage.getItem(key)
56
+ : sessionStorage.getItem(key);
48
57
  if (data == null)
49
58
  return undefined;
50
59
  return JSON.parse(data);
@@ -53,10 +62,15 @@ export class WindowStorage {
53
62
  * Set data
54
63
  * @param key Key name
55
64
  * @param data Data, null for removal
65
+ * @param persistance To the persisted data, false will stop persistance
56
66
  */
57
- setData(key, data) {
67
+ setData(key, data, persistance) {
68
+ if (persistance) {
69
+ StorageUtils.setLocalData(key, data);
70
+ return;
71
+ }
58
72
  StorageUtils.setSessionData(key, data);
59
- if (this.globalFields.includes(key)) {
73
+ if (persistance !== false && this.globalFields.includes(key)) {
60
74
  StorageUtils.setLocalData(key, data);
61
75
  }
62
76
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.83",
3
+ "version": "1.0.87",
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
@@ -5,33 +5,40 @@ export interface IStorage {
5
5
  /**
6
6
  * Get data
7
7
  * @param key Key name
8
+ * @param persistance From the persisted data
8
9
  */
9
- getData<T>(key: string): T | undefined;
10
+ getData<T>(key: string, persistance?: boolean): T | undefined;
10
11
 
11
12
  /**
12
13
  * Get data with default value
13
14
  * @param key Key name
14
15
  * @param defaultValue Default value
16
+ * @param persistance From the persisted data
15
17
  */
16
- getData<T>(key: string, defaultValue: T): T;
18
+ getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
17
19
 
18
20
  /**
19
- * Get session storage object data
21
+ * Get object data
20
22
  * @param key Key name
23
+ * @param persistance From the persisted data
21
24
  */
22
- getObject<T extends {}>(key: string): T | undefined;
25
+ getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
23
26
 
24
27
  /**
25
28
  * Set data
26
29
  * @param key Key name
27
30
  * @param data Data, null for removal
31
+ * @param persistance Persist the data, false will stop persistance
28
32
  */
29
- setData(key: string, data: unknown): void;
33
+ setData(key: string, data: unknown, persistance?: boolean): void;
30
34
  }
31
35
 
32
36
  /**
33
37
  * Storage constructor interface
34
38
  */
35
39
  export interface IStorageConstructor {
36
- new (globalFields: string[]): IStorage;
40
+ new (
41
+ globalFields: string[],
42
+ callback: (field: string, data: string | null) => string | null
43
+ ): IStorage;
37
44
  }
@@ -10,43 +10,56 @@ export class WindowStorage implements IStorage {
10
10
  /**
11
11
  * Constructor
12
12
  * @param globalFields Global fields
13
+ * @param callback Field and data callback
13
14
  */
14
- constructor(private globalFields: string[]) {
15
+ constructor(
16
+ private globalFields: string[],
17
+ callback: (field: string, data: string | null) => string | null
18
+ ) {
15
19
  if (globalFields.length == 0) return;
16
20
 
17
21
  // Copy global fields to session storage where first item does not exist
22
+ // Duplicate browser tab would copy the session storage
18
23
  const firsItem = sessionStorage.getItem(globalFields[0]);
19
24
  if (firsItem) return;
20
25
 
21
26
  globalFields.forEach((field) => {
22
- const data = localStorage.getItem(field);
23
- if (data != null) {
24
- sessionStorage.setItem(field, data);
25
- }
27
+ const data = callback(field, localStorage.getItem(field));
28
+ if (data == null) sessionStorage.removeItem(field);
29
+ else sessionStorage.setItem(field, data);
26
30
  });
27
31
  }
28
32
 
29
33
  /**
30
34
  * Get data
31
35
  * @param key Key name
36
+ * @param persistance From the persisted data
32
37
  */
33
- getData<T>(key: string): T | undefined;
38
+ getData<T>(key: string, persistance?: boolean): T | undefined;
34
39
 
35
40
  /**
36
41
  * Get data with default value
37
42
  * @param key Key name
38
43
  * @param defaultValue Default value
44
+ * @param persistance From the persisted data
39
45
  */
40
- getData<T>(key: string, defaultValue: T): T;
46
+ getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
41
47
 
42
48
  /**
43
49
  * Get data
44
50
  * @param key Key name
45
51
  * @param defaultValue Default value
52
+ * @param persistance From the persisted data
46
53
  */
47
- getData<T>(key: string, defaultValue?: T): T | undefined {
54
+ getData<T>(
55
+ key: string,
56
+ defaultValue?: T,
57
+ persistance?: boolean
58
+ ): T | undefined {
48
59
  // Get storage
49
- const data = sessionStorage.getItem(key);
60
+ const data = persistance
61
+ ? localStorage.getItem(key)
62
+ : sessionStorage.getItem(key);
50
63
 
51
64
  // No default value
52
65
  if (defaultValue == null) return Utils.parseString<T>(data);
@@ -58,11 +71,16 @@ export class WindowStorage implements IStorage {
58
71
  /**
59
72
  * Get object data
60
73
  * @param key Key name
74
+ * @param persistance From the persisted data
61
75
  */
62
- getObject<T extends {}>(key: string) {
76
+ getObject<T extends {}>(key: string, persistance?: boolean) {
63
77
  // Get storage
64
- const data = sessionStorage.getItem(key);
78
+ const data = persistance
79
+ ? localStorage.getItem(key)
80
+ : sessionStorage.getItem(key);
81
+
65
82
  if (data == null) return undefined;
83
+
66
84
  return <T>JSON.parse(data);
67
85
  }
68
86
 
@@ -70,10 +88,16 @@ export class WindowStorage implements IStorage {
70
88
  * Set data
71
89
  * @param key Key name
72
90
  * @param data Data, null for removal
91
+ * @param persistance To the persisted data, false will stop persistance
73
92
  */
74
- setData(key: string, data: unknown) {
93
+ setData(key: string, data: unknown, persistance?: boolean) {
94
+ if (persistance) {
95
+ StorageUtils.setLocalData(key, data);
96
+ return;
97
+ }
98
+
75
99
  StorageUtils.setSessionData(key, data);
76
- if (this.globalFields.includes(key)) {
100
+ if (persistance !== false && this.globalFields.includes(key)) {
77
101
  StorageUtils.setLocalData(key, data);
78
102
  }
79
103
  }