@etsoo/shared 1.0.82 → 1.0.83

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
 
@@ -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,33 @@
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
+ */
26
+ setData(key: string, data: unknown): void;
27
+ }
28
+ /**
29
+ * Storage constructor interface
30
+ */
31
+ export interface IStorageConstructor {
32
+ new (globalFields: string[]): IStorage;
33
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,35 @@
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
+ */
12
+ constructor(globalFields: string[]);
13
+ /**
14
+ * Get data
15
+ * @param key Key name
16
+ */
17
+ getData<T>(key: string): T | undefined;
18
+ /**
19
+ * Get data with default value
20
+ * @param key Key name
21
+ * @param defaultValue Default value
22
+ */
23
+ getData<T>(key: string, defaultValue: T): T;
24
+ /**
25
+ * Get object data
26
+ * @param key Key name
27
+ */
28
+ getObject<T extends {}>(key: string): T | undefined;
29
+ /**
30
+ * Set data
31
+ * @param key Key name
32
+ * @param data Data, null for removal
33
+ */
34
+ setData(key: string, data: unknown): void;
35
+ }
@@ -0,0 +1,67 @@
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
+ */
15
+ constructor(globalFields) {
16
+ this.globalFields = globalFields;
17
+ if (globalFields.length == 0)
18
+ return;
19
+ // Copy global fields to session storage where first item does not exist
20
+ const firsItem = sessionStorage.getItem(globalFields[0]);
21
+ if (firsItem)
22
+ return;
23
+ globalFields.forEach((field) => {
24
+ const data = localStorage.getItem(field);
25
+ if (data != null) {
26
+ sessionStorage.setItem(field, data);
27
+ }
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_1.Utils.parseString(data);
41
+ // Return
42
+ return Utils_1.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
+ */
60
+ setData(key, data) {
61
+ StorageUtils_1.StorageUtils.setSessionData(key, data);
62
+ if (this.globalFields.includes(key)) {
63
+ StorageUtils_1.StorageUtils.setLocalData(key, data);
64
+ }
65
+ }
66
+ }
67
+ exports.WindowStorage = WindowStorage;
@@ -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,33 @@
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
+ */
26
+ setData(key: string, data: unknown): void;
27
+ }
28
+ /**
29
+ * Storage constructor interface
30
+ */
31
+ export interface IStorageConstructor {
32
+ new (globalFields: string[]): IStorage;
33
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,35 @@
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
+ */
12
+ constructor(globalFields: string[]);
13
+ /**
14
+ * Get data
15
+ * @param key Key name
16
+ */
17
+ getData<T>(key: string): T | undefined;
18
+ /**
19
+ * Get data with default value
20
+ * @param key Key name
21
+ * @param defaultValue Default value
22
+ */
23
+ getData<T>(key: string, defaultValue: T): T;
24
+ /**
25
+ * Get object data
26
+ * @param key Key name
27
+ */
28
+ getObject<T extends {}>(key: string): T | undefined;
29
+ /**
30
+ * Set data
31
+ * @param key Key name
32
+ * @param data Data, null for removal
33
+ */
34
+ setData(key: string, data: unknown): void;
35
+ }
@@ -0,0 +1,63 @@
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
+ */
12
+ constructor(globalFields) {
13
+ this.globalFields = globalFields;
14
+ if (globalFields.length == 0)
15
+ return;
16
+ // Copy global fields to session storage where first item does not exist
17
+ const firsItem = sessionStorage.getItem(globalFields[0]);
18
+ if (firsItem)
19
+ return;
20
+ globalFields.forEach((field) => {
21
+ const data = localStorage.getItem(field);
22
+ if (data != null) {
23
+ sessionStorage.setItem(field, data);
24
+ }
25
+ });
26
+ }
27
+ /**
28
+ * Get data
29
+ * @param key Key name
30
+ * @param defaultValue Default value
31
+ */
32
+ getData(key, defaultValue) {
33
+ // Get storage
34
+ const data = sessionStorage.getItem(key);
35
+ // No default value
36
+ if (defaultValue == null)
37
+ return Utils.parseString(data);
38
+ // Return
39
+ return Utils.parseString(data, defaultValue);
40
+ }
41
+ /**
42
+ * Get object data
43
+ * @param key Key name
44
+ */
45
+ getObject(key) {
46
+ // Get storage
47
+ const data = sessionStorage.getItem(key);
48
+ if (data == null)
49
+ return undefined;
50
+ return JSON.parse(data);
51
+ }
52
+ /**
53
+ * Set data
54
+ * @param key Key name
55
+ * @param data Data, null for removal
56
+ */
57
+ setData(key, data) {
58
+ StorageUtils.setSessionData(key, data);
59
+ if (this.globalFields.includes(key)) {
60
+ StorageUtils.setLocalData(key, data);
61
+ }
62
+ }
63
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.82",
3
+ "version": "1.0.83",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -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,37 @@
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
+ */
29
+ setData(key: string, data: unknown): void;
30
+ }
31
+
32
+ /**
33
+ * Storage constructor interface
34
+ */
35
+ export interface IStorageConstructor {
36
+ new (globalFields: string[]): IStorage;
37
+ }
@@ -0,0 +1,80 @@
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
+ */
14
+ constructor(private globalFields: string[]) {
15
+ if (globalFields.length == 0) return;
16
+
17
+ // Copy global fields to session storage where first item does not exist
18
+ const firsItem = sessionStorage.getItem(globalFields[0]);
19
+ if (firsItem) return;
20
+
21
+ globalFields.forEach((field) => {
22
+ const data = localStorage.getItem(field);
23
+ if (data != null) {
24
+ sessionStorage.setItem(field, data);
25
+ }
26
+ });
27
+ }
28
+
29
+ /**
30
+ * Get data
31
+ * @param key Key name
32
+ */
33
+ getData<T>(key: string): T | undefined;
34
+
35
+ /**
36
+ * Get data with default value
37
+ * @param key Key name
38
+ * @param defaultValue Default value
39
+ */
40
+ getData<T>(key: string, defaultValue: T): T;
41
+
42
+ /**
43
+ * Get data
44
+ * @param key Key name
45
+ * @param defaultValue Default value
46
+ */
47
+ getData<T>(key: string, defaultValue?: T): T | undefined {
48
+ // Get storage
49
+ const data = sessionStorage.getItem(key);
50
+
51
+ // No default value
52
+ if (defaultValue == null) return Utils.parseString<T>(data);
53
+
54
+ // Return
55
+ return Utils.parseString<T>(data, defaultValue);
56
+ }
57
+
58
+ /**
59
+ * Get object data
60
+ * @param key Key name
61
+ */
62
+ getObject<T extends {}>(key: string) {
63
+ // Get storage
64
+ const data = sessionStorage.getItem(key);
65
+ if (data == null) return undefined;
66
+ return <T>JSON.parse(data);
67
+ }
68
+
69
+ /**
70
+ * Set data
71
+ * @param key Key name
72
+ * @param data Data, null for removal
73
+ */
74
+ setData(key: string, data: unknown) {
75
+ StorageUtils.setSessionData(key, data);
76
+ if (this.globalFields.includes(key)) {
77
+ StorageUtils.setLocalData(key, data);
78
+ }
79
+ }
80
+ }