« get me outta code hell

"Reverse all" / "Reverse order of groups" options - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/playlist-utils.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2019-07-22 14:58:40 -0300
committerFlorrie <towerofnix@gmail.com>2019-07-22 14:58:40 -0300
commit182514a1e8170d124907b982f5f87f1bffad147c (patch)
tree20c95e694791ea0a6a3b577b682d13e65cbb37d4 /playlist-utils.js
parent366da7740e4d07461572f1e56c51b3ecd8ad4bc9 (diff)
"Reverse all" / "Reverse order of groups" options
Diffstat (limited to 'playlist-utils.js')
-rw-r--r--playlist-utils.js42
1 files changed, 14 insertions, 28 deletions
diff --git a/playlist-utils.js b/playlist-utils.js
index e6d8947..d2a0bad 100644
--- a/playlist-utils.js
+++ b/playlist-utils.js
@@ -197,35 +197,20 @@ function shuffleOrderOfGroups(grouplike) {
   // *if* you also did --collapse-groups first. That was how shuffle-groups was
   // usually used (by me) anyway, so I figure bringing it over (with simpler
   // code) is reasonable. The only potentially confusing part is the behavior
-  // when a group contains both tracks and groups (the tracks are all
-  // considered "leftover" and randomly shuffled between the flat groups in the
-  // end result)... but I couldn't think of any better way to incorporate such
-  // "leftover" items into the result.)
-
-  // First filter the given grouplike into a list of "flat" groups, which are
-  // groups that only contain tracks - no groups. When a group contains both
-  // groups and tracks, add these "leftover" tracks to the list too.
-  const flatItems = []
-  const recursive = group => {
-    for (const item of group.items) {
-      if (isGroup(item) && item.items.every(isTrack)) {
-        flatItems.push(item)
-      } else if (isGroup(item)) {
-        recursive(item)
-      } else {
-        flatItems.push(item)
-      }
-    }
-  }
-  recursive(grouplike)
-
-  // Now shuffle this list of groups (and potentially tracks). This won't
-  // shuffle the *contents* of the groups; only the order in which the whole
-  // list of groups (and tracks) plays.
-  const shuffled = shuffleArray(flatItems)
+  // when a group contains both tracks and groups (the extra tracks in each
+  // group are collected together and considered "leftover", and are treated as
+  // their own ordered flat groups).
+
+  // Shuffle the list of groups (and potentially tracks). This won't shuffle
+  // the *contents* of the groups; only the order in which the whole list of
+  // groups (and tracks) plays.
+  const { items } = collapseGrouplike(grouplike)
+  return {items: shuffleArray(items)}
+}
 
-  // And we're done! Return the shuffled list as a grouplike.
-  return {items: shuffled}
+function reverseOrderOfGroups(grouplike) {
+  const { items } = collapseGrouplike(grouplike)
+  return {items: items.reverse()}
 }
 
 function collectGrouplikeChildren(grouplike, filter = null) {
@@ -621,6 +606,7 @@ module.exports = {
   filterTracks,
   flattenGrouplike, countTotalItems,
   shuffleOrderOfGroups,
+  reverseOrderOfGroups,
   partiallyFlattenGrouplike, collapseGrouplike,
   filterGrouplikeByProperty,
   filterPlaylistByPathString, filterGrouplikeByPath,