2023-07-20 13:45:41 +08:00
|
|
|
/**
|
|
|
|
|
* 限制数字大小
|
|
|
|
|
* @param {*} num
|
|
|
|
|
* @param {*} min
|
|
|
|
|
* @param {*} max
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const limitNumber = (num, min = 0, max = 100) => {
|
|
|
|
|
const number = parseInt(num);
|
|
|
|
|
if (Number.isNaN(number) || number < min) {
|
|
|
|
|
return min;
|
|
|
|
|
} else if (number > max) {
|
|
|
|
|
return max;
|
|
|
|
|
}
|
|
|
|
|
return number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 匹配是否为数组中的值
|
|
|
|
|
* @param {*} arr
|
|
|
|
|
* @param {*} val
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const matchValue = (arr, val) => {
|
|
|
|
|
if (arr.length === 0 || arr.includes(val)) {
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
return arr[0];
|
|
|
|
|
};
|
2023-08-09 17:33:51 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 等待
|
|
|
|
|
* @param {*} delay
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const sleep = (delay) =>
|
|
|
|
|
new Promise((resolve) => setTimeout(resolve, delay));
|
2023-08-16 22:13:07 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 防抖函数
|
|
|
|
|
* @param {*} func
|
|
|
|
|
* @param {*} delay
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const debounce = (func, delay = 200) => {
|
|
|
|
|
let timer;
|
|
|
|
|
return (...args) => {
|
|
|
|
|
timer && clearTimeout(timer);
|
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
|
func(...args);
|
|
|
|
|
}, delay);
|
|
|
|
|
};
|
|
|
|
|
};
|
2023-08-18 13:16:17 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 字符串通配符(*)匹配
|
|
|
|
|
* @param {*} s
|
|
|
|
|
* @param {*} p
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const isMatch = (s, p) => {
|
|
|
|
|
if (s.length === 0 || p.length === 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p = `*${p}*`;
|
|
|
|
|
|
|
|
|
|
let [sIndex, pIndex] = [0, 0];
|
|
|
|
|
let [sRecord, pRecord] = [-1, -1];
|
|
|
|
|
while (sIndex < s.length && pRecord < p.length) {
|
|
|
|
|
if (p[pIndex] === "*") {
|
|
|
|
|
pIndex++;
|
|
|
|
|
[sRecord, pRecord] = [sIndex, pIndex];
|
|
|
|
|
} else if (s[sIndex] === p[pIndex]) {
|
|
|
|
|
sIndex++;
|
|
|
|
|
pIndex++;
|
|
|
|
|
} else if (sRecord + 1 < s.length) {
|
|
|
|
|
sRecord++;
|
|
|
|
|
[sIndex, pIndex] = [sRecord, pRecord];
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (p.length === pIndex) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return p.slice(pIndex).replaceAll("*", "") === "";
|
|
|
|
|
};
|