@etsoo/shared 1.0.77 → 1.0.78
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__/Utils.ts +7 -0
- package/lib/cjs/Utils.d.ts +17 -0
- package/lib/cjs/Utils.js +40 -5
- package/lib/mjs/Utils.d.ts +17 -0
- package/lib/mjs/Utils.js +40 -5
- package/package.json +1 -1
- package/src/Utils.ts +56 -5
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'|
|
package/__tests__/Utils.ts
CHANGED
|
@@ -92,6 +92,13 @@ 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', () => {
|
|
96
103
|
expect(Utils.parseString('test', '')).toBe('test');
|
|
97
104
|
expect(Utils.parseString('true', false)).toBe(true);
|
package/lib/cjs/Utils.d.ts
CHANGED
|
@@ -117,6 +117,23 @@ export declare namespace Utils {
|
|
|
117
117
|
* @returns Result
|
|
118
118
|
*/
|
|
119
119
|
function objectEqual(obj1: {}, obj2: {}, ignoreFields?: string[], strict?: number): boolean;
|
|
120
|
+
/**
|
|
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[];
|
|
120
137
|
/**
|
|
121
138
|
* Parse string (JSON) to specific type
|
|
122
139
|
* @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);
|
|
@@ -231,6 +228,44 @@ var Utils;
|
|
|
231
228
|
return true;
|
|
232
229
|
}
|
|
233
230
|
Utils.objectEqual = objectEqual;
|
|
231
|
+
/**
|
|
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;
|
|
234
269
|
/**
|
|
235
270
|
* Parse string (JSON) to specific type
|
|
236
271
|
* @param input Input string
|
package/lib/mjs/Utils.d.ts
CHANGED
|
@@ -117,6 +117,23 @@ export declare namespace Utils {
|
|
|
117
117
|
* @returns Result
|
|
118
118
|
*/
|
|
119
119
|
function objectEqual(obj1: {}, obj2: {}, ignoreFields?: string[], strict?: number): boolean;
|
|
120
|
+
/**
|
|
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[];
|
|
120
137
|
/**
|
|
121
138
|
* Parse string (JSON) to specific type
|
|
122
139
|
* @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);
|
|
@@ -228,6 +225,44 @@ export var Utils;
|
|
|
228
225
|
return true;
|
|
229
226
|
}
|
|
230
227
|
Utils.objectEqual = objectEqual;
|
|
228
|
+
/**
|
|
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;
|
|
231
266
|
/**
|
|
232
267
|
* Parse string (JSON) to specific type
|
|
233
268
|
* @param input Input string
|
package/package.json
CHANGED
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
|
|
@@ -310,6 +307,60 @@ export namespace Utils {
|
|
|
310
307
|
return true;
|
|
311
308
|
}
|
|
312
309
|
|
|
310
|
+
/**
|
|
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
|
+
|
|
313
364
|
/**
|
|
314
365
|
* Parse string (JSON) to specific type
|
|
315
366
|
* @param input Input string
|