You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

format.ts 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Formats a number with comma separators.
  3. * @example formatNumber(1234567) will return '1,234,567'
  4. * @example formatNumber(1234567.89) will return '1,234,567.89'
  5. */
  6. export const formatNumber = (num: number | string) => {
  7. if (!num)
  8. return num
  9. const parts = num.toString().split('.')
  10. parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
  11. return parts.join('.')
  12. }
  13. /**
  14. * Format file size into standard string format.
  15. * @param fileSize file size (Byte)
  16. * @example formatFileSize(1024) will return '1.00 KB'
  17. * @example formatFileSize(1024 * 1024) will return '1.00 MB'
  18. */
  19. export const formatFileSize = (fileSize: number) => {
  20. if (!fileSize)
  21. return fileSize
  22. const units = ['', 'K', 'M', 'G', 'T', 'P']
  23. let index = 0
  24. while (fileSize >= 1024 && index < units.length) {
  25. fileSize = fileSize / 1024
  26. index++
  27. }
  28. if (index === 0)
  29. return `${fileSize.toFixed(2)} bytes`
  30. return `${fileSize.toFixed(2)} ${units[index]}B`
  31. }
  32. /**
  33. * Format time into standard string format.
  34. * @example formatTime(60) will return '1.00 min'
  35. * @example formatTime(60 * 60) will return '1.00 h'
  36. */
  37. export const formatTime = (seconds: number) => {
  38. if (!seconds)
  39. return seconds
  40. const units = ['sec', 'min', 'h']
  41. let index = 0
  42. while (seconds >= 60 && index < units.length) {
  43. seconds = seconds / 60
  44. index++
  45. }
  46. return `${seconds.toFixed(2)} ${units[index]}`
  47. }
  48. export const downloadFile = ({ data, fileName }: { data: Blob; fileName: string }) => {
  49. const url = window.URL.createObjectURL(data)
  50. const a = document.createElement('a')
  51. a.href = url
  52. a.download = fileName
  53. document.body.appendChild(a)
  54. a.click()
  55. a.remove()
  56. window.URL.revokeObjectURL(url)
  57. }
  58. /**
  59. * Formats a number into a readable string using "k", "M", or "B" suffix.
  60. * @example
  61. * 950 => "950"
  62. * 1200 => "1.2k"
  63. * 1500000 => "1.5M"
  64. * 2000000000 => "2B"
  65. *
  66. * @param {number} num - The number to format
  67. * @returns {string} - The formatted number string
  68. */
  69. export const formatNumberAbbreviated = (num: number) => {
  70. // If less than 1000, return as-is
  71. if (num < 1000) return num.toString()
  72. // Define thresholds and suffixes
  73. const units = [
  74. { value: 1e9, symbol: 'B' },
  75. { value: 1e6, symbol: 'M' },
  76. { value: 1e3, symbol: 'k' },
  77. ]
  78. for (let i = 0; i < units.length; i++) {
  79. if (num >= units[i].value) {
  80. const formatted = (num / units[i].value).toFixed(1)
  81. return formatted.endsWith('.0')
  82. ? `${Number.parseInt(formatted)}${units[i].symbol}`
  83. : `${formatted}${units[i].symbol}`
  84. }
  85. }
  86. }