utils.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. export default {
  2. guid: () => {
  3. [1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1]
  4. .map(b =>
  5. b
  6. ? Math.floor((1 + Math.random()) * 0x10000)
  7. .toString(16)
  8. .substring(1)
  9. : "-"
  10. )
  11. .join("");
  12. },
  13. formatTime: originalDuration => {
  14. if (originalDuration) {
  15. if (originalDuration < 0) return "0:00";
  16. let duration = originalDuration;
  17. let hours = Math.floor(duration / (60 * 60));
  18. duration -= hours * 60 * 60;
  19. let minutes = Math.floor(duration / 60);
  20. duration -= minutes * 60;
  21. let seconds = Math.floor(duration);
  22. if (hours === 0) {
  23. hours = "";
  24. }
  25. if (hours > 0) {
  26. if (minutes < 10) minutes = `0${minutes}`;
  27. }
  28. if (seconds < 10) {
  29. seconds = `0${seconds}`;
  30. }
  31. return `${hours}${hours ? ":" : ""}${minutes}:${seconds}`;
  32. }
  33. return false;
  34. },
  35. formatTimeLong: duration => {
  36. if (duration <= 0) return "0 seconds";
  37. const hours = Math.floor(duration / (60 * 60));
  38. const formatHours = () => {
  39. if (hours > 0) {
  40. if (hours > 1) {
  41. if (hours < 10) return `0${hours} hours `;
  42. return `${hours} hours `;
  43. }
  44. return `0${hours} hour `;
  45. }
  46. return "";
  47. };
  48. const minutes = Math.floor((duration - hours * 60 * 60) / 60);
  49. const formatMinutes = () => {
  50. if (minutes > 0) {
  51. if (minutes > 1) {
  52. if (minutes < 10) return `0${minutes} minutes `;
  53. return `${minutes} minutes `;
  54. }
  55. return `0${minutes} minute `;
  56. }
  57. return "";
  58. };
  59. const seconds = Math.floor(duration - hours * 60 * 60 - minutes * 60);
  60. const formatSeconds = () => {
  61. if (seconds > 0) {
  62. if (seconds > 1) {
  63. if (seconds < 10) return `0${seconds} seconds `;
  64. return `${seconds} seconds `;
  65. }
  66. return `0${seconds} second `;
  67. }
  68. return "";
  69. };
  70. return formatHours() + formatMinutes() + formatSeconds();
  71. }
  72. };