@etsoo/shared 1.0.77 → 1.0.81
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 +2 -0
- package/__tests__/StorageUtils.ts +2 -1
- package/__tests__/Utils.ts +11 -0
- package/lib/cjs/StorageUtils.d.ts +2 -2
- package/lib/cjs/Utils.d.ts +20 -2
- package/lib/cjs/Utils.js +47 -16
- package/lib/mjs/StorageUtils.d.ts +2 -2
- package/lib/mjs/Utils.d.ts +20 -2
- package/lib/mjs/Utils.js +47 -16
- package/package.json +7 -7
- package/src/StorageUtils.ts +10 -4
- package/src/Utils.ts +68 -22
package/README.md
CHANGED
|
@@ -143,6 +143,8 @@ String and other related utilities
|
|
|
143
143
|
|newGUID|Create a GUID|
|
|
144
144
|
|numberToChars|Number to base64 chars|
|
|
145
145
|
|objectEqual|Test two objects are equal or not|
|
|
146
|
+
|objectKeys|Get two object's unqiue properties|
|
|
147
|
+
|objectUpdated|Get the new object's updated fields contrast to the previous object|
|
|
146
148
|
|parseString|Parse string (JSON) to specific type|
|
|
147
149
|
|setLabels|Set source with new labels|
|
|
148
150
|
|snakeNameToWord|Snake name to works, 'snake_name' to 'Snake Name'|
|
|
@@ -7,7 +7,8 @@ test('Tests for all', () => {
|
|
|
7
7
|
StorageUtils.setSessionData('number', 3.14);
|
|
8
8
|
StorageUtils.setSessionData('test', { id: 123, name: 'test' });
|
|
9
9
|
|
|
10
|
-
expect(StorageUtils.getSessionData('string'
|
|
10
|
+
expect(StorageUtils.getSessionData<string>('string')).toBe('test');
|
|
11
|
+
expect(StorageUtils.getSessionData('string1', '')).toBe('');
|
|
11
12
|
expect(StorageUtils.getSessionData('boolean', false)).toBe(true);
|
|
12
13
|
expect(StorageUtils.getSessionData('number', 0)).toBe(3.14);
|
|
13
14
|
expect(StorageUtils.getSessionData('test', {})).toHaveProperty('id', 123);
|
package/__tests__/Utils.ts
CHANGED
|
@@ -92,9 +92,20 @@ test('Tests for objectEqual', () => {
|
|
|
92
92
|
expect(Utils.objectEqual(obj1, obj2, ['a'], 2)).toBeFalsy();
|
|
93
93
|
});
|
|
94
94
|
|
|
95
|
+
test('Tests for objectUpdated', () => {
|
|
96
|
+
const objPrev = { a: 1, b: 'abc', c: true, d: null, f: [1, 2] };
|
|
97
|
+
const objNew = { a: 2, b: 'abc', d: new Date(), f: [1, 2, 3] };
|
|
98
|
+
const fields = Utils.objectUpdated(objNew, objPrev, ['d']);
|
|
99
|
+
expect(fields.sort()).toStrictEqual(['a', 'c', 'f']);
|
|
100
|
+
});
|
|
101
|
+
|
|
95
102
|
test('Tests for parseString', () => {
|
|
103
|
+
expect(Utils.parseString<string>('test')).toBe('test');
|
|
96
104
|
expect(Utils.parseString('test', '')).toBe('test');
|
|
97
105
|
expect(Utils.parseString('true', false)).toBe(true);
|
|
106
|
+
expect(Utils.parseString('', false)).toBeFalsy();
|
|
107
|
+
expect(Utils.parseString<boolean>('')).toBeUndefined();
|
|
108
|
+
expect(Utils.parseString<number>(undefined)).toBeUndefined();
|
|
98
109
|
expect(Utils.parseString('3.14', 0)).toBe(3.14);
|
|
99
110
|
expect(Utils.parseString('2021/4/13', new Date())).toStrictEqual(
|
|
100
111
|
new Date('2021/4/13')
|
|
@@ -20,7 +20,7 @@ export declare namespace StorageUtils {
|
|
|
20
20
|
* @param key Key name
|
|
21
21
|
* @param defaultValue Default value
|
|
22
22
|
*/
|
|
23
|
-
function getLocalData<T>(key: string, defaultValue: T
|
|
23
|
+
function getLocalData<T, M = T | undefined>(key: string, defaultValue?: M): M extends T ? M : undefined;
|
|
24
24
|
/**
|
|
25
25
|
* Get local storage object data
|
|
26
26
|
* @param key Key name
|
|
@@ -30,7 +30,7 @@ export declare namespace StorageUtils {
|
|
|
30
30
|
* Get session storage data
|
|
31
31
|
* @param key Key name
|
|
32
32
|
*/
|
|
33
|
-
function getSessionData<T>(key: string, defaultValue: T
|
|
33
|
+
function getSessionData<T, M = T | undefined>(key: string, defaultValue?: M): M extends T ? M : undefined;
|
|
34
34
|
/**
|
|
35
35
|
* Get session storage object data
|
|
36
36
|
* @param key Key name
|
package/lib/cjs/Utils.d.ts
CHANGED
|
@@ -118,12 +118,30 @@ export declare namespace Utils {
|
|
|
118
118
|
*/
|
|
119
119
|
function objectEqual(obj1: {}, obj2: {}, ignoreFields?: string[], strict?: number): boolean;
|
|
120
120
|
/**
|
|
121
|
-
*
|
|
121
|
+
* Get two object's unqiue properties
|
|
122
|
+
* @param obj1 Object 1
|
|
123
|
+
* @param obj2 Object 2
|
|
124
|
+
* @param ignoreFields Ignored fields
|
|
125
|
+
* @returns Unique properties
|
|
126
|
+
*/
|
|
127
|
+
function objectKeys(obj1: {}, obj2: {}, ignoreFields?: string[]): Set<string>;
|
|
128
|
+
/**
|
|
129
|
+
* Get the new object's updated fields contrast to the previous object
|
|
130
|
+
* @param objNew New object
|
|
131
|
+
* @param objPre Previous object
|
|
132
|
+
* @param ignoreFields Ignored fields
|
|
133
|
+
* @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
|
|
134
|
+
* @returns Updated fields
|
|
135
|
+
*/
|
|
136
|
+
function objectUpdated(objNew: {}, objPrev: {}, ignoreFields?: string[], strict?: number): string[];
|
|
137
|
+
/**
|
|
138
|
+
* Parse string (JSON) to specific type, no type conversion
|
|
139
|
+
* For type conversion, please use DataTypes.convert
|
|
122
140
|
* @param input Input string
|
|
123
141
|
* @param defaultValue Default value
|
|
124
142
|
* @returns Parsed value
|
|
125
143
|
*/
|
|
126
|
-
function parseString<T>(input: string | undefined | null, defaultValue: T
|
|
144
|
+
function parseString<T, M = T | undefined>(input: string | undefined | null, defaultValue?: M): M extends T ? M : undefined;
|
|
127
145
|
/**
|
|
128
146
|
* Remove non letters
|
|
129
147
|
* @param input Input string
|
package/lib/cjs/Utils.js
CHANGED
|
@@ -216,11 +216,8 @@ var Utils;
|
|
|
216
216
|
* @returns Result
|
|
217
217
|
*/
|
|
218
218
|
function objectEqual(obj1, obj2, ignoreFields = [], strict = 1) {
|
|
219
|
-
//
|
|
220
|
-
const keys =
|
|
221
|
-
...Object.keys(obj1).filter((item) => !ignoreFields.includes(item)),
|
|
222
|
-
...Object.keys(obj2).filter((item) => !ignoreFields.includes(item))
|
|
223
|
-
]);
|
|
219
|
+
// Unique keys
|
|
220
|
+
const keys = Utils.objectKeys(obj1, obj2, ignoreFields);
|
|
224
221
|
for (const key of keys) {
|
|
225
222
|
// Values
|
|
226
223
|
const v1 = Reflect.get(obj1, key);
|
|
@@ -232,14 +229,53 @@ var Utils;
|
|
|
232
229
|
}
|
|
233
230
|
Utils.objectEqual = objectEqual;
|
|
234
231
|
/**
|
|
235
|
-
*
|
|
232
|
+
* Get two object's unqiue properties
|
|
233
|
+
* @param obj1 Object 1
|
|
234
|
+
* @param obj2 Object 2
|
|
235
|
+
* @param ignoreFields Ignored fields
|
|
236
|
+
* @returns Unique properties
|
|
237
|
+
*/
|
|
238
|
+
function objectKeys(obj1, obj2, ignoreFields = []) {
|
|
239
|
+
// All keys
|
|
240
|
+
const allKeys = [...Object.keys(obj1), ...Object.keys(obj2)].filter((item) => !ignoreFields.includes(item));
|
|
241
|
+
// Unique keys
|
|
242
|
+
return new Set(allKeys);
|
|
243
|
+
}
|
|
244
|
+
Utils.objectKeys = objectKeys;
|
|
245
|
+
/**
|
|
246
|
+
* Get the new object's updated fields contrast to the previous object
|
|
247
|
+
* @param objNew New object
|
|
248
|
+
* @param objPre Previous object
|
|
249
|
+
* @param ignoreFields Ignored fields
|
|
250
|
+
* @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
|
|
251
|
+
* @returns Updated fields
|
|
252
|
+
*/
|
|
253
|
+
function objectUpdated(objNew, objPrev, ignoreFields = [], strict = 1) {
|
|
254
|
+
// Fields
|
|
255
|
+
const fields = [];
|
|
256
|
+
// Unique keys
|
|
257
|
+
const keys = Utils.objectKeys(objNew, objPrev, ignoreFields);
|
|
258
|
+
for (const key of keys) {
|
|
259
|
+
// Values
|
|
260
|
+
const vNew = Reflect.get(objNew, key);
|
|
261
|
+
const vPrev = Reflect.get(objPrev, key);
|
|
262
|
+
if (!Utils.equals(vNew, vPrev, strict)) {
|
|
263
|
+
fields.push(key);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return fields;
|
|
267
|
+
}
|
|
268
|
+
Utils.objectUpdated = objectUpdated;
|
|
269
|
+
/**
|
|
270
|
+
* Parse string (JSON) to specific type, no type conversion
|
|
271
|
+
* For type conversion, please use DataTypes.convert
|
|
236
272
|
* @param input Input string
|
|
237
273
|
* @param defaultValue Default value
|
|
238
274
|
* @returns Parsed value
|
|
239
275
|
*/
|
|
240
276
|
function parseString(input, defaultValue) {
|
|
241
|
-
// Undefined case, return default value
|
|
242
|
-
if (input == null)
|
|
277
|
+
// Undefined and empty case, return default value
|
|
278
|
+
if (input == null || input === '')
|
|
243
279
|
return defaultValue;
|
|
244
280
|
// String
|
|
245
281
|
if (typeof defaultValue === 'string')
|
|
@@ -257,16 +293,11 @@ var Utils;
|
|
|
257
293
|
// Return
|
|
258
294
|
return json;
|
|
259
295
|
}
|
|
260
|
-
catch
|
|
261
|
-
|
|
296
|
+
catch {
|
|
297
|
+
if (defaultValue == null)
|
|
298
|
+
return input;
|
|
262
299
|
return defaultValue;
|
|
263
300
|
}
|
|
264
|
-
/*
|
|
265
|
-
finally part will still return the boolean value
|
|
266
|
-
finally {
|
|
267
|
-
return defaultValue
|
|
268
|
-
}
|
|
269
|
-
*/
|
|
270
301
|
}
|
|
271
302
|
Utils.parseString = parseString;
|
|
272
303
|
/**
|
|
@@ -20,7 +20,7 @@ export declare namespace StorageUtils {
|
|
|
20
20
|
* @param key Key name
|
|
21
21
|
* @param defaultValue Default value
|
|
22
22
|
*/
|
|
23
|
-
function getLocalData<T>(key: string, defaultValue: T
|
|
23
|
+
function getLocalData<T, M = T | undefined>(key: string, defaultValue?: M): M extends T ? M : undefined;
|
|
24
24
|
/**
|
|
25
25
|
* Get local storage object data
|
|
26
26
|
* @param key Key name
|
|
@@ -30,7 +30,7 @@ export declare namespace StorageUtils {
|
|
|
30
30
|
* Get session storage data
|
|
31
31
|
* @param key Key name
|
|
32
32
|
*/
|
|
33
|
-
function getSessionData<T>(key: string, defaultValue: T
|
|
33
|
+
function getSessionData<T, M = T | undefined>(key: string, defaultValue?: M): M extends T ? M : undefined;
|
|
34
34
|
/**
|
|
35
35
|
* Get session storage object data
|
|
36
36
|
* @param key Key name
|
package/lib/mjs/Utils.d.ts
CHANGED
|
@@ -118,12 +118,30 @@ export declare namespace Utils {
|
|
|
118
118
|
*/
|
|
119
119
|
function objectEqual(obj1: {}, obj2: {}, ignoreFields?: string[], strict?: number): boolean;
|
|
120
120
|
/**
|
|
121
|
-
*
|
|
121
|
+
* Get two object's unqiue properties
|
|
122
|
+
* @param obj1 Object 1
|
|
123
|
+
* @param obj2 Object 2
|
|
124
|
+
* @param ignoreFields Ignored fields
|
|
125
|
+
* @returns Unique properties
|
|
126
|
+
*/
|
|
127
|
+
function objectKeys(obj1: {}, obj2: {}, ignoreFields?: string[]): Set<string>;
|
|
128
|
+
/**
|
|
129
|
+
* Get the new object's updated fields contrast to the previous object
|
|
130
|
+
* @param objNew New object
|
|
131
|
+
* @param objPre Previous object
|
|
132
|
+
* @param ignoreFields Ignored fields
|
|
133
|
+
* @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
|
|
134
|
+
* @returns Updated fields
|
|
135
|
+
*/
|
|
136
|
+
function objectUpdated(objNew: {}, objPrev: {}, ignoreFields?: string[], strict?: number): string[];
|
|
137
|
+
/**
|
|
138
|
+
* Parse string (JSON) to specific type, no type conversion
|
|
139
|
+
* For type conversion, please use DataTypes.convert
|
|
122
140
|
* @param input Input string
|
|
123
141
|
* @param defaultValue Default value
|
|
124
142
|
* @returns Parsed value
|
|
125
143
|
*/
|
|
126
|
-
function parseString<T>(input: string | undefined | null, defaultValue: T
|
|
144
|
+
function parseString<T, M = T | undefined>(input: string | undefined | null, defaultValue?: M): M extends T ? M : undefined;
|
|
127
145
|
/**
|
|
128
146
|
* Remove non letters
|
|
129
147
|
* @param input Input string
|
package/lib/mjs/Utils.js
CHANGED
|
@@ -213,11 +213,8 @@ export var Utils;
|
|
|
213
213
|
* @returns Result
|
|
214
214
|
*/
|
|
215
215
|
function objectEqual(obj1, obj2, ignoreFields = [], strict = 1) {
|
|
216
|
-
//
|
|
217
|
-
const keys =
|
|
218
|
-
...Object.keys(obj1).filter((item) => !ignoreFields.includes(item)),
|
|
219
|
-
...Object.keys(obj2).filter((item) => !ignoreFields.includes(item))
|
|
220
|
-
]);
|
|
216
|
+
// Unique keys
|
|
217
|
+
const keys = Utils.objectKeys(obj1, obj2, ignoreFields);
|
|
221
218
|
for (const key of keys) {
|
|
222
219
|
// Values
|
|
223
220
|
const v1 = Reflect.get(obj1, key);
|
|
@@ -229,14 +226,53 @@ export var Utils;
|
|
|
229
226
|
}
|
|
230
227
|
Utils.objectEqual = objectEqual;
|
|
231
228
|
/**
|
|
232
|
-
*
|
|
229
|
+
* Get two object's unqiue properties
|
|
230
|
+
* @param obj1 Object 1
|
|
231
|
+
* @param obj2 Object 2
|
|
232
|
+
* @param ignoreFields Ignored fields
|
|
233
|
+
* @returns Unique properties
|
|
234
|
+
*/
|
|
235
|
+
function objectKeys(obj1, obj2, ignoreFields = []) {
|
|
236
|
+
// All keys
|
|
237
|
+
const allKeys = [...Object.keys(obj1), ...Object.keys(obj2)].filter((item) => !ignoreFields.includes(item));
|
|
238
|
+
// Unique keys
|
|
239
|
+
return new Set(allKeys);
|
|
240
|
+
}
|
|
241
|
+
Utils.objectKeys = objectKeys;
|
|
242
|
+
/**
|
|
243
|
+
* Get the new object's updated fields contrast to the previous object
|
|
244
|
+
* @param objNew New object
|
|
245
|
+
* @param objPre Previous object
|
|
246
|
+
* @param ignoreFields Ignored fields
|
|
247
|
+
* @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
|
|
248
|
+
* @returns Updated fields
|
|
249
|
+
*/
|
|
250
|
+
function objectUpdated(objNew, objPrev, ignoreFields = [], strict = 1) {
|
|
251
|
+
// Fields
|
|
252
|
+
const fields = [];
|
|
253
|
+
// Unique keys
|
|
254
|
+
const keys = Utils.objectKeys(objNew, objPrev, ignoreFields);
|
|
255
|
+
for (const key of keys) {
|
|
256
|
+
// Values
|
|
257
|
+
const vNew = Reflect.get(objNew, key);
|
|
258
|
+
const vPrev = Reflect.get(objPrev, key);
|
|
259
|
+
if (!Utils.equals(vNew, vPrev, strict)) {
|
|
260
|
+
fields.push(key);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return fields;
|
|
264
|
+
}
|
|
265
|
+
Utils.objectUpdated = objectUpdated;
|
|
266
|
+
/**
|
|
267
|
+
* Parse string (JSON) to specific type, no type conversion
|
|
268
|
+
* For type conversion, please use DataTypes.convert
|
|
233
269
|
* @param input Input string
|
|
234
270
|
* @param defaultValue Default value
|
|
235
271
|
* @returns Parsed value
|
|
236
272
|
*/
|
|
237
273
|
function parseString(input, defaultValue) {
|
|
238
|
-
// Undefined case, return default value
|
|
239
|
-
if (input == null)
|
|
274
|
+
// Undefined and empty case, return default value
|
|
275
|
+
if (input == null || input === '')
|
|
240
276
|
return defaultValue;
|
|
241
277
|
// String
|
|
242
278
|
if (typeof defaultValue === 'string')
|
|
@@ -254,16 +290,11 @@ export var Utils;
|
|
|
254
290
|
// Return
|
|
255
291
|
return json;
|
|
256
292
|
}
|
|
257
|
-
catch
|
|
258
|
-
|
|
293
|
+
catch {
|
|
294
|
+
if (defaultValue == null)
|
|
295
|
+
return input;
|
|
259
296
|
return defaultValue;
|
|
260
297
|
}
|
|
261
|
-
/*
|
|
262
|
-
finally part will still return the boolean value
|
|
263
|
-
finally {
|
|
264
|
-
return defaultValue
|
|
265
|
-
}
|
|
266
|
-
*/
|
|
267
298
|
}
|
|
268
299
|
Utils.parseString = parseString;
|
|
269
300
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/shared",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.81",
|
|
4
4
|
"description": "TypeScript shared utilities and functions",
|
|
5
5
|
"main": "lib/cjs/index.js",
|
|
6
6
|
"module": "lib/mjs/index.js",
|
|
@@ -55,13 +55,13 @@
|
|
|
55
55
|
"dependencies": {},
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"@types/jest": "^27.0.3",
|
|
58
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
59
|
-
"@typescript-eslint/parser": "^5.
|
|
60
|
-
"eslint": "^8.
|
|
58
|
+
"@typescript-eslint/eslint-plugin": "^5.8.0",
|
|
59
|
+
"@typescript-eslint/parser": "^5.8.0",
|
|
60
|
+
"eslint": "^8.5.0",
|
|
61
61
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
62
62
|
"eslint-plugin-import": "^2.25.3",
|
|
63
|
-
"jest": "^27.
|
|
64
|
-
"ts-jest": "^27.
|
|
65
|
-
"typescript": "^4.5.
|
|
63
|
+
"jest": "^27.4.5",
|
|
64
|
+
"ts-jest": "^27.1.2",
|
|
65
|
+
"typescript": "^4.5.4"
|
|
66
66
|
}
|
|
67
67
|
}
|
package/src/StorageUtils.ts
CHANGED
|
@@ -49,12 +49,15 @@ export namespace StorageUtils {
|
|
|
49
49
|
* @param key Key name
|
|
50
50
|
* @param defaultValue Default value
|
|
51
51
|
*/
|
|
52
|
-
export function getLocalData<T
|
|
52
|
+
export function getLocalData<T, M = T | undefined>(
|
|
53
|
+
key: string,
|
|
54
|
+
defaultValue?: M
|
|
55
|
+
): M extends T ? M : undefined {
|
|
53
56
|
// Get storage
|
|
54
57
|
const data = localStorage.getItem(key);
|
|
55
58
|
|
|
56
59
|
// Return
|
|
57
|
-
return Utils.parseString(data, defaultValue);
|
|
60
|
+
return Utils.parseString<T, M>(data, defaultValue);
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
/**
|
|
@@ -72,12 +75,15 @@ export namespace StorageUtils {
|
|
|
72
75
|
* Get session storage data
|
|
73
76
|
* @param key Key name
|
|
74
77
|
*/
|
|
75
|
-
export function getSessionData<T
|
|
78
|
+
export function getSessionData<T, M = T | undefined>(
|
|
79
|
+
key: string,
|
|
80
|
+
defaultValue?: M
|
|
81
|
+
): M extends T ? M : undefined {
|
|
76
82
|
// Get storage
|
|
77
83
|
const data = sessionStorage.getItem(key);
|
|
78
84
|
|
|
79
85
|
// Return
|
|
80
|
-
return Utils.parseString(data, defaultValue);
|
|
86
|
+
return Utils.parseString<T, M>(data, defaultValue);
|
|
81
87
|
}
|
|
82
88
|
|
|
83
89
|
/**
|
package/src/Utils.ts
CHANGED
|
@@ -293,11 +293,8 @@ export namespace Utils {
|
|
|
293
293
|
ignoreFields: string[] = [],
|
|
294
294
|
strict = 1
|
|
295
295
|
) {
|
|
296
|
-
//
|
|
297
|
-
const keys =
|
|
298
|
-
...Object.keys(obj1).filter((item) => !ignoreFields.includes(item)),
|
|
299
|
-
...Object.keys(obj2).filter((item) => !ignoreFields.includes(item))
|
|
300
|
-
]);
|
|
296
|
+
// Unique keys
|
|
297
|
+
const keys = Utils.objectKeys(obj1, obj2, ignoreFields);
|
|
301
298
|
|
|
302
299
|
for (const key of keys) {
|
|
303
300
|
// Values
|
|
@@ -311,17 +308,72 @@ export namespace Utils {
|
|
|
311
308
|
}
|
|
312
309
|
|
|
313
310
|
/**
|
|
314
|
-
*
|
|
311
|
+
* Get two object's unqiue properties
|
|
312
|
+
* @param obj1 Object 1
|
|
313
|
+
* @param obj2 Object 2
|
|
314
|
+
* @param ignoreFields Ignored fields
|
|
315
|
+
* @returns Unique properties
|
|
316
|
+
*/
|
|
317
|
+
export function objectKeys(
|
|
318
|
+
obj1: {},
|
|
319
|
+
obj2: {},
|
|
320
|
+
ignoreFields: string[] = []
|
|
321
|
+
) {
|
|
322
|
+
// All keys
|
|
323
|
+
const allKeys = [...Object.keys(obj1), ...Object.keys(obj2)].filter(
|
|
324
|
+
(item) => !ignoreFields.includes(item)
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
// Unique keys
|
|
328
|
+
return new Set(allKeys);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Get the new object's updated fields contrast to the previous object
|
|
333
|
+
* @param objNew New object
|
|
334
|
+
* @param objPre Previous object
|
|
335
|
+
* @param ignoreFields Ignored fields
|
|
336
|
+
* @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
|
|
337
|
+
* @returns Updated fields
|
|
338
|
+
*/
|
|
339
|
+
export function objectUpdated(
|
|
340
|
+
objNew: {},
|
|
341
|
+
objPrev: {},
|
|
342
|
+
ignoreFields: string[] = [],
|
|
343
|
+
strict = 1
|
|
344
|
+
) {
|
|
345
|
+
// Fields
|
|
346
|
+
const fields: string[] = [];
|
|
347
|
+
|
|
348
|
+
// Unique keys
|
|
349
|
+
const keys = Utils.objectKeys(objNew, objPrev, ignoreFields);
|
|
350
|
+
|
|
351
|
+
for (const key of keys) {
|
|
352
|
+
// Values
|
|
353
|
+
const vNew = Reflect.get(objNew, key);
|
|
354
|
+
const vPrev = Reflect.get(objPrev, key);
|
|
355
|
+
|
|
356
|
+
if (!Utils.equals(vNew, vPrev, strict)) {
|
|
357
|
+
fields.push(key);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return fields;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Parse string (JSON) to specific type, no type conversion
|
|
366
|
+
* For type conversion, please use DataTypes.convert
|
|
315
367
|
* @param input Input string
|
|
316
368
|
* @param defaultValue Default value
|
|
317
369
|
* @returns Parsed value
|
|
318
370
|
*/
|
|
319
|
-
export function parseString<T>(
|
|
371
|
+
export function parseString<T, M = T | undefined>(
|
|
320
372
|
input: string | undefined | null,
|
|
321
|
-
defaultValue
|
|
322
|
-
): T {
|
|
323
|
-
// Undefined case, return default value
|
|
324
|
-
if (input == null) return defaultValue;
|
|
373
|
+
defaultValue?: M
|
|
374
|
+
): M extends T ? M : undefined {
|
|
375
|
+
// Undefined and empty case, return default value
|
|
376
|
+
if (input == null || input === '') return <any>defaultValue;
|
|
325
377
|
|
|
326
378
|
// String
|
|
327
379
|
if (typeof defaultValue === 'string') return <any>input;
|
|
@@ -330,7 +382,7 @@ export namespace Utils {
|
|
|
330
382
|
// Date
|
|
331
383
|
if (defaultValue instanceof Date) {
|
|
332
384
|
const date = new Date(input);
|
|
333
|
-
if (date == null) return defaultValue;
|
|
385
|
+
if (date == null) return <any>defaultValue;
|
|
334
386
|
return <any>date;
|
|
335
387
|
}
|
|
336
388
|
|
|
@@ -338,17 +390,11 @@ export namespace Utils {
|
|
|
338
390
|
const json = JSON.parse(input);
|
|
339
391
|
|
|
340
392
|
// Return
|
|
341
|
-
return
|
|
342
|
-
} catch
|
|
343
|
-
|
|
344
|
-
return defaultValue;
|
|
345
|
-
}
|
|
346
|
-
/*
|
|
347
|
-
finally part will still return the boolean value
|
|
348
|
-
finally {
|
|
349
|
-
return defaultValue
|
|
393
|
+
return json;
|
|
394
|
+
} catch {
|
|
395
|
+
if (defaultValue == null) return <any>input;
|
|
396
|
+
return <any>defaultValue;
|
|
350
397
|
}
|
|
351
|
-
*/
|
|
352
398
|
}
|
|
353
399
|
|
|
354
400
|
/**
|