type.js
export function getType(target) {
return Object.prototype.toString.call(target).slice(8, -1)
}
export function isObject(target) {
return getType(target) === 'Object'
}
export function isArray(target) {
return getType(target) === 'Array'
}
export function isString(target) {
return getType(target) === 'String'
}
export function isFunction(target) {
return getType(target) === 'Function'
}
/**
* 将substr从str中移除, 并返回新的str
* @param {*} str
* @param {*} substr
* @returns
*/
export function removeSubstrFromStr(str, substr) {
if (!isString(str)) throw new Error('参数必须为字符串')
const index = str.indexOf(substr)
if (index >= 0) str = str.slice(0, index)
return str
}
/**
* 将subobj从arr中移除
* @param {*} arr
* @param {*} target
* @param {*} key
* @returns
*/
export function removeSubItemFromArr(arr, target, key) {
if (!isArray(arr)) throw new Error('参数必须为数组')
const index = arr.findIndex(item => {
if (isObject(item)) {
return item[key] === isObject(target) ? target[key] : target
} else if (isNumber(item) || isString(item)) {
return item === target
}
return false
})
index > -1 && arr.splice(index, 1)
浙公网安备 33010602011771号