1. 1 : /**
  2. 2 : * @file format-time.js
  3. 3 : * @module format-time
  4. 4 : */
  5. 5 :
  6. 6 : /**
  7. 7 : * Format seconds as a time string, H:MM:SS or M:SS. Supplying a guide (in
  8. 8 : * seconds) will force a number of leading zeros to cover the length of the
  9. 9 : * guide.
  10. 10 : *
  11. 11 : * @private
  12. 12 : * @param {number} seconds
  13. 13 : * Number of seconds to be turned into a string
  14. 14 : *
  15. 15 : * @param {number} guide
  16. 16 : * Number (in seconds) to model the string after
  17. 17 : *
  18. 18 : * @return {string}
  19. 19 : * Time formatted as H:MM:SS or M:SS
  20. 20 : */
  21. 21 : const defaultImplementation = function(seconds, guide) {
  22. 22 : seconds = seconds < 0 ? 0 : seconds;
  23. 23 : let s = Math.floor(seconds % 60);
  24. 24 : let m = Math.floor(seconds / 60 % 60);
  25. 25 : let h = Math.floor(seconds / 3600);
  26. 26 : const gm = Math.floor(guide / 60 % 60);
  27. 27 : const gh = Math.floor(guide / 3600);
  28. 28 :
  29. 29 : // handle invalid times
  30. 30 : if (isNaN(seconds) || seconds === Infinity) {
  31. 31 : // '-' is false for all relational operators (e.g. <, >=) so this setting
  32. 32 : // will add the minimum number of fields specified by the guide
  33. 33 : h = m = s = '-';
  34. 34 : }
  35. 35 :
  36. 36 : // Check if we need to show hours
  37. 37 : h = (h > 0 || gh > 0) ? h + ':' : '';
  38. 38 :
  39. 39 : // If hours are showing, we may need to add a leading zero.
  40. 40 : // Always show at least one digit of minutes.
  41. 41 : m = (((h || gm >= 10) && m < 10) ? '0' + m : m) + ':';
  42. 42 :
  43. 43 : // Check if leading zero is need for seconds
  44. 44 : s = (s < 10) ? '0' + s : s;
  45. 45 :
  46. 46 : return h + m + s;
  47. 47 : };
  48. 48 :
  49. 49 : // Internal pointer to the current implementation.
  50. 50 : let implementation = defaultImplementation;
  51. 51 :
  52. 52 : /**
  53. 53 : * Replaces the default formatTime implementation with a custom implementation.
  54. 54 : *
  55. 55 : * @param {Function} customImplementation
  56. 56 : * A function which will be used in place of the default formatTime
  57. 57 : * implementation. Will receive the current time in seconds and the
  58. 58 : * guide (in seconds) as arguments.
  59. 59 : */
  60. 60 : export function setFormatTime(customImplementation) {
  61. 61 : implementation = customImplementation;
  62. 62 : }
  63. 63 :
  64. 64 : /**
  65. 65 : * Resets formatTime to the default implementation.
  66. 66 : */
  67. 67 : export function resetFormatTime() {
  68. 68 : implementation = defaultImplementation;
  69. 69 : }
  70. 70 :
  71. 71 : /**
  72. 72 : * Delegates to either the default time formatting function or a custom
  73. 73 : * function supplied via `setFormatTime`.
  74. 74 : *
  75. 75 : * Formats seconds as a time string (H:MM:SS or M:SS). Supplying a
  76. 76 : * guide (in seconds) will force a number of leading zeros to cover the
  77. 77 : * length of the guide.
  78. 78 : *
  79. 79 : * @static
  80. 80 : * @example formatTime(125, 600) === "02:05"
  81. 81 : * @param {number} seconds
  82. 82 : * Number of seconds to be turned into a string
  83. 83 : *
  84. 84 : * @param {number} guide
  85. 85 : * Number (in seconds) to model the string after
  86. 86 : *
  87. 87 : * @return {string}
  88. 88 : * Time formatted as H:MM:SS or M:SS
  89. 89 : */
  90. 90 : function formatTime(seconds, guide = seconds) {
  91. 91 : return implementation(seconds, guide);
  92. 92 : }
  93. 93 :
  94. 94 : export default formatTime;