All files / gitwork/src utils.js

98.74% Statements 157/159
86.95% Branches 80/92
98.55% Functions 68/69
98.49% Lines 131/133

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552                5x           5x             4x         4x             4x 1x 1x                         5x 4x 4x             5x 5x 4x 3x 2x 2x 1x 1x               6x 6x 6x   6x 6x 6x             4x 7x 7x 7x     7x               2x   2x 4x 2x 2x 4x 7x 7x       3x     3x   2x 2x 2x 2x 2x     2x             3x 3x 3x                         3x 9x 9x 9x 5x   4x                 4x             2x                           1x 3x 3x 3x 3x               1x 3x 3x 3x               9x             2x 1x             2x 1x 2x               2x 1x 2x               1x 1x 3x 3x 2x 2x               2x             7x               2x 5x 5x   4x 4x 3x       1x               1x 3x 3x                 1x 4x 2x 2x 2x                 101x             5x 5x             4x 4x   3x   1x               2x 1x             2x                       2x             2x             1x             1x             1x             10x 8x 7x 6x 5x             5x             5x             4x             4x             5x             4x             3x             4x             3x             3x             3x             6x             8x     1x                                                                                                                          
/**
 * Utils - 通用工具函数
 */
 
/**
 * 格式化任务状态
 */
function formatStatus(status) {
  const statusMap = {
    pending: '⏳ 待分配',
    in_progress: '🔄 进行中',
    completed: '✅ 已完成',
    cancelled: '❌ 已取消'
  };
  return statusMap[status] || `🔵 ${status}`;
}
 
/**
 * 格式化优先级
 */
function formatPriority(priority) {
  const priorityMap = {
    1: '🔴 高',
    2: '🟡 中',
    3: '🟢 低'
  };
  return priorityMap[priority] || `⚪ ${priority}`;
}
 
/**
 * 格式化时间
 */
function formatTime(date) {
  if (!date) return '-';
  const d = new Date(date);
  return d.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  });
}
 
/**
 * 计算时间差(分钟)
 */
function timeDiffMinutes(date) {
  if (!date) return null;
  const diff = Date.now() - new Date(date).getTime();
  return Math.floor(diff / 60000);
}
 
/**
 * 格式化时间差
 */
function formatTimeDiff(date) {
  const minutes = timeDiffMinutes(date);
  if (minutes === null) return '-';
  if (minutes < 1) return '刚刚';
  if (minutes < 60) return `${minutes} 分钟前`;
  const hours = Math.floor(minutes / 60);
  if (hours < 24) return `${hours} 小时前`;
  const days = Math.floor(hours / 24);
  return `${days} 天前`;
}
 
/**
 * 创建进度条
 */
function createProgressBar(percentage, width = 50) {
  // 限制百分比在 0-100 范围内
  const clamped = Math.max(0, Math.min(100, percentage));
  const filled = Math.round((clamped / 100) * width);
  const empty = width - filled;
  // 确保 filled 和 empty 都是非负数
  const safeFilled = Math.max(0, Math.min(width, filled));
  const safeEmpty = Math.max(0, width - safeFilled);
  return '█'.repeat(safeFilled) + '░'.repeat(safeEmpty);
}
 
/**
 * 格式化表格行
 */
function formatTableRow(cells, widths) {
  return cells.map((cell, i) => {
    const str = String(cell || '');
    const width = widths[i] || str.length;
    Iif (str.length > width) {
      return str.slice(0, width - 3) + '...';
    }
    return str.padEnd(width, ' ');
  }).join(' | ');
}
 
/**
 * 创建表格
 */
function createTable(headers, rows, colWidths = null) {
  Eif (!colWidths) {
    // 自动计算列宽
    const allRows = [headers, ...rows];
    const maxCols = Math.max(...allRows.map(r => r.length));
    colWidths = Array(maxCols).fill(0);
    allRows.forEach(row => {
      row.forEach((cell, i) => {
        const width = String(cell || '').length;
        if (width > colWidths[i]) colWidths[i] = width;
      });
    });
    // 添加 padding
    colWidths = colWidths.map(w => Math.min(w + 2, 30));
  }
 
  const separator = colWidths.map(w => '─'.repeat(w)).join('─┬─');
  
  let result = '';
  result += '|' + formatTableRow(headers, colWidths) + '|\n';
  result += '|' + separator + '|\n';
  rows.forEach(row => {
    result += '|' + formatTableRow(row, colWidths) + '|\n';
  });
  
  return result;
}
 
/**
 * 分页
 */
function paginate(items, page = 1, pageSize = 10) {
  const start = (page - 1) * pageSize;
  const end = start + pageSize;
  return {
    items: items.slice(start, end),
    total: items.length,
    page,
    pageSize,
    totalPages: Math.ceil(items.length / pageSize)
  };
}
 
/**
 * 排序
 */
function sortByKey(items, key, order = 'asc') {
  return [...items].sort((a, b) => {
    const aVal = a[key];
    const bVal = b[key];
    if (order === 'asc') {
      return aVal > bVal ? 1 : -1;
    } else {
      return aVal < bVal ? 1 : -1;
    }
  });
}
 
/**
 * 过滤
 */
function filterBy(items, field, value) {
  return items.filter(item => item[field] === value);
}
 
/**
 * 查找
 */
function findFirst(items, predicate) {
  return items.find(predicate);
}
 
/**
 * 映射
 */
function mapItems(items, mapper) {
  return items.map(mapper);
}
 
/**
 * 分组
 */
function groupBy(items, key) {
  return items.reduce((acc, item) => {
    const k = item[key];
    if (!acc[k]) acc[k] = [];
    acc[k].push(item);
    return acc;
  }, {});
}
 
/**
 * 计数
 */
function countBy(items, key) {
  return items.reduce((acc, item) => {
    const k = item[key];
    acc[k] = (acc[k] || 0) + 1;
    return acc;
  }, {});
}
 
/**
 * 求和
 */
function sumBy(items, key) {
  return items.reduce((sum, item) => sum + (item[key] || 0), 0);
}
 
/**
 * 平均值
 */
function averageBy(items, key) {
  if (items.length === 0) return 0;
  return sumBy(items, key) / items.length;
}
 
/**
 * 最大值
 */
function maxBy(items, key) {
  if (items.length === 0) return null;
  return items.reduce((max, item) => {
    return item[key] > max[key] ? item : max;
  });
}
 
/**
 * 最小值
 */
function minBy(items, key) {
  if (items.length === 0) return null;
  return items.reduce((min, item) => {
    return item[key] < min[key] ? item : min;
  });
}
 
/**
 * 去重
 */
function uniqueBy(items, key) {
  const seen = new Set();
  return items.filter(item => {
    const k = item[key];
    if (seen.has(k)) return false;
    seen.add(k);
    return true;
  });
}
 
/**
 * 限制数量
 */
function limit(items, n) {
  return items.slice(0, n);
}
 
/**
 * 延迟
 */
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
 
/**
 * 重试
 */
async function retry(fn, times = 3, delay = 1000) {
  let lastError;
  for (let i = 0; i < times; i++) {
    try {
      return await fn();
    } catch (error) {
      lastError = error;
      if (i < times - 1) {
        await sleep(delay);
      }
    }
  }
  throw lastError;
}
 
/**
 * 防抖
 */
function debounce(fn, delay) {
  let timer;
  return function(...args) {
    clearTimeout(timer);
    timer = setTimeout(() => fn.apply(this, args), delay);
  };
}
 
/**
 * 节流
 */
function throttle(fn, limit) {
  let inThrottle;
  return function(...args) {
    if (!inThrottle) {
      fn.apply(this, args);
      inThrottle = true;
      setTimeout(() => inThrottle = false, limit);
    }
  };
}
 
/**
 * 生成唯一 ID
 */
function generateId() {
  return Date.now().toString(36) + Math.random().toString(36).substr(2);
}
 
/**
 * 验证邮箱
 */
function isValidEmail(email) {
  const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  return re.test(email);
}
 
/**
 * 验证 URL
 */
function isValidUrl(url) {
  try {
    const parsed = new URL(url);
    // 只允许 http 和 https 协议
    return parsed.protocol === 'http:' || parsed.protocol === 'https:';
  } catch {
    return false;
  }
}
 
/**
 * 截断字符串
 */
function truncate(str, length = 50, suffix = '...') {
  if (str.length <= length) return str;
  return str.slice(0, length - suffix.length) + suffix;
}
 
/**
 * 转义 HTML
 */
function escapeHtml(str) {
  return str
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;')
    .replace(/'/g, '&#039;');
}
 
/**
 * 深拷贝
 */
function deepClone(obj) {
  return JSON.parse(JSON.stringify(obj));
}
 
/**
 * 合并对象
 */
function mergeObjects(...objects) {
  return Object.assign({}, ...objects);
}
 
/**
 * 获取对象键
 */
function getKeys(obj) {
  return Object.keys(obj);
}
 
/**
 * 获取对象值
 */
function getValues(obj) {
  return Object.values(obj);
}
 
/**
 * 获取对象条目
 */
function getEntries(obj) {
  return Object.entries(obj);
}
 
/**
 * 检查是否为空
 */
function isEmpty(value) {
  if (value === null || value === undefined) return true;
  if (value === '') return true;
  if (Array.isArray(value) && value.length === 0) return true;
  if (typeof value === 'object' && Object.keys(value).length === 0) return true;
  return false;
}
 
/**
 * 检查是否为数字
 */
function isNumber(value) {
  return !isNaN(parseFloat(value)) && isFinite(value);
}
 
/**
 * 检查是否为整数
 */
function isInteger(value) {
  return Number.isInteger(value);
}
 
/**
 * 检查是否为字符串
 */
function isString(value) {
  return typeof value === 'string';
}
 
/**
 * 检查是否为数组
 */
function isArray(value) {
  return Array.isArray(value);
}
 
/**
 * 检查是否为对象
 */
function isObject(value) {
  return value !== null && typeof value === 'object' && !Array.isArray(value);
}
 
/**
 * 检查是否为函数
 */
function isFunction(value) {
  return typeof value === 'function';
}
 
/**
 * 检查是否为日期
 */
function isDate(value) {
  return value instanceof Date && !isNaN(value.getTime());
}
 
/**
 * 检查是否为布尔值
 */
function isBoolean(value) {
  return typeof value === 'boolean';
}
 
/**
 * 检查是否为 null
 */
function isNull(value) {
  return value === null;
}
 
/**
 * 检查是否为 undefined
 */
function isUndefined(value) {
  return value === undefined;
}
 
/**
 * 检查是否为空值
 */
function isNil(value) {
  return value === null || value === undefined;
}
 
/**
 * 检查是否为真值
 */
function isTruthy(value) {
  return !!value;
}
 
/**
 * 检查是否为假值
 */
function isFalsy(value) {
  return !value;
}
 
module.exports = {
  // 格式化
  formatStatus,
  formatPriority,
  formatTime,
  formatTimeDiff,
  createProgressBar,
  formatTableRow,
  createTable,
  
  // 数据处理
  paginate,
  sortByKey,
  filterBy,
  findFirst,
  mapItems,
  groupBy,
  countBy,
  sumBy,
  averageBy,
  maxBy,
  minBy,
  uniqueBy,
  limit,
  
  // 异步工具
  sleep,
  retry,
  debounce,
  throttle,
  
  // 工具函数
  generateId,
  isValidEmail,
  isValidUrl,
  truncate,
  escapeHtml,
  deepClone,
  mergeObjects,
  
  // 对象工具
  getKeys,
  getValues,
  getEntries,
  
  // 类型检查
  isEmpty,
  isNumber,
  isInteger,
  isString,
  isArray,
  isObject,
  isFunction,
  isDate,
  isBoolean,
  isNull,
  isUndefined,
  isNil,
  isTruthy,
  isFalsy
};