From 182514a1e8170d124907b982f5f87f1bffad147c Mon Sep 17 00:00:00 2001 From: Florrie Date: Mon, 22 Jul 2019 14:58:40 -0300 Subject: "Reverse all" / "Reverse order of groups" options --- playlist-utils.js | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) (limited to 'playlist-utils.js') 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, -- cgit 1.3.0-6-gf8a5