@etsoo/shared 1.0.93 → 1.0.97
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 +6 -0
- package/__tests__/DataTypes.ts +39 -0
- package/__tests__/StorageUtils.ts +5 -0
- package/lib/cjs/DataTypes.d.ts +44 -0
- package/lib/cjs/DataTypes.js +52 -0
- package/lib/cjs/storage/IStorage.d.ts +13 -1
- package/lib/cjs/storage/WindowStorage.d.ts +13 -1
- package/lib/cjs/storage/WindowStorage.js +26 -1
- package/lib/mjs/DataTypes.d.ts +44 -0
- package/lib/mjs/DataTypes.js +52 -0
- package/lib/mjs/storage/IStorage.d.ts +13 -1
- package/lib/mjs/storage/WindowStorage.d.ts +13 -1
- package/lib/mjs/storage/WindowStorage.js +26 -1
- package/package.json +1 -1
- package/src/DataTypes.ts +74 -0
- package/src/storage/IStorage.ts +15 -1
- package/src/storage/WindowStorage.ts +25 -1
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,10 @@ 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
|
+
|getIdValue|Get object id field value|
|
|
62
|
+
|getItemId|Get item id|
|
|
63
|
+
|getItemLabel|Get item label|
|
|
64
|
+
|getStringValue|Get object string field value|
|
|
59
65
|
|isBasicName|Check the type is a basic type or not (type guard)|
|
|
60
66
|
|isSimpleObject|Is the target a simple object, all values are simple type (Type guard)|
|
|
61
67
|
|isSimpleType|Is the input value simple type, include null and undefined|
|
package/__tests__/DataTypes.ts
CHANGED
|
@@ -74,6 +74,45 @@ 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 getIdValue', () => {
|
|
91
|
+
const data = { id: 1, flag: true };
|
|
92
|
+
expect(DataTypes.getIdValue(data, 'id')).toBe(1);
|
|
93
|
+
expect(DataTypes.getIdValue(data, 'flag')).toBe('true');
|
|
94
|
+
expect(DataTypes.getIdValue(data, 'unknown')).toBeNull();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('Tests for getStringValue', () => {
|
|
98
|
+
const data = { id: 1, flag: true };
|
|
99
|
+
expect(DataTypes.getStringValue(data, 'id')).toBe('1');
|
|
100
|
+
expect(DataTypes.getStringValue(data, 'flag')).toBe('true');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('Tests for getItemLabel', () => {
|
|
104
|
+
// Arrange
|
|
105
|
+
const items: DataTypes.IdLabelItem[] = [
|
|
106
|
+
{ id: 1, label: '123' },
|
|
107
|
+
{ id: '123', label: () => 'f123' },
|
|
108
|
+
{ id: () => 'f123', label: 'l123' }
|
|
109
|
+
];
|
|
110
|
+
|
|
111
|
+
// Assert
|
|
112
|
+
expect(DataTypes.getItemLabel(items[0])).toBe('123');
|
|
113
|
+
expect(DataTypes.getItemLabel(items[1])).toBe('f123');
|
|
114
|
+
});
|
|
115
|
+
|
|
77
116
|
test('Tests for isSimpleType', () => {
|
|
78
117
|
expect(DataTypes.isSimpleType(1)).toBeTruthy();
|
|
79
118
|
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', () => {
|
package/lib/cjs/DataTypes.d.ts
CHANGED
|
@@ -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
|
*/
|
|
@@ -215,6 +245,20 @@ export declare namespace DataTypes {
|
|
|
215
245
|
* @returns Keys
|
|
216
246
|
*/
|
|
217
247
|
function getEnumKeys<T extends EnumBase, K extends keyof T>(input: T): K[];
|
|
248
|
+
/**
|
|
249
|
+
* Get object id field value
|
|
250
|
+
* @param data Data
|
|
251
|
+
* @param key Property name
|
|
252
|
+
* @returns Id value
|
|
253
|
+
*/
|
|
254
|
+
function getIdValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
|
|
255
|
+
/**
|
|
256
|
+
* Get object string field value
|
|
257
|
+
* @param data Data
|
|
258
|
+
* @param key Property name
|
|
259
|
+
* @returns String value
|
|
260
|
+
*/
|
|
261
|
+
function getStringValue<T extends {}>(data: T | undefined | null, key: keyof T | string): string | undefined | null;
|
|
218
262
|
/**
|
|
219
263
|
* Check the type is a basic type or not (type guard)
|
|
220
264
|
* @param name Type name
|
package/lib/cjs/DataTypes.js
CHANGED
|
@@ -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
|
|
@@ -286,6 +308,36 @@ var DataTypes;
|
|
|
286
308
|
.map((item) => item);
|
|
287
309
|
}
|
|
288
310
|
DataTypes.getEnumKeys = getEnumKeys;
|
|
311
|
+
/**
|
|
312
|
+
* Get object id field value
|
|
313
|
+
* @param data Data
|
|
314
|
+
* @param key Property name
|
|
315
|
+
* @returns Id value
|
|
316
|
+
*/
|
|
317
|
+
function getIdValue(data, key) {
|
|
318
|
+
if (data == null)
|
|
319
|
+
return null;
|
|
320
|
+
const value = typeof key === 'string' ? Reflect.get(data, key) : data[key];
|
|
321
|
+
if (value == null)
|
|
322
|
+
return null;
|
|
323
|
+
if (typeof value === 'number')
|
|
324
|
+
return value;
|
|
325
|
+
return convert(value, 'string');
|
|
326
|
+
}
|
|
327
|
+
DataTypes.getIdValue = getIdValue;
|
|
328
|
+
/**
|
|
329
|
+
* Get object string field value
|
|
330
|
+
* @param data Data
|
|
331
|
+
* @param key Property name
|
|
332
|
+
* @returns String value
|
|
333
|
+
*/
|
|
334
|
+
function getStringValue(data, key) {
|
|
335
|
+
const value = getIdValue(data, key);
|
|
336
|
+
if (typeof value === 'number')
|
|
337
|
+
return value.toString();
|
|
338
|
+
return value;
|
|
339
|
+
}
|
|
340
|
+
DataTypes.getStringValue = getStringValue;
|
|
289
341
|
/**
|
|
290
342
|
* Check the type is a basic type or not (type guard)
|
|
291
343
|
* @param name Type name
|
|
@@ -2,12 +2,24 @@
|
|
|
2
2
|
* Storage interface
|
|
3
3
|
*/
|
|
4
4
|
export interface IStorage {
|
|
5
|
+
/**
|
|
6
|
+
* Clear keys
|
|
7
|
+
* @param keys Keys
|
|
8
|
+
* @param persisted Persisted or session data
|
|
9
|
+
*/
|
|
10
|
+
clear(keys: string[], persisted?: boolean): void;
|
|
5
11
|
/**
|
|
6
12
|
* Copy keys to session from persisted source
|
|
7
13
|
* @param keys Keys
|
|
8
14
|
* @param removeSource Remove from the source
|
|
9
15
|
*/
|
|
10
|
-
|
|
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;
|
|
11
23
|
/**
|
|
12
24
|
* Get data
|
|
13
25
|
* @param key Key name
|
|
@@ -4,12 +4,24 @@ import { IStorage } from './IStorage';
|
|
|
4
4
|
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
|
|
5
5
|
*/
|
|
6
6
|
export declare class WindowStorage implements IStorage {
|
|
7
|
+
/**
|
|
8
|
+
* Clear keys
|
|
9
|
+
* @param keys Keys
|
|
10
|
+
* @param persisted Persisted or session data
|
|
11
|
+
*/
|
|
12
|
+
clear(keys: string[], persisted?: boolean): void;
|
|
7
13
|
/**
|
|
8
14
|
* Copy keys to session from persisted source
|
|
9
15
|
* @param keys Keys
|
|
10
16
|
* @param removeSource Remove from the source
|
|
11
17
|
*/
|
|
12
|
-
|
|
18
|
+
copyFrom(keys: string[], removeSource?: boolean): void;
|
|
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;
|
|
13
25
|
/**
|
|
14
26
|
* Get data
|
|
15
27
|
* @param key Key name
|
|
@@ -8,18 +8,43 @@ 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
|
+
* Clear keys
|
|
13
|
+
* @param keys Keys
|
|
14
|
+
* @param persisted Persisted or session data
|
|
15
|
+
*/
|
|
16
|
+
clear(keys, persisted) {
|
|
17
|
+
keys.forEach((key) => {
|
|
18
|
+
if (persisted)
|
|
19
|
+
this.setPersistedData(key, undefined);
|
|
20
|
+
else
|
|
21
|
+
this.setData(key, undefined);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
11
24
|
/**
|
|
12
25
|
* Copy keys to session from persisted source
|
|
13
26
|
* @param keys Keys
|
|
14
27
|
* @param removeSource Remove from the source
|
|
15
28
|
*/
|
|
16
|
-
|
|
29
|
+
copyFrom(keys, removeSource) {
|
|
17
30
|
keys.forEach((key) => {
|
|
18
31
|
this.setData(key, this.getPersistedData(key));
|
|
19
32
|
if (removeSource)
|
|
20
33
|
this.setPersistedData(key, undefined);
|
|
21
34
|
});
|
|
22
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
|
+
});
|
|
47
|
+
}
|
|
23
48
|
/**
|
|
24
49
|
* Get data
|
|
25
50
|
* @param key Key name
|
package/lib/mjs/DataTypes.d.ts
CHANGED
|
@@ -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
|
*/
|
|
@@ -215,6 +245,20 @@ export declare namespace DataTypes {
|
|
|
215
245
|
* @returns Keys
|
|
216
246
|
*/
|
|
217
247
|
function getEnumKeys<T extends EnumBase, K extends keyof T>(input: T): K[];
|
|
248
|
+
/**
|
|
249
|
+
* Get object id field value
|
|
250
|
+
* @param data Data
|
|
251
|
+
* @param key Property name
|
|
252
|
+
* @returns Id value
|
|
253
|
+
*/
|
|
254
|
+
function getIdValue<T extends {}>(data: T | undefined | null, key: keyof T | string): IdType | undefined | null;
|
|
255
|
+
/**
|
|
256
|
+
* Get object string field value
|
|
257
|
+
* @param data Data
|
|
258
|
+
* @param key Property name
|
|
259
|
+
* @returns String value
|
|
260
|
+
*/
|
|
261
|
+
function getStringValue<T extends {}>(data: T | undefined | null, key: keyof T | string): string | undefined | null;
|
|
218
262
|
/**
|
|
219
263
|
* Check the type is a basic type or not (type guard)
|
|
220
264
|
* @param name Type name
|
package/lib/mjs/DataTypes.js
CHANGED
|
@@ -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
|
|
@@ -283,6 +305,36 @@ export var DataTypes;
|
|
|
283
305
|
.map((item) => item);
|
|
284
306
|
}
|
|
285
307
|
DataTypes.getEnumKeys = getEnumKeys;
|
|
308
|
+
/**
|
|
309
|
+
* Get object id field value
|
|
310
|
+
* @param data Data
|
|
311
|
+
* @param key Property name
|
|
312
|
+
* @returns Id value
|
|
313
|
+
*/
|
|
314
|
+
function getIdValue(data, key) {
|
|
315
|
+
if (data == null)
|
|
316
|
+
return null;
|
|
317
|
+
const value = typeof key === 'string' ? Reflect.get(data, key) : data[key];
|
|
318
|
+
if (value == null)
|
|
319
|
+
return null;
|
|
320
|
+
if (typeof value === 'number')
|
|
321
|
+
return value;
|
|
322
|
+
return convert(value, 'string');
|
|
323
|
+
}
|
|
324
|
+
DataTypes.getIdValue = getIdValue;
|
|
325
|
+
/**
|
|
326
|
+
* Get object string field value
|
|
327
|
+
* @param data Data
|
|
328
|
+
* @param key Property name
|
|
329
|
+
* @returns String value
|
|
330
|
+
*/
|
|
331
|
+
function getStringValue(data, key) {
|
|
332
|
+
const value = getIdValue(data, key);
|
|
333
|
+
if (typeof value === 'number')
|
|
334
|
+
return value.toString();
|
|
335
|
+
return value;
|
|
336
|
+
}
|
|
337
|
+
DataTypes.getStringValue = getStringValue;
|
|
286
338
|
/**
|
|
287
339
|
* Check the type is a basic type or not (type guard)
|
|
288
340
|
* @param name Type name
|
|
@@ -2,12 +2,24 @@
|
|
|
2
2
|
* Storage interface
|
|
3
3
|
*/
|
|
4
4
|
export interface IStorage {
|
|
5
|
+
/**
|
|
6
|
+
* Clear keys
|
|
7
|
+
* @param keys Keys
|
|
8
|
+
* @param persisted Persisted or session data
|
|
9
|
+
*/
|
|
10
|
+
clear(keys: string[], persisted?: boolean): void;
|
|
5
11
|
/**
|
|
6
12
|
* Copy keys to session from persisted source
|
|
7
13
|
* @param keys Keys
|
|
8
14
|
* @param removeSource Remove from the source
|
|
9
15
|
*/
|
|
10
|
-
|
|
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;
|
|
11
23
|
/**
|
|
12
24
|
* Get data
|
|
13
25
|
* @param key Key name
|
|
@@ -4,12 +4,24 @@ import { IStorage } from './IStorage';
|
|
|
4
4
|
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
|
|
5
5
|
*/
|
|
6
6
|
export declare class WindowStorage implements IStorage {
|
|
7
|
+
/**
|
|
8
|
+
* Clear keys
|
|
9
|
+
* @param keys Keys
|
|
10
|
+
* @param persisted Persisted or session data
|
|
11
|
+
*/
|
|
12
|
+
clear(keys: string[], persisted?: boolean): void;
|
|
7
13
|
/**
|
|
8
14
|
* Copy keys to session from persisted source
|
|
9
15
|
* @param keys Keys
|
|
10
16
|
* @param removeSource Remove from the source
|
|
11
17
|
*/
|
|
12
|
-
|
|
18
|
+
copyFrom(keys: string[], removeSource?: boolean): void;
|
|
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;
|
|
13
25
|
/**
|
|
14
26
|
* Get data
|
|
15
27
|
* @param key Key name
|
|
@@ -5,18 +5,43 @@ 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
|
+
* Clear keys
|
|
10
|
+
* @param keys Keys
|
|
11
|
+
* @param persisted Persisted or session data
|
|
12
|
+
*/
|
|
13
|
+
clear(keys, persisted) {
|
|
14
|
+
keys.forEach((key) => {
|
|
15
|
+
if (persisted)
|
|
16
|
+
this.setPersistedData(key, undefined);
|
|
17
|
+
else
|
|
18
|
+
this.setData(key, undefined);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
8
21
|
/**
|
|
9
22
|
* Copy keys to session from persisted source
|
|
10
23
|
* @param keys Keys
|
|
11
24
|
* @param removeSource Remove from the source
|
|
12
25
|
*/
|
|
13
|
-
|
|
26
|
+
copyFrom(keys, removeSource) {
|
|
14
27
|
keys.forEach((key) => {
|
|
15
28
|
this.setData(key, this.getPersistedData(key));
|
|
16
29
|
if (removeSource)
|
|
17
30
|
this.setPersistedData(key, undefined);
|
|
18
31
|
});
|
|
19
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
|
+
});
|
|
44
|
+
}
|
|
20
45
|
/**
|
|
21
46
|
* Get data
|
|
22
47
|
* @param key Key name
|
package/package.json
CHANGED
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
|
*/
|
|
@@ -450,6 +491,39 @@ export namespace DataTypes {
|
|
|
450
491
|
.map((item) => <K>item);
|
|
451
492
|
}
|
|
452
493
|
|
|
494
|
+
/**
|
|
495
|
+
* Get object id field value
|
|
496
|
+
* @param data Data
|
|
497
|
+
* @param key Property name
|
|
498
|
+
* @returns Id value
|
|
499
|
+
*/
|
|
500
|
+
export function getIdValue<T extends {}>(
|
|
501
|
+
data: T | undefined | null,
|
|
502
|
+
key: keyof T | string
|
|
503
|
+
): IdType | undefined | null {
|
|
504
|
+
if (data == null) return null;
|
|
505
|
+
const value =
|
|
506
|
+
typeof key === 'string' ? Reflect.get(data, key) : data[key];
|
|
507
|
+
if (value == null) return null;
|
|
508
|
+
if (typeof value === 'number') return value;
|
|
509
|
+
return convert(value, 'string');
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* Get object string field value
|
|
514
|
+
* @param data Data
|
|
515
|
+
* @param key Property name
|
|
516
|
+
* @returns String value
|
|
517
|
+
*/
|
|
518
|
+
export function getStringValue<T extends {}>(
|
|
519
|
+
data: T | undefined | null,
|
|
520
|
+
key: keyof T | string
|
|
521
|
+
): string | undefined | null {
|
|
522
|
+
const value = getIdValue(data, key);
|
|
523
|
+
if (typeof value === 'number') return value.toString();
|
|
524
|
+
return value;
|
|
525
|
+
}
|
|
526
|
+
|
|
453
527
|
/**
|
|
454
528
|
* Check the type is a basic type or not (type guard)
|
|
455
529
|
* @param name Type name
|
package/src/storage/IStorage.ts
CHANGED
|
@@ -2,12 +2,26 @@
|
|
|
2
2
|
* Storage interface
|
|
3
3
|
*/
|
|
4
4
|
export interface IStorage {
|
|
5
|
+
/**
|
|
6
|
+
* Clear keys
|
|
7
|
+
* @param keys Keys
|
|
8
|
+
* @param persisted Persisted or session data
|
|
9
|
+
*/
|
|
10
|
+
clear(keys: string[], persisted?: boolean): void;
|
|
11
|
+
|
|
5
12
|
/**
|
|
6
13
|
* Copy keys to session from persisted source
|
|
7
14
|
* @param keys Keys
|
|
8
15
|
* @param removeSource Remove from the source
|
|
9
16
|
*/
|
|
10
|
-
|
|
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;
|
|
11
25
|
|
|
12
26
|
/**
|
|
13
27
|
* Get data
|
|
@@ -7,18 +7,42 @@ import { IStorage } from './IStorage';
|
|
|
7
7
|
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
|
|
8
8
|
*/
|
|
9
9
|
export class WindowStorage implements IStorage {
|
|
10
|
+
/**
|
|
11
|
+
* Clear keys
|
|
12
|
+
* @param keys Keys
|
|
13
|
+
* @param persisted Persisted or session data
|
|
14
|
+
*/
|
|
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
|
+
}
|
|
21
|
+
|
|
10
22
|
/**
|
|
11
23
|
* Copy keys to session from persisted source
|
|
12
24
|
* @param keys Keys
|
|
13
25
|
* @param removeSource Remove from the source
|
|
14
26
|
*/
|
|
15
|
-
|
|
27
|
+
copyFrom(keys: string[], removeSource?: boolean) {
|
|
16
28
|
keys.forEach((key) => {
|
|
17
29
|
this.setData(key, this.getPersistedData(key));
|
|
18
30
|
if (removeSource) this.setPersistedData(key, undefined);
|
|
19
31
|
});
|
|
20
32
|
}
|
|
21
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Copy keys to persisted source
|
|
36
|
+
* @param keys Keys
|
|
37
|
+
* @param removeSource Remove from the source
|
|
38
|
+
*/
|
|
39
|
+
copyTo(keys: string[], removeSource?: boolean) {
|
|
40
|
+
keys.forEach((key) => {
|
|
41
|
+
this.setPersistedData(key, this.getData(key));
|
|
42
|
+
if (removeSource) this.setData(key, undefined);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
22
46
|
/**
|
|
23
47
|
* Get data
|
|
24
48
|
* @param key Key name
|