All files / src/utilities time.js

100% Statements 36/36
73.08% Branches 19/26
100% Functions 5/5
100% Lines 36/36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184                    31x                     3x           3x           3x         3x         3x 2x             3x 1x 1x           1x 1x             3x 3x 3x   3x                         3x           3x           3x         3x         3x 2x             3x 1x 1x           1x 1x             3x     3x     3x       3x                 1x                       1x       1x 1x               31x                  
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * The utility to handle the computation of time in AmplitudeJS.
 * @module utilities/Time
 */
let Time = (function() {
  /**
   * Computes the current song time. Breaks down where the song is into
   * hours, minutes, seconds and formats it to be displayed to the user.
   *
   * @access public
   */
  function computeCurrentTimes() {
    /*
			Initialize the current time object that will be returned.
		*/
    let currentTime = {};
 
    /*
			Computes the current seconds for the song.
		*/
    let currentSeconds =
      (Math.floor(config.audio.currentTime % 60) < 10 ? "0" : "") +
      Math.floor(config.audio.currentTime % 60);
 
    /*
			Computes the current minutes for the song.
		*/
    let currentMinutes = Math.floor(config.audio.currentTime / 60);
 
    /*
			Initialize the current hours variable.
		*/
    let currentHours = "00";
 
    /*
			If the current minutes is less than 10, we add a leading 0.
		*/
    if (currentMinutes < 10) {
      currentMinutes = "0" + currentMinutes;
    }
 
    /*
			If the user is more than 60 minutes into the song, then
			we extract the hours.
		*/
    if (currentMinutes >= 60) {
      currentHours = Math.floor(currentMinutes / 60);
      currentMinutes = currentMinutes % 60;
 
      /*
				If the user is less than 10 minutes in, we append the
				additional 0 to the minutes.
			*/
      Eif (currentMinutes < 10) {
        currentMinutes = "0" + currentMinutes;
      }
    }
 
    /*
			Build a clean current time object and send back the appropriate information.
		*/
    currentTime.seconds = currentSeconds;
    currentTime.minutes = currentMinutes;
    currentTime.hours = currentHours;
 
    return currentTime;
  }
 
  /**
   * Computes the current song duration. Breaks down where the song is into
   * hours, minutes, seconds and formats it to be displayed to the user.
   *
   * @access public
   */
  function computeSongDuration() {
    /*
			Initialize the song duration object that will be returned.
		*/
    let songDuration = {};
 
    /*
			Computes the duration of the song's seconds.
		*/
    let songDurationSeconds =
      (Math.floor(config.audio.duration % 60) < 10 ? "0" : "") +
      Math.floor(config.audio.duration % 60);
 
    /*
			Computes the duration of the song's minutes.
		*/
    let songDurationMinutes = Math.floor(config.audio.duration / 60);
 
    /*
			Initialize the hours duration variable.
		*/
    var songDurationHours = "00";
 
    /*
			If the song duration minutes is less than 10, we add a leading 0.
		*/
    if (songDurationMinutes < 10) {
      songDurationMinutes = "0" + songDurationMinutes;
    }
 
    /*
			If there is more than 60 minutes in the song, then we
			extract the hours.
		*/
    if (songDurationMinutes >= 60) {
      songDurationHours = Math.floor(songDurationMinutes / 60);
      songDurationMinutes = songDurationMinutes % 60;
 
      /*
				If the song duration minutes is less than 10 we append
				the additional 0.
			*/
      Eif (songDurationMinutes < 10) {
        songDurationMinutes = "0" + songDurationMinutes;
      }
    }
 
    /*
			Build a clean song duration object and send back the appropriate information.
		*/
    songDuration.seconds = isNaN(songDurationSeconds)
      ? "00"
      : songDurationSeconds;
    songDuration.minutes = isNaN(songDurationMinutes)
      ? "00"
      : songDurationMinutes;
    songDuration.hours = isNaN(songDurationHours)
      ? "00"
      : songDurationHours.toString();
 
    return songDuration;
  }
 
  /**
   * Computes the song completion percentage.
   *
   * @access public
   */
  function computeSongCompletionPercentage() {
    return (config.audio.currentTime / config.audio.duration) * 100;
  }
 
  /**
   * Sets the current time for the audio.
   *
   * @access public
   */
  function setCurrentTime(time) {
    /*
      If the song is not live, we can set the current time.
    */
    Eif (!config.active_metadata.live) {
      /*
        Makes sure the number is finite to set the time.
      */
      Eif (isFinite(time)) {
        config.audio.currentTime = time;
      }
    }
  }
 
  /**
   * Defines what is returned by the module
   */
  return {
    computeCurrentTimes: computeCurrentTimes,
    computeSongDuration: computeSongDuration,
    computeSongCompletionPercentage: computeSongCompletionPercentage,
    setCurrentTime: setCurrentTime
  };
})();
 
export default Time;