Skip to content

Instantly share code, notes, and snippets.

@Flare576
Created July 24, 2018 14:10
Show Gist options
  • Save Flare576/462bd7093bdc4a269137f88be5f29aa3 to your computer and use it in GitHub Desktop.
Save Flare576/462bd7093bdc4a269137f88be5f29aa3 to your computer and use it in GitHub Desktop.
A better song shuffle
function getNextSong(songList) {
if (!songList.length) {
return;
}
// First, are there any songs that have never been played?
let nextSong = songList[0];
songList.each(song => {
nextSong = isFewerOrOlder(nextSong, sont);
});
return nextSong;
}
function isFewerOrOlder (current, check) {
// If checking song has been played less, return it
if (check.playedCount < current.playCount) {
return check;
}
// If checking song has been played the same number of times...
if (check.playedCount === current.playCOunt) {
// And was played longer ago than current, return it
if (check.lastPlayedTS < current.lastPlayedTS) {
return check;
}
// Or, if they're both unplayed, reuturn the oldes added
if (!check.playedCount && check.addedTS < current.addedTS) {
return check;
}
return current;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment