UNPKG

bridgecrate

Version:

一个提供数据传输和桥接功能的JavaScript工具包

74 lines (68 loc) 1.89 kB
// 工具函数模块 /** * 工具函数集合 */ export const utils = { /** * 检查数据是否为空 * @param {any} data - 要检查的数据 * @returns {boolean} 是否为空 */ isEmpty(data) { if (data === null || data === undefined) return true; if (typeof data === 'string') return data.trim() === ''; if (Array.isArray(data)) return data.length === 0; if (typeof data === 'object') return Object.keys(data).length === 0; return false; }, /** * 深拷贝对象 * @param {any} obj - 要拷贝的对象 * @returns {any} 拷贝后的对象 */ deepClone(obj) { if (obj === null || typeof obj !== 'object') return obj; if (obj instanceof Date) return new Date(obj.getTime()); if (obj instanceof Array) return obj.map(item => this.deepClone(item)); if (typeof obj === 'object') { const cloned = {}; Object.keys(obj).forEach(key => { cloned[key] = this.deepClone(obj[key]); }); return cloned; } return obj; }, /** * 延迟执行 * @param {number} ms - 延迟毫秒数 * @returns {Promise<void>} Promise对象 */ delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)); }, /** * 生成唯一ID * @returns {string} 唯一ID */ generateId() { return Date.now().toString(36) + Math.random().toString(36).substr(2); }, /** * 防抖函数 * @param {Function} func - 要防抖的函数 * @param {number} wait - 等待时间 * @returns {Function} 防抖后的函数 */ debounce(func, wait) { let timeout; return function executedFunction(...args) { const later = () => { clearTimeout(timeout); func(...args); }; clearTimeout(timeout); timeout = setTimeout(later, wait); }; } };