Design system.
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.
 
 
 

55 line
1.4 KiB

  1. export const formatNumeral = (n?: number) => {
  2. if (typeof n !== 'number') {
  3. return '';
  4. }
  5. if (!Number.isFinite(n)) {
  6. return '';
  7. }
  8. return new Intl.NumberFormat().format(n);
  9. }
  10. export const formatFileSize = (size?: number) => {
  11. if (typeof size !== 'number') {
  12. return '';
  13. }
  14. if (!Number.isFinite(size)) {
  15. return '';
  16. }
  17. if (size < (2 ** 10)) {
  18. return `${formatNumeral(size)} byte(s)`;
  19. }
  20. if (size < (2 ** 20)) {
  21. return `${(size / (2 ** 10)).toFixed(3)} kiB`;
  22. }
  23. if (size < (2 ** 30)) {
  24. return `${(size / (2 ** 20)).toFixed(3)} MiB`;
  25. }
  26. if (size < (2 ** 40)) {
  27. return `${(size / (2 ** 30)).toFixed(3)} GiB`;
  28. }
  29. };
  30. export const formatSecondsDurationPrecise = (seconds: number) => {
  31. const secondsInt = Math.floor(seconds);
  32. const secondsFrac = seconds - secondsInt;
  33. const hh = Math.floor(secondsInt / 3600).toString().padStart(2, '0');
  34. const mm = Math.floor(secondsInt / 60 % 60).toString().padStart(2, '0');
  35. const ss = (secondsInt % 60).toString().padStart(2, '0');
  36. const sss = Math.floor(secondsFrac * 1000).toString().padStart(3, '0');
  37. return `${hh}:${mm}:${ss}.${sss}`;
  38. };
  39. export const formatSecondsDurationConcise = (seconds: number) => {
  40. const secondsInt = Math.floor(seconds);
  41. const mm = Math.floor(secondsInt / 60).toString().padStart(2, '0');
  42. const ss = (secondsInt % 60).toString().padStart(2, '0');
  43. return `${mm}:${ss}`;
  44. };