-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobj.js
56 lines (52 loc) · 1.58 KB
/
obj.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
export class ObjectUtils {
/**
* Creates a deep copy of the provided object.
* @param {Object} obj - The object to be cloned.
* @returns {Object} The deep clone of the object.
*/
static deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
/**
* Merges two objects into a new object.
* @param {Object} obj1 - The first object to merge.
* @param {Object} obj2 - The second object to merge.
* @returns {Object} The merged object.
*/
static mergeObjects(obj1, obj2) {
return { ...obj1, ...obj2 };
}
/**
* Merges multiple objects into a new object.
* @param {...any} objects - The array of objects to merge.
* @returns {Object} The merged object.
*/
static mergeManyObjects(...objects) {
return objects.reduce((acc, obj) => ObjectUtils.mergeObjects(acc, obj), {});
}
/**
* Checks whether a variable is an object and not an array.
* @param {*} item - The variable to be checked.
* @returns {Boolean} True if the item is an object, false otherwise.
*/
static isObject(item) {
return item && typeof item === 'object' && !Array.isArray(item);
}
/**
* Allows you to access nested properties of an object using a dot-separated path.
* @param {Object} obj - The object to access.
* @param {String} path - The dot-separated path of the property.
* @returns {*} The value of the accessed property, or undefined if not found.
*/
static accessObject(obj, path) {
const keys = path.split('.');
let current = obj;
for (let key of keys) {
if (current[key] === undefined) {
return undefined;
}
current = current[key];
}
return current;
}
}