unique-interface
Version:
无UI组件库
63 lines (58 loc) • 1.62 kB
JavaScript
/**
* @file 工具包
*
* @export isUndef {function}
*
* @author soulran(xr@ethercap.com)
*/
// type
const _class2type = {}, _toString = Object.prototype.toString;
"Boolean Number String Function Array Date RegExp Object Error Symbol".split( " " ).forEach(function(name){
_class2type["[object " + name + "]"] = name.toLowerCase();
});
export function type(val){
return typeof val === "object" || typeof val === "function" ?
_class2type[ _toString.call(val) ] || "object" :
typeof val;
}
/**
* Convert an Array-like object to a real Array.
*/
export function toArray(list, start){
start = start || 0;
var i = list.length - start;
var ret = new Array(i);
while (i--) {
ret[i] = list[i + start];
}
return ret;
}
/**
* Merge several object/array to a target object/array.
*/
export function merge(target){
toArray(arguments, 1).forEach(function(source){
Object.keys(source).forEach(key => { target[key] = source[key] });
});
return target;
}
/**
* Deep merge several object/array to a target object/array.
*/
export function deepMerge(target){
var key, stype;
toArray(arguments, 1).forEach(function(source){
for(key in source){
stype = type(source[key]);
if(stype === 'array' || stype === 'object'){
if(type(target[key]) !== stype){
target[key] = stype === 'array' ? [] : {};
}
deepMerge(target[key], source[key]);
} else {
target[key] = source[key];
}
}
});
return target;
}