« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--playlist-utils.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/playlist-utils.js b/playlist-utils.js
index 0fe26df..8611f06 100644
--- a/playlist-utils.js
+++ b/playlist-utils.js
@@ -157,6 +157,31 @@ export function filterTracks(grouplike, handleTrack) {
   })
 }
 
+export function selectTracks(grouplike, mode = 'all', modeValue = 0) {
+  // Sort of like filterTracks, but with some ready-to-go modes for convenient
+  // results! Unlike filterTracks, this ignores the grouplike's structure and
+  // flattens it before processing and returning a flat list of tracks.
+
+  const all = flattenGrouplike(grouplike)
+
+  switch (mode) {
+    case 'all':
+      return all
+
+    case 'random': {
+      const count =
+        (modeValue.toString().endsWith('%')
+          ? Math.ceil(parseInt(modeValue) / 100 * all.items.length)
+          : parseInt(modeValue))
+
+      return {items: shuffleArray(all.items, count)}
+    }
+
+    default:
+      return {items: []}
+  }
+}
+
 export function flattenGrouplike(grouplike) {
   // Flattens a group-like, taking all of the non-group items (tracks) at all
   // levels in the group tree and returns them as a new group containing those