utils.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. export default {
  2. guid: () =>
  3. "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, symbol => {
  4. let array;
  5. if (symbol === "y") {
  6. array = ["8", "9", "a", "b"];
  7. return array[Math.floor(Math.random() * array.length)];
  8. }
  9. array = new Uint8Array(1);
  10. window.crypto.getRandomValues(array);
  11. return (array[0] % 16).toString(16);
  12. }),
  13. formatTime: (originalDuration: number) => {
  14. if (originalDuration <= 0) return "0:00";
  15. let duration = originalDuration;
  16. let hours: number | string = Math.floor(duration / (60 * 60));
  17. duration -= hours * 60 * 60;
  18. let minutes: number | string = Math.floor(duration / 60);
  19. duration -= minutes * 60;
  20. let seconds: number | string = Math.floor(duration);
  21. if (hours === 0) {
  22. hours = "";
  23. }
  24. if (hours > 0) {
  25. if (minutes < 10) minutes = `0${minutes}`;
  26. }
  27. if (seconds < 10) {
  28. seconds = `0${seconds}`;
  29. }
  30. return `${hours}${hours ? ":" : ""}${minutes}:${seconds}`;
  31. },
  32. formatTimeLong: (duration: number) => {
  33. if (duration <= 0) return "0 seconds";
  34. const hours = Math.floor(duration / (60 * 60));
  35. const formatHours = () => {
  36. if (hours > 0) {
  37. if (hours > 1) return `${hours} hours `;
  38. return `${hours} hour `;
  39. }
  40. return "";
  41. };
  42. const minutes = Math.floor((duration - hours * 60 * 60) / 60);
  43. const formatMinutes = () => {
  44. if (minutes > 0) {
  45. if (minutes > 1) return `${minutes} minutes `;
  46. return `${minutes} minute `;
  47. }
  48. return "";
  49. };
  50. const seconds = Math.floor(duration - hours * 60 * 60 - minutes * 60);
  51. const formatSeconds = () => {
  52. if (seconds > 0) {
  53. if (seconds > 1) return `${seconds} seconds `;
  54. return `${seconds} second `;
  55. }
  56. return "";
  57. };
  58. return formatHours() + formatMinutes() + formatSeconds();
  59. },
  60. getDateFormatted: (createdAt: number | Date) => {
  61. const date = new Date(createdAt);
  62. const year = date.getFullYear();
  63. const month = `${date.getMonth() + 1}`.padStart(2, "0");
  64. const day = `${date.getDate()}`.padStart(2, "0");
  65. const hour = `${date.getHours()}`.padStart(2, "0");
  66. const minute = `${date.getMinutes()}`.padStart(2, "0");
  67. return `${year}-${month}-${day} ${hour}:${minute}`;
  68. }
  69. };