« get me outta code hell

http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/pickers.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/pickers.js')
-rw-r--r--src/pickers.js33
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
+}