From 6d92670f2dd5849e51dd98780d09952164dfe2da Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Tue, 14 May 2024 13:36:51 -0300 Subject: handy dandy selectTracks function --- playlist-utils.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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 -- cgit 1.3.0-6-gf8a5