|
|
|
|
@ -1,41 +1,77 @@ |
|
|
|
|
/** |
|
|
|
|
* 判断是否为空值,undefined、null、'' 都视为空值 |
|
|
|
|
* |
|
|
|
|
* @param str 不定参数 |
|
|
|
|
* @return {boolean} 若为空值,返回true,否则返回false |
|
|
|
|
*/ |
|
|
|
|
export function isEmpty(...str) { |
|
|
|
|
return str.some(i => i === undefined || i === null || i === '') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 当传入空值时,返回默认值 |
|
|
|
|
* |
|
|
|
|
* @param v 传入值 |
|
|
|
|
* @param defaultValue 当传入值为空值(使用{@link #isEmpty}判断)时,返回的值 |
|
|
|
|
* @return {string|*} |
|
|
|
|
*/ |
|
|
|
|
export function emptyOrDefault(v, defaultValue = '') { |
|
|
|
|
return isEmpty(v) ? defaultValue : v |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 字符串替换全部 |
|
|
|
|
* |
|
|
|
|
* @param str 原字符串 |
|
|
|
|
* @param substr 被替换的字符串 |
|
|
|
|
* @param replacement 替换的字符串 |
|
|
|
|
* @return {string} |
|
|
|
|
*/ |
|
|
|
|
export function replaceAll(str, substr, replacement) { |
|
|
|
|
if (str == null) return str |
|
|
|
|
|
|
|
|
|
return str.replace(new RegExp(substr, 'gm'), replacement) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 根据传入值的类型,返回基础起始值 |
|
|
|
|
* |
|
|
|
|
* @param v |
|
|
|
|
* @return {boolean|{}|string|*[]|number|null} |
|
|
|
|
*/ |
|
|
|
|
export function getInitialValue(v) { |
|
|
|
|
if (v === undefined || v === null) v = null |
|
|
|
|
else if (typeof v === 'string') v = '' |
|
|
|
|
else if (typeof v === 'boolean') v = false |
|
|
|
|
else if (typeof v === 'number') v = 0 |
|
|
|
|
else if (typeof v === 'object') v = {} |
|
|
|
|
else if (Array.isArray(v)) v = [] |
|
|
|
|
if (v === undefined || v === null) return null |
|
|
|
|
if (typeof v === 'string') return '' |
|
|
|
|
if (typeof v === 'boolean') return false |
|
|
|
|
if (typeof v === 'number') return 0 |
|
|
|
|
if (typeof v === 'object') return {} |
|
|
|
|
if (Array.isArray(v)) return [] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//简单重置对象属性
|
|
|
|
|
/** |
|
|
|
|
* 简单重置对象属性,遇到对象时会递归重置 |
|
|
|
|
* 重置方法使用{@link #getInitialValue} |
|
|
|
|
* |
|
|
|
|
* @param obj 需要重置的对象 |
|
|
|
|
*/ |
|
|
|
|
export function resetObj(obj) { |
|
|
|
|
if (isEmpty(obj)) return |
|
|
|
|
Object.keys(obj).forEach(key => { |
|
|
|
|
if (Array.isArray(obj[key])) { |
|
|
|
|
obj[key] = [] |
|
|
|
|
} |
|
|
|
|
else if (typeof obj[key] === 'number') { |
|
|
|
|
obj[key] = 0 |
|
|
|
|
} |
|
|
|
|
else if (typeof obj[key] === 'boolean') { |
|
|
|
|
obj[key] = false |
|
|
|
|
} |
|
|
|
|
else if (obj[key] !== null && typeof obj[key] === 'object') { |
|
|
|
|
if (obj[key] !== null && typeof obj[key] === 'object') { |
|
|
|
|
resetObj(obj[key]) |
|
|
|
|
} |
|
|
|
|
else obj[key] = null |
|
|
|
|
else obj[key] = getInitialValue(obj[key]) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//简单合并对象
|
|
|
|
|
/** |
|
|
|
|
* 将source合并到target中 |
|
|
|
|
* 仅对target中存在的键进行合并 |
|
|
|
|
* 仅当遇到对象值时进行递归 |
|
|
|
|
* |
|
|
|
|
* @param target |
|
|
|
|
* @param source |
|
|
|
|
*/ |
|
|
|
|
export function mergeObj(target, source) { |
|
|
|
|
if (isEmpty(target, source)) return |
|
|
|
|
|
|
|
|
|
@ -64,6 +100,13 @@ export function mergeObj(target, source) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 日期格式化 |
|
|
|
|
* |
|
|
|
|
* @param fmt {string} 格式,y(+..y)-年、M(+M)-月、d(+d)-天、H(+H)-时、m(+m)-分、s(+s)-秒、S-毫秒 |
|
|
|
|
* @param date {Date} 可选,被格式化的日期 |
|
|
|
|
* @return {string} 格式化后的日期字符串 |
|
|
|
|
*/ |
|
|
|
|
export function timeFormat(fmt, date = new Date()) { |
|
|
|
|
if (isEmpty(fmt)) fmt = 'yyyy-MM-dd HH:mm:ss' |
|
|
|
|
|
|
|
|
|
@ -73,7 +116,6 @@ export function timeFormat(fmt, date = new Date()) { |
|
|
|
|
"H+": date.getHours(), //小时
|
|
|
|
|
"m+": date.getMinutes(), //分
|
|
|
|
|
"s+": date.getSeconds(), //秒
|
|
|
|
|
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
|
|
|
|
|
"S": date.getMilliseconds() //毫秒
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -85,7 +127,7 @@ export function timeFormat(fmt, date = new Date()) { |
|
|
|
|
for (const k in o) { |
|
|
|
|
if (new RegExp(`(${k})`).test(fmt)) { |
|
|
|
|
const firstMatch = RegExp.$1 |
|
|
|
|
const replace = firstMatch.length === 1 ? o[k] : ("00" + o[k]).substring(("" + o[k]).length) |
|
|
|
|
const replace = firstMatch.length === 1 ? o[k] + "" : ("00" + o[k]).substring(("" + o[k]).length) |
|
|
|
|
fmt = fmt.replace(firstMatch, [...replace].join('')) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -93,6 +135,14 @@ export function timeFormat(fmt, date = new Date()) { |
|
|
|
|
return fmt |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 防抖 |
|
|
|
|
* |
|
|
|
|
* @param func {function} 原函数 |
|
|
|
|
* @param wait {number} 防抖间隔,单位毫秒 |
|
|
|
|
* @param immediate {boolean} 是否立即执行一次 |
|
|
|
|
* @return {function} 经过防抖包装后的函数 |
|
|
|
|
*/ |
|
|
|
|
export function debounce(func, wait = 100, immediate = false) { |
|
|
|
|
let timeout, args, context, timestamp, result |
|
|
|
|
|
|
|
|
|
@ -183,15 +233,6 @@ export function waitUntilSuccess(success, callback, interval = 1000, maxTryTime |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//删除所有url参数
|
|
|
|
|
export function delAllUrlParam() { |
|
|
|
|
let paramStartIndex = location.href.indexOf('?') |
|
|
|
|
if (paramStartIndex > -1) { |
|
|
|
|
const href = location.href.substring(0, paramStartIndex) |
|
|
|
|
history.replaceState(null, null, [...href].join('')) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//将传入对象的所有函数的this绑定为其自身
|
|
|
|
|
export function bindThis(obj, root = obj) { |
|
|
|
|
if (!obj || typeof obj !== 'object') return |
|
|
|
|
|