分离工具函数

This commit is contained in:
Harvey committed 2019-11-14 11:24:02 +08:00
1 parent 7a84c262da
commit 8ef582f714
22 files changed
+724 -687

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
import { errorHandle } from './error';
export function hasOwnProperty(obj, name) {
return Object.prototype.hasOwnProperty.call(obj, name);
}
export function proxyPropertys(target, ...sources) {
return sources.reduce((result, source) => {
Object.getOwnPropertyNames(source).forEach(key => {
errorHandle(!hasOwnProperty(result, key), `Target attribute name is duplicated: ${key}`);
Object.defineProperty(result, key, Object.getOwnPropertyDescriptor(source, key));
});
return result;
}, target);
}
export function mergeDeep(...objects) {
const isObject = item => item && typeof item === 'object' && !Array.isArray(item);
return objects.reduce((prev, obj) => {
Object.keys(obj).forEach(key => {
const pVal = prev[key];
const oVal = obj[key];
if (Array.isArray(pVal) && Array.isArray(oVal)) {
prev[key] = pVal.concat(...oVal);
} else if (isObject(pVal) && isObject(oVal) && !(oVal instanceof Element)) {
prev[key] = mergeDeep(pVal, oVal);
} else {
prev[key] = oVal;
}
});
return prev;
}, {});
}