utils.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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) return `${hours} hours `;
  41. return `${hours} hour `;
  42. }
  43. return "";
  44. };
  45. const minutes = Math.floor((duration - hours * 60 * 60) / 60);
  46. const formatMinutes = () => {
  47. if (minutes > 0) {
  48. if (minutes > 1) return `${minutes} minutes `;
  49. return `${minutes} minute `;
  50. }
  51. return "";
  52. };
  53. const seconds = Math.floor(duration - hours * 60 * 60 - minutes * 60);
  54. const formatSeconds = () => {
  55. if (seconds > 0) {
  56. if (seconds > 1) return `${seconds} seconds `;
  57. return `${seconds} second `;
  58. }
  59. return "";
  60. };
  61. return formatHours() + formatMinutes() + formatSeconds();
  62. }
  63. };