« get me outta code hell

Modularize it all - 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:
authorLiam <towerofnix@gmail.com>2017-05-31 14:42:22 -0300
committerLiam <towerofnix@gmail.com>2017-05-31 14:42:22 -0300
commit42ec01bb91c517067a9eba901272c1248ed52261 (patch)
treed28bdaa85f9f02028de5f3aad76873a183e0c157 /src/pickers.js
parent07626fe82986214d4ef56a5790d864c4bf19b27e (diff)
Modularize it all
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
+}