From ed54217c7b1f720ce817a6df10f4f9920e815e5f Mon Sep 17 00:00:00 2001 From: Florrie Date: Wed, 24 Jan 2018 17:34:27 -0400 Subject: Let ?abc match tracks as well as groups If you want a *track* whose name is abc but also have a *group* that is named abc, just specify the name of the group containing abc: --keep ?theGroupName/abc instead of --keep ?abc. --- src/playlist-utils.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) (limited to 'src/playlist-utils.js') diff --git a/src/playlist-utils.js b/src/playlist-utils.js index 3983c7a..5343fb4 100644 --- a/src/playlist-utils.js +++ b/src/playlist-utils.js @@ -261,15 +261,7 @@ function filterGrouplikeByPath(grouplike, pathParts) { let possibleMatches if (firstPart.startsWith('?')) { - // TODO: Note to self - remove isGroup here to let this match anything, not - // just groups. Definitely want to do that in the future, but there'll need - // to be some preparing first - for example, what if a group contains a - // track which is the same name as the group? Then there are two possible - // matches; how should http-music know which to pick? Probably be biased to - // pick a group before a track, but.. that doesn't seem perfect either. And - // it doesn't solve the problem where there might be two descendants of the - // same name (groups or otherwise). - possibleMatches = collectGrouplikeChildren(grouplike, isGroup) + possibleMatches = collectGrouplikeChildren(grouplike) firstPart = firstPart.slice(1) } else { possibleMatches = grouplike.items -- cgit 1.3.0-6-gf8a5 From 1b5d6f8f96baae53367a7a7d0f9485a42029eaa3 Mon Sep 17 00:00:00 2001 From: Florrie Date: Fri, 16 Feb 2018 11:02:51 -0400 Subject: Make --track-display-file show SOURCE path to track This also means we're keeping track of the source item of items, which is sure to be useful more later. --- src/playlist-utils.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src/playlist-utils.js') diff --git a/src/playlist-utils.js b/src/playlist-utils.js index 5343fb4..dad828c 100644 --- a/src/playlist-utils.js +++ b/src/playlist-utils.js @@ -8,8 +8,9 @@ const unlink = promisify(fs.unlink) const parentSymbol = Symbol('Parent group') const oldSymbol = Symbol('Old track or group reference') +const sourceSymbol = Symbol('Source-playlist item reference') -function updatePlaylistFormat(playlist) { +function updatePlaylistFormat(playlist, firstTime = false) { const defaultPlaylist = { options: [], items: [] @@ -39,10 +40,10 @@ function updatePlaylistFormat(playlist) { const fullPlaylistObj = Object.assign(defaultPlaylist, playlistObj) - return updateGroupFormat(fullPlaylistObj) + return updateGroupFormat(fullPlaylistObj, firstTime) } -function updateGroupFormat(group) { +function updateGroupFormat(group, firstTime = false) { const defaultGroup = { name: '', items: [], @@ -62,7 +63,7 @@ function updateGroupFormat(group) { groupObj.items = groupObj.items.map(item => { // Check if it's a group; if not, it's probably a track. if (typeof item[1] === 'array' || item.items) { - item = updateGroupFormat(item) + item = updateGroupFormat(item, firstTime) } else { item = updateTrackFormat(item) @@ -79,6 +80,10 @@ function updateGroupFormat(group) { item[parentSymbol] = groupObj + if (firstTime) { + item[sourceSymbol] = item + } + return item }) @@ -517,7 +522,7 @@ async function safeUnlink(file, playlist) { } module.exports = { - parentSymbol, oldSymbol, + parentSymbol, oldSymbol, sourceSymbol, updatePlaylistFormat, updateTrackFormat, flattenGrouplike, partiallyFlattenGrouplike, collapseGrouplike, -- cgit 1.3.0-6-gf8a5 From b2ac9246886f72bef8b96cf218ed2d803397dafa Mon Sep 17 00:00:00 2001 From: Florrie Date: Sun, 18 Feb 2018 23:44:07 -0400 Subject: Make completely new filter system See the man page for how it works now. --- src/playlist-utils.js | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) (limited to 'src/playlist-utils.js') diff --git a/src/playlist-utils.js b/src/playlist-utils.js index dad828c..f817037 100644 --- a/src/playlist-utils.js +++ b/src/playlist-utils.js @@ -112,22 +112,37 @@ function updateTrackFormat(track) { return Object.assign(defaultTrack, trackObj) } -function mapGrouplikeItems(grouplike, handleTrack) { - if (typeof handleTrack === 'undefined') { +function filterTracks(grouplike, handleTrack) { + // Recursively filters every track in the passed grouplike. The track-handler + // function passed should either return true (to keep a track) or false (to + // remove the track). After tracks are filtered, groups which contain no + // items are removed. + + if (typeof handleTrack !== 'function') { throw new Error("Missing track handler function") } - return { - items: grouplike.items.map(item => { + return Object.assign({}, grouplike, { + items: grouplike.items.filter(item => { if (isTrack(item)) { return handleTrack(item) - } else if (isGroup(item)) { - return mapGrouplikeItems(item, handleTrack, handleGroup) } else { - throw new Error('Non-track/group item') + return true + } + }).map(item => { + if (isGroup(item)) { + return filterTracks(item, handleTrack) + } else { + return item + } + }).filter(item => { + if (isGroup(item)) { + return item.items.length > 0 + } else { + return true } }) - } + }) } function flattenGrouplike(grouplike) { @@ -524,6 +539,7 @@ async function safeUnlink(file, playlist) { module.exports = { parentSymbol, oldSymbol, sourceSymbol, updatePlaylistFormat, updateTrackFormat, + filterTracks, flattenGrouplike, partiallyFlattenGrouplike, collapseGrouplike, filterGrouplikeByProperty, -- cgit 1.3.0-6-gf8a5