diff options
author | Liam <towerofnix@gmail.com> | 2017-05-31 14:42:22 -0300 |
---|---|---|
committer | Liam <towerofnix@gmail.com> | 2017-05-31 14:42:22 -0300 |
commit | 42ec01bb91c517067a9eba901272c1248ed52261 (patch) | |
tree | d28bdaa85f9f02028de5f3aad76873a183e0c157 /src/pickers.js | |
parent | 07626fe82986214d4ef56a5790d864c4bf19b27e (diff) |
Modularize it all
Diffstat (limited to 'src/pickers.js')
-rw-r--r-- | src/pickers.js | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/pickers.js b/src/pickers.js new file mode 100644 index 0000000..236f9ea --- /dev/null +++ b/src/pickers.js @@ -0,0 +1,33 @@ +'use strict' + +const { flattenPlaylist } = require('./playlist-utils') + +function makeOrderedPlaylistPicker(playlist) { + const allSongs = flattenPlaylist(playlist) + let index = 0 + + return function() { + if (index < allSongs.length) { + const picked = allSongs[index] + index++ + return picked + } else { + return null + } + } +} + +function makeShufflePlaylistPicker(playlist) { + const allSongs = flattenPlaylist(playlist) + + return function() { + const index = Math.floor(Math.random() * allSongs.length) + const picked = allSongs[index] + return picked + } +} + +module.exports = { + makeOrderedPlaylistPicker, + makeShufflePlaylistPicker +} |