front-standard-utils
Version:
33 lines (32 loc) • 793 B
text/typescript
/**
*
* @desc 判断`obj`是否为空
* @param {Object} obj
* @return {Boolean}
*/
export const isEmptyObject = (obj: any) => {
if (!obj || typeof obj !== 'object' || Array.isArray(obj))
return false
return !Object.keys(obj).length
}
/**
* @desc obj深克隆
* @param {Object} obj
* @return {obj}
*/
export function deepClone(obj: any) {
const type = Object.prototype.toString.call(obj).slice(8, -1); // [object Array] => Array
switch (type) {
case 'Array':
return obj.map((o: any) => deepClone(o));
case 'Object':
return Object.keys(obj).reduce((sum: any, curr) => {
sum[curr] = deepClone(obj[curr]);
return sum;
}, {});
case 'Date':
return new obj.constructor(Number(obj));
default:
return obj;
}
}