本文最后更新于 2025-01-10,文章内容可能已经过时。

随机生成16进制颜色

console.log("#" + Math.random().toString(16).substring(2,8))
// 输出: #f9eb3a

字节单位大小转换

export function byteSizeTransfer( kb ){
  let units = ['KB', 'MB', 'GB', 'TB', 'PB'];
  let unitIndex = 0;

  while (kb >= 1024 && unitIndex < units.length - 1) {
    kb /= 1024;
    unitIndex++;
    console.log("kb", kb, unitIndex)
  }

  return `${kb.toFixed(2)} ${units[unitIndex]}`;
}
let size = formatSizeUnits(124011230)
console.log("size", size)
// size 118.27 GB

分页

export const currentPageData = (currentPage, pageSize, arr) =>{
  let offset = (currentPage - 1) * pageSize;
  let pageData = (offset + pageSize >= arr.length)
    ? arr.slice(offset, arr.length)
    : arr.slice(offset, offset + pageSize);
  return pageData;
}