工具函数
2025/4/9...大约 3 分钟
防抖函数
/**
* ## 防抖函数
* 最后一次执行,至少等待 delay 时间才执行,如果在 delay 时间内则重新计时
* @param {*} func 传入执行函数
* @param {*} delay 定义延迟时间
*/
export const debounce = (func, delay) => {
let timer;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(this, args);
}, delay);
}
}
节流函数
/**
* ## 节流函数
* func 函数离上一次执行间隔 delay 才执行下一次,如果在 delay 时间内则重新计时,如果大于 delay 则更新最后一次的执行时间
* @param {*} func 传入执行函数
* @param {*} delay 定义延迟时间
*/
export const throttle = (func, delay) => {
let timer,lase;
return function (...args) {
let now = +new Date();
if (last && now < last + delay) {
clearTimeout(timer);
timer = setTimeout(() => {
last = now;
func.apply(this, args);
}, delay);
} else {
last = now;
func.apply(this, args);
}
}
}
深拷贝
/**
* ## 深拷贝
* @param {*} obj 传入需要深拷贝的对象
*/
export const deepClone = (obj) => {
// 非引用类型直接返回
if (!(obj instanceof Object)) return obj;
// 如果形参 obj 是数组,就创建数组,如果是对象就创建对象
let objCopy = obj instanceof Array ? [] : {};
for (let key in obj) {
if (obj instanceof Object) {
objCopy[key] = deepClone(obj[key]);
} else {
if (obj.hasOwnProperty(key)) {
objCopy[key] = obj[key];
}
}
}
return objCopy;
}
对象中删除某个数,并返回新的对象
/**
* 实现从无限嵌套对象或数组中删除某个数,并返回新的对象
* @param {*} object 传入需要删除的对象
* @param {*} item 传入需要删除的值
*/
export const deleteItemFromObject = (obj, item) => {
if (obj instanceof Object) {
const objCopy = obj instanceof Array ? [] : {};
for (let i in obj) {
if (obj[i] instanceof Array) {
objCopy[i] = obj[i].filter(j => j !== item).map(h => deleteItemFromObject(h, item));
} else {
objCopy[i] = deleteItemFromObject(obj[i], item);
}
}
} else {
return obj === item ? null : obj;
}
}
请求队列设置最大请求数量
/**
* 控制并发请求,所有的请求放到请求队列中,当请求数量大于number时则在队列中排队等候
* @param {*} reqs 请求
* @param {*} number 请求队列的数量
*/
export const reqQueue = (reqs, number) => {
reqs = reqs || [];
const requestQueue = (concurrency) => {
concurrency = concurrency || 6; // 最大请求数
const queue = []; // 请求队列
let current = 0; // 当前请求数
const dequeue = () => {
while (current < concurrency && queue.length) {
current++;
const requestPromiseFactory = queue.shift(); // 获取请求
requestPromiseFactory()
.then(() => {
// 请求成功
})
.catch(() => {
// 请求失败
})
.finally(() => {
current--;
dequeue(); // 继续请求
})
}
}
return (requestPromiseFactory) => {
queue.push(requestPromiseFactory); // 将请求放入队列
dequeue(); // 开始请求
}
}
const enqueue = requestQueue(number); // 创建请求队列
for (let i = 0; i < reqs.length; i++) {
enqueue(() => axios.get('/api/demo' + i));
}
}
实现 get 方法
/**
* 从对象中获取嵌套属性的值,如果属性不存在,则返回默认值
* @param {Object} object - 要获取属性值的对象
* @param {Array|string} path - 属性路径,可以是数组或字符串
* @param {*} defaultValue - 默认值,如果属性不存在,则返回该值
* @return {*} - 返回嵌套属性的值或默认值
*/
function get(object, path, defaultValue) {
// 如果 path 是字符串,使用 . 或 [] 分割成数组
if (typeof path === 'string') {
path = path.replace(/\[(\w+)\]/g, '.$1'); // 将 [key] 转换为 .key
path = path.replace(/^\./, ''); // 去掉开头的 .
path = path.split('.'); // 分割成数组
}
// 使用数组路径进行遍历
for (let i = 0; i < path.length; i++) {
if (object == null) { // 如果对象为 null 或 undefined,返回默认值
return defaultValue;
}
object = object[path[i]]; // 获取嵌套属性
}
return object === undefined ? defaultValue : object;
}
示例
const obj = {
a: {
b: {
c: 1
}
}
}
console.log(get(obj, 'a.b.c')); // 结果: 1
console.log(get(obj, ['a', 'b', 'c'])); // 结果: 1
贡献者
廿四