From 64aa6eeb01491d04464206d94ae82107acb3f140 Mon Sep 17 00:00:00 2001 From: Florrie Date: Wed, 1 May 2019 18:11:15 -0300 Subject: Hide track number in queue listing! --- playlist-utils.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'playlist-utils.js') diff --git a/playlist-utils.js b/playlist-utils.js index 4367fb0..6741dd0 100644 --- a/playlist-utils.js +++ b/playlist-utils.js @@ -532,6 +532,40 @@ function getTrackIndexInParent(track) { } } +const nameWithoutTrackNumberSymbol = Symbol('Cached name without track number') +function getNameWithoutTrackNumber(track) { + // Be lazy and reuse an old value if possible! Don't do this if the track's + // name has changed. + const [oldName, cachedValue] = track[nameWithoutTrackNumberSymbol] || [] + if (cachedValue && track.name === oldName) { + return cachedValue + } + + // This is the expression that matches a track number at the start of + // a track's title. + const regex = /^[0-9\-]*\s*/ + + // First we need to determine whether every track in the group even starts + // with a track number. + const parent = track[parentSymbol] + if (parent) { + const names = parent.items.filter(isTrack).map(t => t.name) + if (names.some(name => !regex.test(name))) { + // If any of the names don't match the track number regex, just return + // the track name unmodified. + return track.name + } + } + + // Now actually perform the replacement to get rid of the track number! + const value = track.name.replace(regex, '') + + // Cache the value, so we don't need to do this whole process again. + track[nameWithoutTrackNumberSymbol] = [track.name, value] + + return value +} + function isGroup(obj) { return !!(obj && obj.items) } @@ -555,5 +589,6 @@ module.exports = { getItemPath, getItemPathString, parsePathString, getTrackIndexInParent, + getNameWithoutTrackNumber, isGroup, isTrack } -- cgit 1.3.0-6-gf8a5