-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject-util.js
147 lines (130 loc) · 3.69 KB
/
object-util.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"use strict";
/**
* Check whether it is an empty object
* @param {*} o, The object needed to be verified
* @returns {boolean}, The result of the empty verification
*/
function isEmpty(o) {
if (o === null || o === undefined || o === '' || typeof o === 'undefined') {
return true;
}
const objectType = getObjectType(o);
switch (objectType) {
case 'object':
return Object.keys(o).length === 0;
case 'array':
return o.length === 0;
case 'string':
return o.trim().length === 0;
case 'map':
console.log(o);
return o.size === 0;
case 'htmlElement':
return typeof o.click === 'undefined';
default:
return false;
}
}
/*
* use to obtain the type of the object
* @param {*} o, The object needed to be verified
* @returns {string} object, array, number, string, undefined, boolean, date, map, htmlElement
* */
function getObjectType(o) {
if (Array.isArray(o)) return 'array';
else if (o instanceof Map) return 'map';
else if (o instanceof Date) return 'date';
// else if (o instanceof HTMLElement) return 'htmlElement';
return typeof o;
}
/*
* get the empty value of a particular object type
* */
function getEmptyValue(objectType) {
switch (objectType) {
case 'string':
return '';
case 'object':
return {};
case 'array':
return [];
case 'number':
return NaN;
case 'date':
return new Date();
case 'map':
return new Map();
case 'htmlElement':
return new HTMLElement();
default:
return null;
}
}
function getValueFromObject(o, t) {
if (isEmpty(o) || isEmpty(t) || getObjectType(o) !== 'object') return null;
const keyArr = t.split('.');
const firstKey = keyArr.splice(0, 1); // remove the first key from the array
if (isEmpty(keyArr)) return o[firstKey.toString()];
return getValueFromObject(o[firstKey.toString()], keyArr.join('.'));
}
// get a deep copy of an object
function clone(o) {
if (isEmpty(o) || typeof o !== 'object') return o;
if (o instanceof Date) {
return new Date(o);
}
if (o instanceof Map) {
return new Map(o);
}
if (o instanceof Array) {
return JSON.parse(JSON.stringify(o));
}
if (o instanceof Object) {
const copy = o.constructor();
for (let attr in o) {
if (o.hasOwnProperty(attr)) copy[attr] = o[attr];
}
return copy;
}
throw new Error("Unable to copy obj! Its type isn't supported.");
}
// check whether the two object are the same or not
function isTheSameObject(o1, o2, skipFields = []) {
if ((!o1 && o2) || (o1 && !o2)) {
return false;
} else if (typeof o1 !== typeof o2) {
return false;
} else if (typeof o1 === 'object' && Array.isArray(o1) !== Array.isArray(o2)) {
return false;
}
if (typeof o1 === 'string') {
return o1.toLowerCase().trim() === o2.toLowerCase().trim();
} else if (Array.isArray(o1)) {
return o1.every(item => o2.includes(item)) && o1.length === o2.length;
} else if (typeof o1 === 'object') {
return checkIfSameObject();
} else {
return o1 === o2;
}
// inner function to specifically check object
function checkIfSameObject() {
if (Object.keys(o1).length !== Object.keys(o2).length) {
return false;
}
if (Object.keys(o1).some(o1Key => !Object.keys(o2).includes(o1Key))) {
return false;
}
return Object.keys(o1)
.filter(key => !skipFields.includes(key))
.every(o1Key => {
const o1Value = o1[o1Key];
return isTheSameObject(o1Value, o2[o1Key]);
});
}
}
exports.clone = clone;
exports.isEmpty = isEmpty;
exports.getObjectType = getObjectType;
exports.getEmptyValue = getEmptyValue;
exports.isTheSameObject = isTheSameObject;
exports.getValueFromObject = getValueFromObject;