All files / src/utilities shuffler.js

100% Statements 37/37
100% Branches 8/8
100% Functions 8/8
100% Lines 37/37
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 185 186 187 188                        31x               9x   9x 8x   1x                           6x 3x 3x   3x 3x                       9x   9x 8x   1x                             6x 3x 3x   3x 3x                               12x         12x 132x             12x 120x 120x           12x                             12x         12x 63x             12x 51x     51x           12x                       171x 171x 171x           31x                      
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * AmplitudeJS Shuffle Module. Handles all of the shuffling functionality for
 * AmplitudeJS
 *
 * @module utilities/Shuffler
 */
let Shuffler = (function() {
  /**
   * Sets the shuffle state globally
   *
   * @access public
   * @param {boolean} shuffle   - True when we are shuffling, false when we turn it off.
   */
  function setShuffle(shuffle) {
    config.shuffle_on = shuffle;
 
    if (shuffle) {
      shuffleSongs();
    } else {
      config.shuffle_list = [];
    }
  }
 
  /**
   * Toggles the shuffle status globally.
   *
   * @access public
   */
  function toggleShuffle() {
    /*
      If shuffle is on, we toggle it off. If shuffle is off, we
      toggle on.
    */
    if (config.shuffle_on) {
      config.shuffle_on = false;
      config.shuffle_list = [];
    } else {
      config.shuffle_on = true;
      shuffleSongs();
    }
  }
 
  /**
   * Sets the shuffle state for a playlist
   *
   * @access public
   * @param {string} playlist   The key of the playlist we are shuffling.
   * @param {boolean} shuffle   True when we are shuffling the playlist, false when we turn off shuffle.
   */
  function setShufflePlaylist(playlist, shuffle) {
    config.playlists[playlist].shuffle = shuffle;
 
    if (config.playlists[playlist].shuffle) {
      shufflePlaylistSongs(playlist);
    } else {
      config.playlists[playlist].shuffle_list = [];
    }
  }
 
  /**
   * Sets the shuffle state for a playlist
   *
   * @access public
   * @param {string} playlist   The key of the playlist we are shuffling.
   */
  function toggleShufflePlaylist(playlist) {
    /*
      If the playlist shuffled is on, we toggle it off. If the
      playlist shuffled is off, we toggle it on.
    */
    if (config.playlists[playlist].shuffle) {
      config.playlists[playlist].shuffle = false;
      config.playlists[playlist].shuffle_list = [];
    } else {
      config.playlists[playlist].shuffle = true;
      shufflePlaylistSongs(playlist);
    }
  }
 
  /**
   * Shuffles individual songs in the config
   * Based off of: http://www.codinghorror.com/blog/2007/12/the-danger-of-naivete.html
   *
   * Public Accessor: Shuffle.shuffleSongs()
   *
   * @access public
   */
  function shuffleSongs() {
    /*
			Builds a temporary array with the length of the config.
		*/
    let shuffleTemp = new Array(config.songs.length);
 
    /*
			Set the temporary array equal to the songs array.
		*/
    for (let i = 0; i < config.songs.length; i++) {
      shuffleTemp[i] = config.songs[i];
    }
 
    /*
			Iterate ove rthe songs and generate random numbers to
			swap the indexes of the shuffle array.
		*/
    for (let i = config.songs.length - 1; i > 0; i--) {
      let randNum = Math.floor(Math.random() * config.songs.length + 1);
      shuffleSwap(shuffleTemp, i, randNum - 1);
    }
 
    /*
			Set the shuffle list to the shuffle temp.
		*/
    config.shuffle_list = shuffleTemp;
  }
 
  /**
   * Shuffle songs in a playlist
   *
   * Public Accessor: Shuffle.shufflePlaylistSongs( playlist )
   *
   * @access public
   * @param {string} playlist - The playlist we are shuffling.
   */
  function shufflePlaylistSongs(playlist) {
    /*
      Builds a temporary array with the length of the playlist songs.
    */
    let shuffleTemp = new Array(config.playlists[playlist].songs.length);
 
    /*
      Set the temporary array equal to the playlist array.
    */
    for (let i = 0; i < config.playlists[playlist].songs.length; i++) {
      shuffleTemp[i] = config.playlists[playlist].songs[i];
    }
 
    /*
      Iterate ove rthe songs and generate random numbers to
      swap the indexes of the shuffle array.
    */
    for (let i = config.playlists[playlist].songs.length - 1; i > 0; i--) {
      let randNum = Math.floor(
        Math.random() * config.playlists[playlist].songs.length + 1
      );
      shuffleSwap(shuffleTemp, i, randNum - 1);
    }
 
    /*
      Set the shuffle list to the shuffle temp.
    */
    config.playlists[playlist].shuffle_list = shuffleTemp;
  }
 
  /**
   * Swaps and randomizes the song shuffle.
   *
   * @access private
   * @param {object} shuffleList 	- The list of songs that is going to be shuffled
   * @param {number} original 		- The original index of he song in the songs array
   * @param {number} random 			- The randomized index that will be the new index of the song in the shuffle array.
   */
  function shuffleSwap(shuffleList, original, random) {
    let temp = shuffleList[original];
    shuffleList[original] = shuffleList[random];
    shuffleList[random] = temp;
  }
 
  /**
   * Returns public facing methods
   */
  return {
    setShuffle: setShuffle,
    toggleShuffle: toggleShuffle,
    setShufflePlaylist: setShufflePlaylist,
    toggleShufflePlaylist: toggleShufflePlaylist,
    shuffleSongs: shuffleSongs,
    shufflePlaylistSongs: shufflePlaylistSongs
  };
})();
 
export default Shuffler;