@etsoo/shared 1.0.72 → 1.0.76
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 +4 -0
- package/__tests__/DomUtils.ts +6 -0
- package/__tests__/StorageUtils.ts +14 -0
- package/__tests__/Utils.ts +7 -0
- package/lib/cjs/StorageUtils.d.ts +10 -0
- package/lib/cjs/StorageUtils.js +24 -0
- package/lib/cjs/Utils.d.ts +13 -1
- package/lib/cjs/Utils.js +40 -7
- package/lib/mjs/StorageUtils.d.ts +10 -0
- package/lib/mjs/StorageUtils.js +24 -0
- package/lib/mjs/Utils.d.ts +13 -1
- package/lib/mjs/Utils.js +40 -7
- package/package.json +10 -10
- package/src/StorageUtils.ts +22 -0
- package/src/Utils.ts +48 -17
package/README.md
CHANGED
|
@@ -122,13 +122,16 @@ Storage related utilities
|
|
|
122
122
|
|setLocalData|Set local storage data|
|
|
123
123
|
|setSessionData|Set session storage data|
|
|
124
124
|
|getLocalData|Get local storage data|
|
|
125
|
+
|getLocalObject|Get local storage object data|
|
|
125
126
|
|getSessionData|Get session storage data|
|
|
127
|
+
|getSessionObject|Get session storage object data|
|
|
126
128
|
|
|
127
129
|
## Utils
|
|
128
130
|
String and other related utilities
|
|
129
131
|
|
|
130
132
|
|Name|Description|
|
|
131
133
|
|---:|---|
|
|
134
|
+
|charsToNumber|Base64 chars to number|
|
|
132
135
|
|formatInitial|Format inital character to lower case or upper case|
|
|
133
136
|
|formatString|Format string with parameters|
|
|
134
137
|
|getDataChanges|Get data changed fields with input data updated|
|
|
@@ -137,6 +140,7 @@ String and other related utilities
|
|
|
137
140
|
|mergeFormData|Merge form data to primary one|
|
|
138
141
|
|mergeClasses|Merge class names|
|
|
139
142
|
|newGUID|Create a GUID|
|
|
143
|
+
|numberToChars|Number to base64 chars|
|
|
140
144
|
|objectEqual|Test two objects are equal or not|
|
|
141
145
|
|parseString|Parse string (JSON) to specific type|
|
|
142
146
|
|setLabels|Set source with new labels|
|
package/__tests__/DomUtils.ts
CHANGED
|
@@ -245,6 +245,12 @@ test('Tests for headersToObject', () => {
|
|
|
245
245
|
);
|
|
246
246
|
});
|
|
247
247
|
|
|
248
|
+
test('Tests for isFormData', () => {
|
|
249
|
+
const formData = new FormData();
|
|
250
|
+
expect(DomUtils.isFormData(formData)).toBeTruthy();
|
|
251
|
+
expect(DomUtils.isFormData({})).toBeFalsy();
|
|
252
|
+
});
|
|
253
|
+
|
|
248
254
|
test('Tests for isJSONContentType', () => {
|
|
249
255
|
expect(DomUtils.isJSONContentType('application/problem+json')).toBeTruthy();
|
|
250
256
|
expect(DomUtils.isJSONContentType('application/javascript')).toBeTruthy();
|
|
@@ -12,3 +12,17 @@ test('Tests for all', () => {
|
|
|
12
12
|
expect(StorageUtils.getSessionData('number', 0)).toBe(3.14);
|
|
13
13
|
expect(StorageUtils.getSessionData('test', {})).toHaveProperty('id', 123);
|
|
14
14
|
});
|
|
15
|
+
|
|
16
|
+
test('Tests for getLocalObject', () => {
|
|
17
|
+
StorageUtils.setLocalData('test', { id: 123, name: 'test' });
|
|
18
|
+
const data = StorageUtils.getLocalObject<{ id: number }>('test');
|
|
19
|
+
expect(data?.id).toBe(123);
|
|
20
|
+
expect(data).toHaveProperty('name', 'test');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('Tests for getSessionObject', () => {
|
|
24
|
+
StorageUtils.setSessionData('test', { id: 123, name: 'test' });
|
|
25
|
+
const data = StorageUtils.getSessionObject<{ id: number }>('test');
|
|
26
|
+
expect(data?.id).toBe(123);
|
|
27
|
+
expect(data).toHaveProperty('name', 'test');
|
|
28
|
+
});
|
package/__tests__/Utils.ts
CHANGED
|
@@ -67,6 +67,13 @@ test('Tests for newGUID', () => {
|
|
|
67
67
|
expect(id1.length).toBe(id2.length);
|
|
68
68
|
});
|
|
69
69
|
|
|
70
|
+
test('Tests for numberToChars and charsToNumber', () => {
|
|
71
|
+
const num = 1638777042242;
|
|
72
|
+
const chars = Utils.numberToChars(num);
|
|
73
|
+
expect(chars).toEqual('QmpkdVgv');
|
|
74
|
+
expect(Utils.charsToNumber(chars)).toEqual(num);
|
|
75
|
+
});
|
|
76
|
+
|
|
70
77
|
test('Tests for removeNonLetters', () => {
|
|
71
78
|
const input = '1234-5678@abc.';
|
|
72
79
|
const result = '12345678abc';
|
|
@@ -21,9 +21,19 @@ export declare namespace StorageUtils {
|
|
|
21
21
|
* @param defaultValue Default value
|
|
22
22
|
*/
|
|
23
23
|
function getLocalData<T>(key: string, defaultValue: T): T;
|
|
24
|
+
/**
|
|
25
|
+
* Get local storage object data
|
|
26
|
+
* @param key Key name
|
|
27
|
+
*/
|
|
28
|
+
function getLocalObject<T extends {}>(key: string): T | undefined;
|
|
24
29
|
/**
|
|
25
30
|
* Get session storage data
|
|
26
31
|
* @param key Key name
|
|
27
32
|
*/
|
|
28
33
|
function getSessionData<T>(key: string, defaultValue: T): T;
|
|
34
|
+
/**
|
|
35
|
+
* Get session storage object data
|
|
36
|
+
* @param key Key name
|
|
37
|
+
*/
|
|
38
|
+
function getSessionObject<T extends {}>(key: string): T | undefined;
|
|
29
39
|
}
|
package/lib/cjs/StorageUtils.js
CHANGED
|
@@ -51,6 +51,18 @@ var StorageUtils;
|
|
|
51
51
|
return Utils_1.Utils.parseString(data, defaultValue);
|
|
52
52
|
}
|
|
53
53
|
StorageUtils.getLocalData = getLocalData;
|
|
54
|
+
/**
|
|
55
|
+
* Get local storage object data
|
|
56
|
+
* @param key Key name
|
|
57
|
+
*/
|
|
58
|
+
function getLocalObject(key) {
|
|
59
|
+
// Get storage
|
|
60
|
+
const data = localStorage.getItem(key);
|
|
61
|
+
if (data == null)
|
|
62
|
+
return undefined;
|
|
63
|
+
return JSON.parse(data);
|
|
64
|
+
}
|
|
65
|
+
StorageUtils.getLocalObject = getLocalObject;
|
|
54
66
|
/**
|
|
55
67
|
* Get session storage data
|
|
56
68
|
* @param key Key name
|
|
@@ -62,4 +74,16 @@ var StorageUtils;
|
|
|
62
74
|
return Utils_1.Utils.parseString(data, defaultValue);
|
|
63
75
|
}
|
|
64
76
|
StorageUtils.getSessionData = getSessionData;
|
|
77
|
+
/**
|
|
78
|
+
* Get session storage object data
|
|
79
|
+
* @param key Key name
|
|
80
|
+
*/
|
|
81
|
+
function getSessionObject(key) {
|
|
82
|
+
// Get storage
|
|
83
|
+
const data = sessionStorage.getItem(key);
|
|
84
|
+
if (data == null)
|
|
85
|
+
return undefined;
|
|
86
|
+
return JSON.parse(data);
|
|
87
|
+
}
|
|
88
|
+
StorageUtils.getSessionObject = getSessionObject;
|
|
65
89
|
})(StorageUtils = exports.StorageUtils || (exports.StorageUtils = {}));
|
package/lib/cjs/Utils.d.ts
CHANGED
|
@@ -35,6 +35,12 @@ declare global {
|
|
|
35
35
|
* Utilities
|
|
36
36
|
*/
|
|
37
37
|
export declare namespace Utils {
|
|
38
|
+
/**
|
|
39
|
+
* Base64 chars to number
|
|
40
|
+
* @param base64Chars Base64 chars
|
|
41
|
+
* @returns Number
|
|
42
|
+
*/
|
|
43
|
+
function charsToNumber(base64Chars: string): number;
|
|
38
44
|
/**
|
|
39
45
|
* Format inital character to lower case or upper case
|
|
40
46
|
* @param input Input string
|
|
@@ -89,6 +95,12 @@ export declare namespace Utils {
|
|
|
89
95
|
* Create a GUID
|
|
90
96
|
*/
|
|
91
97
|
function newGUID(): string;
|
|
98
|
+
/**
|
|
99
|
+
* Number to base64 chars
|
|
100
|
+
* @param num Input number
|
|
101
|
+
* @returns Result
|
|
102
|
+
*/
|
|
103
|
+
function numberToChars(num: number): string;
|
|
92
104
|
/**
|
|
93
105
|
* Test two objects are equal or not
|
|
94
106
|
* @param obj1 Object 1
|
|
@@ -117,7 +129,7 @@ export declare namespace Utils {
|
|
|
117
129
|
* @param labels Labels
|
|
118
130
|
* @param reference Key reference dictionary
|
|
119
131
|
*/
|
|
120
|
-
const setLabels: (source: {}, labels: DataTypes.StringRecord, reference?: Readonly<DataTypes.StringDictionary>) => void;
|
|
132
|
+
const setLabels: (source: {}, labels: DataTypes.StringRecord, reference?: Readonly<DataTypes.StringDictionary> | undefined) => void;
|
|
121
133
|
/**
|
|
122
134
|
* Snake name to works, 'snake_name' to 'Snake Name'
|
|
123
135
|
* @param name Name text
|
package/lib/cjs/Utils.js
CHANGED
|
@@ -29,6 +29,20 @@ String.prototype.removeNonLetters = function () {
|
|
|
29
29
|
*/
|
|
30
30
|
var Utils;
|
|
31
31
|
(function (Utils) {
|
|
32
|
+
/**
|
|
33
|
+
* Base64 chars to number
|
|
34
|
+
* @param base64Chars Base64 chars
|
|
35
|
+
* @returns Number
|
|
36
|
+
*/
|
|
37
|
+
function charsToNumber(base64Chars) {
|
|
38
|
+
const chars = typeof Buffer === 'undefined'
|
|
39
|
+
? [...atob(base64Chars)].map((char) => char.charCodeAt(0))
|
|
40
|
+
: [...Buffer.from(base64Chars, 'base64')];
|
|
41
|
+
return chars.reduce((previousValue, currentValue, currentIndex) => {
|
|
42
|
+
return previousValue + currentValue * Math.pow(128, currentIndex);
|
|
43
|
+
}, 0);
|
|
44
|
+
}
|
|
45
|
+
Utils.charsToNumber = charsToNumber;
|
|
32
46
|
/**
|
|
33
47
|
* Format inital character to lower case or upper case
|
|
34
48
|
* @param input Input string
|
|
@@ -149,6 +163,27 @@ var Utils;
|
|
|
149
163
|
});
|
|
150
164
|
}
|
|
151
165
|
Utils.newGUID = newGUID;
|
|
166
|
+
/**
|
|
167
|
+
* Number to base64 chars
|
|
168
|
+
* @param num Input number
|
|
169
|
+
* @returns Result
|
|
170
|
+
*/
|
|
171
|
+
function numberToChars(num) {
|
|
172
|
+
const codes = [];
|
|
173
|
+
while (num > 0) {
|
|
174
|
+
const code = num % 128;
|
|
175
|
+
codes.push(code);
|
|
176
|
+
num = (num - code) / 128;
|
|
177
|
+
}
|
|
178
|
+
if (typeof Buffer === 'undefined') {
|
|
179
|
+
return btoa(String.fromCharCode(...codes));
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
const buffer = Buffer.from(codes);
|
|
183
|
+
return buffer.toString('base64');
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
Utils.numberToChars = numberToChars;
|
|
152
187
|
/**
|
|
153
188
|
* Test two objects are equal or not
|
|
154
189
|
* @param obj1 Object 1
|
|
@@ -232,20 +267,18 @@ var Utils;
|
|
|
232
267
|
* @param labels Labels
|
|
233
268
|
* @param reference Key reference dictionary
|
|
234
269
|
*/
|
|
235
|
-
Utils.setLabels = (source, labels, reference
|
|
236
|
-
|
|
270
|
+
Utils.setLabels = (source, labels, reference) => {
|
|
271
|
+
Object.keys(source).forEach((key) => {
|
|
237
272
|
var _a;
|
|
238
273
|
// Reference key
|
|
239
|
-
const labelKey = (_a = reference[key]) !== null && _a !== void 0 ? _a : key;
|
|
274
|
+
const labelKey = reference == null ? key : (_a = reference[key]) !== null && _a !== void 0 ? _a : key;
|
|
240
275
|
// Label
|
|
241
276
|
const label = labels[labelKey];
|
|
242
277
|
if (label != null) {
|
|
243
278
|
// If found, update
|
|
244
|
-
|
|
279
|
+
Reflect.set(source, key, label);
|
|
245
280
|
}
|
|
246
|
-
|
|
247
|
-
}, {});
|
|
248
|
-
Object.assign(source, newLabels);
|
|
281
|
+
});
|
|
249
282
|
};
|
|
250
283
|
/**
|
|
251
284
|
* Snake name to works, 'snake_name' to 'Snake Name'
|
|
@@ -21,9 +21,19 @@ export declare namespace StorageUtils {
|
|
|
21
21
|
* @param defaultValue Default value
|
|
22
22
|
*/
|
|
23
23
|
function getLocalData<T>(key: string, defaultValue: T): T;
|
|
24
|
+
/**
|
|
25
|
+
* Get local storage object data
|
|
26
|
+
* @param key Key name
|
|
27
|
+
*/
|
|
28
|
+
function getLocalObject<T extends {}>(key: string): T | undefined;
|
|
24
29
|
/**
|
|
25
30
|
* Get session storage data
|
|
26
31
|
* @param key Key name
|
|
27
32
|
*/
|
|
28
33
|
function getSessionData<T>(key: string, defaultValue: T): T;
|
|
34
|
+
/**
|
|
35
|
+
* Get session storage object data
|
|
36
|
+
* @param key Key name
|
|
37
|
+
*/
|
|
38
|
+
function getSessionObject<T extends {}>(key: string): T | undefined;
|
|
29
39
|
}
|
package/lib/mjs/StorageUtils.js
CHANGED
|
@@ -48,6 +48,18 @@ export var StorageUtils;
|
|
|
48
48
|
return Utils.parseString(data, defaultValue);
|
|
49
49
|
}
|
|
50
50
|
StorageUtils.getLocalData = getLocalData;
|
|
51
|
+
/**
|
|
52
|
+
* Get local storage object data
|
|
53
|
+
* @param key Key name
|
|
54
|
+
*/
|
|
55
|
+
function getLocalObject(key) {
|
|
56
|
+
// Get storage
|
|
57
|
+
const data = localStorage.getItem(key);
|
|
58
|
+
if (data == null)
|
|
59
|
+
return undefined;
|
|
60
|
+
return JSON.parse(data);
|
|
61
|
+
}
|
|
62
|
+
StorageUtils.getLocalObject = getLocalObject;
|
|
51
63
|
/**
|
|
52
64
|
* Get session storage data
|
|
53
65
|
* @param key Key name
|
|
@@ -59,4 +71,16 @@ export var StorageUtils;
|
|
|
59
71
|
return Utils.parseString(data, defaultValue);
|
|
60
72
|
}
|
|
61
73
|
StorageUtils.getSessionData = getSessionData;
|
|
74
|
+
/**
|
|
75
|
+
* Get session storage object data
|
|
76
|
+
* @param key Key name
|
|
77
|
+
*/
|
|
78
|
+
function getSessionObject(key) {
|
|
79
|
+
// Get storage
|
|
80
|
+
const data = sessionStorage.getItem(key);
|
|
81
|
+
if (data == null)
|
|
82
|
+
return undefined;
|
|
83
|
+
return JSON.parse(data);
|
|
84
|
+
}
|
|
85
|
+
StorageUtils.getSessionObject = getSessionObject;
|
|
62
86
|
})(StorageUtils || (StorageUtils = {}));
|
package/lib/mjs/Utils.d.ts
CHANGED
|
@@ -35,6 +35,12 @@ declare global {
|
|
|
35
35
|
* Utilities
|
|
36
36
|
*/
|
|
37
37
|
export declare namespace Utils {
|
|
38
|
+
/**
|
|
39
|
+
* Base64 chars to number
|
|
40
|
+
* @param base64Chars Base64 chars
|
|
41
|
+
* @returns Number
|
|
42
|
+
*/
|
|
43
|
+
function charsToNumber(base64Chars: string): number;
|
|
38
44
|
/**
|
|
39
45
|
* Format inital character to lower case or upper case
|
|
40
46
|
* @param input Input string
|
|
@@ -89,6 +95,12 @@ export declare namespace Utils {
|
|
|
89
95
|
* Create a GUID
|
|
90
96
|
*/
|
|
91
97
|
function newGUID(): string;
|
|
98
|
+
/**
|
|
99
|
+
* Number to base64 chars
|
|
100
|
+
* @param num Input number
|
|
101
|
+
* @returns Result
|
|
102
|
+
*/
|
|
103
|
+
function numberToChars(num: number): string;
|
|
92
104
|
/**
|
|
93
105
|
* Test two objects are equal or not
|
|
94
106
|
* @param obj1 Object 1
|
|
@@ -117,7 +129,7 @@ export declare namespace Utils {
|
|
|
117
129
|
* @param labels Labels
|
|
118
130
|
* @param reference Key reference dictionary
|
|
119
131
|
*/
|
|
120
|
-
const setLabels: (source: {}, labels: DataTypes.StringRecord, reference?: Readonly<DataTypes.StringDictionary>) => void;
|
|
132
|
+
const setLabels: (source: {}, labels: DataTypes.StringRecord, reference?: Readonly<DataTypes.StringDictionary> | undefined) => void;
|
|
121
133
|
/**
|
|
122
134
|
* Snake name to works, 'snake_name' to 'Snake Name'
|
|
123
135
|
* @param name Name text
|
package/lib/mjs/Utils.js
CHANGED
|
@@ -26,6 +26,20 @@ String.prototype.removeNonLetters = function () {
|
|
|
26
26
|
*/
|
|
27
27
|
export var Utils;
|
|
28
28
|
(function (Utils) {
|
|
29
|
+
/**
|
|
30
|
+
* Base64 chars to number
|
|
31
|
+
* @param base64Chars Base64 chars
|
|
32
|
+
* @returns Number
|
|
33
|
+
*/
|
|
34
|
+
function charsToNumber(base64Chars) {
|
|
35
|
+
const chars = typeof Buffer === 'undefined'
|
|
36
|
+
? [...atob(base64Chars)].map((char) => char.charCodeAt(0))
|
|
37
|
+
: [...Buffer.from(base64Chars, 'base64')];
|
|
38
|
+
return chars.reduce((previousValue, currentValue, currentIndex) => {
|
|
39
|
+
return previousValue + currentValue * Math.pow(128, currentIndex);
|
|
40
|
+
}, 0);
|
|
41
|
+
}
|
|
42
|
+
Utils.charsToNumber = charsToNumber;
|
|
29
43
|
/**
|
|
30
44
|
* Format inital character to lower case or upper case
|
|
31
45
|
* @param input Input string
|
|
@@ -146,6 +160,27 @@ export var Utils;
|
|
|
146
160
|
});
|
|
147
161
|
}
|
|
148
162
|
Utils.newGUID = newGUID;
|
|
163
|
+
/**
|
|
164
|
+
* Number to base64 chars
|
|
165
|
+
* @param num Input number
|
|
166
|
+
* @returns Result
|
|
167
|
+
*/
|
|
168
|
+
function numberToChars(num) {
|
|
169
|
+
const codes = [];
|
|
170
|
+
while (num > 0) {
|
|
171
|
+
const code = num % 128;
|
|
172
|
+
codes.push(code);
|
|
173
|
+
num = (num - code) / 128;
|
|
174
|
+
}
|
|
175
|
+
if (typeof Buffer === 'undefined') {
|
|
176
|
+
return btoa(String.fromCharCode(...codes));
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
const buffer = Buffer.from(codes);
|
|
180
|
+
return buffer.toString('base64');
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
Utils.numberToChars = numberToChars;
|
|
149
184
|
/**
|
|
150
185
|
* Test two objects are equal or not
|
|
151
186
|
* @param obj1 Object 1
|
|
@@ -229,20 +264,18 @@ export var Utils;
|
|
|
229
264
|
* @param labels Labels
|
|
230
265
|
* @param reference Key reference dictionary
|
|
231
266
|
*/
|
|
232
|
-
Utils.setLabels = (source, labels, reference
|
|
233
|
-
|
|
267
|
+
Utils.setLabels = (source, labels, reference) => {
|
|
268
|
+
Object.keys(source).forEach((key) => {
|
|
234
269
|
var _a;
|
|
235
270
|
// Reference key
|
|
236
|
-
const labelKey = (_a = reference[key]) !== null && _a !== void 0 ? _a : key;
|
|
271
|
+
const labelKey = reference == null ? key : (_a = reference[key]) !== null && _a !== void 0 ? _a : key;
|
|
237
272
|
// Label
|
|
238
273
|
const label = labels[labelKey];
|
|
239
274
|
if (label != null) {
|
|
240
275
|
// If found, update
|
|
241
|
-
|
|
276
|
+
Reflect.set(source, key, label);
|
|
242
277
|
}
|
|
243
|
-
|
|
244
|
-
}, {});
|
|
245
|
-
Object.assign(source, newLabels);
|
|
278
|
+
});
|
|
246
279
|
};
|
|
247
280
|
/**
|
|
248
281
|
* Snake name to works, 'snake_name' to 'Snake Name'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/shared",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.76",
|
|
4
4
|
"description": "TypeScript shared utilities and functions",
|
|
5
5
|
"main": "lib/cjs/index.js",
|
|
6
6
|
"module": "lib/mjs/index.js",
|
|
@@ -54,14 +54,14 @@
|
|
|
54
54
|
"homepage": "https://github.com/ETSOO/Shared#readme",
|
|
55
55
|
"dependencies": {},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@types/jest": "^27.0.
|
|
58
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
59
|
-
"@typescript-eslint/parser": "^5.
|
|
60
|
-
"eslint": "^8.0
|
|
61
|
-
"eslint-config-airbnb-base": "^
|
|
62
|
-
"eslint-plugin-import": "^2.25.
|
|
63
|
-
"jest": "^27.
|
|
64
|
-
"ts-jest": "^27.0.
|
|
65
|
-
"typescript": "^4.
|
|
57
|
+
"@types/jest": "^27.0.3",
|
|
58
|
+
"@typescript-eslint/eslint-plugin": "^5.4.0",
|
|
59
|
+
"@typescript-eslint/parser": "^5.4.0",
|
|
60
|
+
"eslint": "^8.2.0",
|
|
61
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
62
|
+
"eslint-plugin-import": "^2.25.3",
|
|
63
|
+
"jest": "^27.3.1",
|
|
64
|
+
"ts-jest": "^27.0.7",
|
|
65
|
+
"typescript": "^4.5.2"
|
|
66
66
|
}
|
|
67
67
|
}
|
package/src/StorageUtils.ts
CHANGED
|
@@ -57,6 +57,17 @@ export namespace StorageUtils {
|
|
|
57
57
|
return Utils.parseString(data, defaultValue);
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Get local storage object data
|
|
62
|
+
* @param key Key name
|
|
63
|
+
*/
|
|
64
|
+
export function getLocalObject<T extends {}>(key: string) {
|
|
65
|
+
// Get storage
|
|
66
|
+
const data = localStorage.getItem(key);
|
|
67
|
+
if (data == null) return undefined;
|
|
68
|
+
return <T>JSON.parse(data);
|
|
69
|
+
}
|
|
70
|
+
|
|
60
71
|
/**
|
|
61
72
|
* Get session storage data
|
|
62
73
|
* @param key Key name
|
|
@@ -68,4 +79,15 @@ export namespace StorageUtils {
|
|
|
68
79
|
// Return
|
|
69
80
|
return Utils.parseString(data, defaultValue);
|
|
70
81
|
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Get session storage object data
|
|
85
|
+
* @param key Key name
|
|
86
|
+
*/
|
|
87
|
+
export function getSessionObject<T extends {}>(key: string) {
|
|
88
|
+
// Get storage
|
|
89
|
+
const data = sessionStorage.getItem(key);
|
|
90
|
+
if (data == null) return undefined;
|
|
91
|
+
return <T>JSON.parse(data);
|
|
92
|
+
}
|
|
71
93
|
}
|
package/src/Utils.ts
CHANGED
|
@@ -77,6 +77,22 @@ String.prototype.removeNonLetters = function (this: string) {
|
|
|
77
77
|
* Utilities
|
|
78
78
|
*/
|
|
79
79
|
export namespace Utils {
|
|
80
|
+
/**
|
|
81
|
+
* Base64 chars to number
|
|
82
|
+
* @param base64Chars Base64 chars
|
|
83
|
+
* @returns Number
|
|
84
|
+
*/
|
|
85
|
+
export function charsToNumber(base64Chars: string) {
|
|
86
|
+
const chars =
|
|
87
|
+
typeof Buffer === 'undefined'
|
|
88
|
+
? [...atob(base64Chars)].map((char) => char.charCodeAt(0))
|
|
89
|
+
: [...Buffer.from(base64Chars, 'base64')];
|
|
90
|
+
|
|
91
|
+
return chars.reduce((previousValue, currentValue, currentIndex) => {
|
|
92
|
+
return previousValue + currentValue * Math.pow(128, currentIndex);
|
|
93
|
+
}, 0);
|
|
94
|
+
}
|
|
95
|
+
|
|
80
96
|
/**
|
|
81
97
|
* Format inital character to lower case or upper case
|
|
82
98
|
* @param input Input string
|
|
@@ -218,6 +234,27 @@ export namespace Utils {
|
|
|
218
234
|
});
|
|
219
235
|
}
|
|
220
236
|
|
|
237
|
+
/**
|
|
238
|
+
* Number to base64 chars
|
|
239
|
+
* @param num Input number
|
|
240
|
+
* @returns Result
|
|
241
|
+
*/
|
|
242
|
+
export function numberToChars(num: number) {
|
|
243
|
+
const codes = [];
|
|
244
|
+
while (num > 0) {
|
|
245
|
+
const code = num % 128;
|
|
246
|
+
codes.push(code);
|
|
247
|
+
num = (num - code) / 128;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (typeof Buffer === 'undefined') {
|
|
251
|
+
return btoa(String.fromCharCode(...codes));
|
|
252
|
+
} else {
|
|
253
|
+
const buffer = Buffer.from(codes);
|
|
254
|
+
return buffer.toString('base64');
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
221
258
|
/**
|
|
222
259
|
* Test two objects are equal or not
|
|
223
260
|
* @param obj1 Object 1
|
|
@@ -315,26 +352,20 @@ export namespace Utils {
|
|
|
315
352
|
export const setLabels = (
|
|
316
353
|
source: {},
|
|
317
354
|
labels: DataTypes.StringRecord,
|
|
318
|
-
reference
|
|
355
|
+
reference?: Readonly<DataTypes.StringDictionary>
|
|
319
356
|
) => {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
const labelKey = reference[key] ?? key;
|
|
357
|
+
Object.keys(source).forEach((key) => {
|
|
358
|
+
// Reference key
|
|
359
|
+
const labelKey = reference == null ? key : reference[key] ?? key;
|
|
324
360
|
|
|
325
|
-
|
|
326
|
-
|
|
361
|
+
// Label
|
|
362
|
+
const label = labels[labelKey];
|
|
327
363
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
return newLabels;
|
|
334
|
-
},
|
|
335
|
-
{} as DataTypes.StringDictionary
|
|
336
|
-
);
|
|
337
|
-
Object.assign(source, newLabels);
|
|
364
|
+
if (label != null) {
|
|
365
|
+
// If found, update
|
|
366
|
+
Reflect.set(source, key, label);
|
|
367
|
+
}
|
|
368
|
+
});
|
|
338
369
|
};
|
|
339
370
|
|
|
340
371
|
/**
|