"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toString = exports.paginate = exports.limitArrayLength = exports.generateRandomColor = exports.formatBytes = exports.isValidMqttTopic = exports.isValidIpAddress = exports.retry = exports.sleep = exports.deepClone = exports.generateId = exports.getDateMinutesAgo = exports.getDateHoursAgo = exports.getDateDaysAgo = exports.getTimeDifferenceInDays = exports.getTimeDifferenceInHours = exports.getTimeDifferenceInMinutes = exports.getTimeDifferenceInSeconds = exports.getCurrentDateTime = exports.getCurrentTimestamp = exports.formatTimestamp = exports.formatDateTime = exports.formatDate = void 0; const formatDate = (date) => { const d = new Date(date); return d.toISOString().split('T')[0]; }; exports.formatDate = formatDate; const formatDateTime = (date) => { const d = new Date(date); return d.toISOString(); }; exports.formatDateTime = formatDateTime; const formatTimestamp = (timestamp) => { return new Date(timestamp).toISOString(); }; exports.formatTimestamp = formatTimestamp; const getCurrentTimestamp = () => { return Date.now(); }; exports.getCurrentTimestamp = getCurrentTimestamp; const getCurrentDateTime = () => { return new Date().toISOString(); }; exports.getCurrentDateTime = getCurrentDateTime; const getTimeDifferenceInSeconds = (date1, date2) => { const d1 = new Date(date1); const d2 = new Date(date2); return Math.abs((d1.getTime() - d2.getTime()) / 1000); }; exports.getTimeDifferenceInSeconds = getTimeDifferenceInSeconds; const getTimeDifferenceInMinutes = (date1, date2) => { return (0, exports.getTimeDifferenceInSeconds)(date1, date2) / 60; }; exports.getTimeDifferenceInMinutes = getTimeDifferenceInMinutes; const getTimeDifferenceInHours = (date1, date2) => { return (0, exports.getTimeDifferenceInMinutes)(date1, date2) / 60; }; exports.getTimeDifferenceInHours = getTimeDifferenceInHours; const getTimeDifferenceInDays = (date1, date2) => { return (0, exports.getTimeDifferenceInHours)(date1, date2) / 24; }; exports.getTimeDifferenceInDays = getTimeDifferenceInDays; const getDateDaysAgo = (days) => { const date = new Date(); date.setDate(date.getDate() - days); return date; }; exports.getDateDaysAgo = getDateDaysAgo; const getDateHoursAgo = (hours) => { const date = new Date(); date.setHours(date.getHours() - hours); return date; }; exports.getDateHoursAgo = getDateHoursAgo; const getDateMinutesAgo = (minutes) => { const date = new Date(); date.setMinutes(date.getMinutes() - minutes); return date; }; exports.getDateMinutesAgo = getDateMinutesAgo; const generateId = () => { return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); }; exports.generateId = generateId; const 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 => (0, exports.deepClone)(item)); } if (typeof obj === 'object') { const clonedObj = {}; for (const key in obj) { if (obj.hasOwnProperty(key)) { clonedObj[key] = (0, exports.deepClone)(obj[key]); } } return clonedObj; } return obj; }; exports.deepClone = deepClone; const sleep = (ms) => { return new Promise(resolve => setTimeout(resolve, ms)); }; exports.sleep = sleep; const retry = async (fn, maxAttempts = 3, delayMs = 1000) => { let lastError; for (let attempt = 1; attempt <= maxAttempts; attempt++) { try { return await fn(); } catch (error) { lastError = error; console.error(`尝试 ${attempt}/${maxAttempts} 失败:`, error); if (attempt < maxAttempts) { console.log(`${delayMs}ms后重试...`); await (0, exports.sleep)(delayMs); } } } throw lastError; }; exports.retry = retry; const isValidIpAddress = (ip) => { const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; return ipv4Regex.test(ip); }; exports.isValidIpAddress = isValidIpAddress; const isValidMqttTopic = (topic) => { if (!topic || topic.length > 65535) { return false; } const hasWildcards = topic.includes('#') || topic.includes('+'); if (hasWildcards) { if (topic.includes('#') && topic.lastIndexOf('#') !== topic.length - 1) { return false; } const parts = topic.split('/'); for (const part of parts) { if (part === '+') { continue; } if (part.includes('+')) { return false; } } } return true; }; exports.isValidMqttTopic = isValidMqttTopic; const formatBytes = (bytes, decimals = 2) => { if (bytes === 0) return '0 Bytes'; const k = 1024; const dm = decimals < 0 ? 0 : decimals; const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; }; exports.formatBytes = formatBytes; const generateRandomColor = () => { return '#' + Math.floor(Math.random() * 16777215).toString(16); }; exports.generateRandomColor = generateRandomColor; const limitArrayLength = (array, maxLength) => { return array.length > maxLength ? array.slice(0, maxLength) : array; }; exports.limitArrayLength = limitArrayLength; const paginate = (items, page = 1, pageSize = 10) => { const totalPages = Math.ceil(items.length / pageSize); const currentPage = Math.max(1, Math.min(page, totalPages)); const startIndex = (currentPage - 1) * pageSize; const endIndex = startIndex + pageSize; return { items: items.slice(startIndex, endIndex), totalPages, currentPage, hasNext: currentPage < totalPages, hasPrev: currentPage > 1 }; }; exports.paginate = paginate; const toString = (value) => { if (Array.isArray(value)) { const first = value[0]; if (typeof first === 'string') { return first; } return String(first || ''); } if (typeof value === 'string') { return value; } return String(value || ''); }; exports.toString = toString; //# sourceMappingURL=helpers.js.map