diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2024-05-14 13:36:51 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2024-05-14 13:36:51 -0300 |
commit | 6d92670f2dd5849e51dd98780d09952164dfe2da (patch) | |
tree | 2689cf0aac3853dbced5732c3a118303db813e7b | |
parent | 2635f089b0ce5e37df538b68ca6263439bf91b40 (diff) |
handy dandy selectTracks function
-rw-r--r-- | playlist-utils.js | 25 |
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 |