// 输入目标, 返回目标具体类型
/**
* @param {any} target 要判断类型的目标对象
* @returns 返回相应类型
*/
export const targetType = (target) => {
let result = ''
switch (Object.prototype.toString.call(target)) {
case '[object Object]':
result = 'Object'
break
case '[object Array]':
result = 'Array'
break
case '[object String]':
result = 'String'
break
case '[object Number]':
result = 'Number'
break
case '[object Boolean]':
result = 'Boolean'
break
case '[object Function]':
result = 'Function'
break
case '[object Null]':
result = 'Null'
break
case '[object Undefined]':
result = 'Undefined'
break
}
return result
}
// 检查目标是否有效( 空数组以及空对象被认为不合法 )
export const checkIsValid = (item) => {
switch (typeof (item)) {
case 'undefined': return false
case 'null': return false
case 'number': {
if (isNaN(item)) {
return false
} else {
return true
}
}
case 'object': {
for (let key in item) {
return true
}
return false
}
}
}
// 驼峰与下划线写法相互转换
/**
* @function 驼峰命名->下划线命名
* @param {string} name {驼峰名称}
* @return {string} {下划线名称}
*/
export const toLineName = (name) => {
return name && name.replace(/([A-Z])/g, '_$1').toLowerCase()
}
/**
* @function 下划线命名->驼峰命名
* @param {string} name {下划线名称}
* @return {string} {驼峰名称}
*/
export const toHumpName = (name) => {
return name && name.replace(/_([a-zA-Z])/g, function (all, letter) {
return letter.toUpperCase()
})
}
// 合并对象
/**
* Mix properties into target object.
*/
export function extend (to, _from) {
for (const key in _from) {
to[key] = _from[key]
}
return to
}
// 类数组转为真数组
/**
* Convert an Array-like object to a real Array.
*/
export function toArray (list, start) {
start = start || 0
let i = list.length - start
const ret = new Array(i)
while (i--) {
ret[i] = list[i + start]
}
return ret
}
转载于:https://www.cnblogs.com/a-xi-ba/p/7874500.html