2022-01-08 17:20:46 +08:00
|
|
|
|
const toString = Object.prototype.toString
|
|
|
|
|
|
|
|
|
|
export function is(val, type) {
|
|
|
|
|
return toString.call(val) === `[object ${type}]`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isDef(val) {
|
|
|
|
|
return typeof val !== 'undefined'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isUndef(val) {
|
|
|
|
|
return typeof val === 'undefined'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isNull(val) {
|
|
|
|
|
return val === null
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-16 18:29:32 +08:00
|
|
|
|
export function isWhitespace(val) {
|
|
|
|
|
return val === ''
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-08 17:20:46 +08:00
|
|
|
|
export function isObject(val) {
|
2022-01-25 15:21:03 +08:00
|
|
|
|
return !isNull(val) && is(val, 'Object')
|
2022-01-08 17:20:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isArray(val) {
|
|
|
|
|
return val && Array.isArray(val)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isString(val) {
|
|
|
|
|
return is(val, 'String')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isNumber(val) {
|
|
|
|
|
return is(val, 'Number')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isBoolean(val) {
|
|
|
|
|
return is(val, 'Boolean')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isDate(val) {
|
|
|
|
|
return is(val, 'Date')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isRegExp(val) {
|
|
|
|
|
return is(val, 'RegExp')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isFunction(val) {
|
|
|
|
|
return typeof val === 'function'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isPromise(val) {
|
|
|
|
|
return is(val, 'Promise') && isObject(val) && isFunction(val.then) && isFunction(val.catch)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isElement(val) {
|
|
|
|
|
return isObject(val) && !!val.tagName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isWindow(val) {
|
|
|
|
|
return typeof window !== 'undefined' && isDef(window) && is(val, 'Window')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isNullOrUndef(val) {
|
|
|
|
|
return isNull(val) || isUndef(val)
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-16 18:29:32 +08:00
|
|
|
|
export function isNullOrWhitespace(val) {
|
|
|
|
|
return isNullOrUndef(val) || isWhitespace(val)
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-08 17:20:46 +08:00
|
|
|
|
export function isEmpty(val) {
|
|
|
|
|
if (isArray(val) || isString(val)) {
|
|
|
|
|
return val.length === 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (val instanceof Map || val instanceof Set) {
|
|
|
|
|
return val.size === 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isObject(val)) {
|
|
|
|
|
return Object.keys(val).length === 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-25 15:21:03 +08:00
|
|
|
|
/**
|
2022-05-16 18:29:32 +08:00
|
|
|
|
* * 类似mysql的IFNULL函数
|
|
|
|
|
* * 第一个参数为null/undefined/'' 则返回第二个参数作为备用值,否则返回第一个参数
|
2022-01-25 15:21:03 +08:00
|
|
|
|
* @param {Number|Boolean|String} val
|
2022-05-16 18:29:32 +08:00
|
|
|
|
* @param {Number|Boolean|String} def
|
2022-01-25 15:21:03 +08:00
|
|
|
|
* @returns
|
|
|
|
|
*/
|
2022-05-16 18:29:32 +08:00
|
|
|
|
export function ifNull(val, def = '') {
|
|
|
|
|
return isNullOrWhitespace(val) ? def : val
|
2022-01-25 15:21:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-08 17:20:46 +08:00
|
|
|
|
export function isUrl(path) {
|
|
|
|
|
const reg =
|
|
|
|
|
/(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/
|
|
|
|
|
return reg.test(path)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 14:29:06 +08:00
|
|
|
|
/**
|
|
|
|
|
* @param {string} path
|
|
|
|
|
* @returns {Boolean}
|
|
|
|
|
*/
|
|
|
|
|
export function isExternal(path) {
|
|
|
|
|
return /^(https?:|mailto:|tel:)/.test(path)
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-08 17:20:46 +08:00
|
|
|
|
export const isServer = typeof window === 'undefined'
|
|
|
|
|
|
|
|
|
|
export const isClient = !isServer
|