From 94c68dbdca1bb57086cd5921f87f47b8a30e21fa Mon Sep 17 00:00:00 2001 From: Florrie Date: Thu, 28 Sep 2017 22:00:45 -0300 Subject: Add --filter --- src/play.js | 16 +++++++++++++++- src/playlist-utils.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/play.js b/src/play.js index 6061d09..6adbb9c 100755 --- a/src/play.js +++ b/src/play.js @@ -14,7 +14,7 @@ const processSmartPlaylist = require('./smart-playlist') const { filterPlaylistByPathString, removeGroupByPathString, getPlaylistTreeString, - updatePlaylistFormat, collapseGrouplike + updatePlaylistFormat, collapseGrouplike, filterGrouplikeByProperty } = require('./playlist-utils') const readFile = promisify(fs.readFile) @@ -300,6 +300,20 @@ async function main(args) { 'r': util => util.alias('-remove'), 'x': util => util.alias('-remove'), + '-filter': function(util) { + // --filter (alias: -f) + // Filters the playlist so that only tracks with the given property- + // value pair are kept. + + const property = util.nextArg() + const value = util.nextArg() + + const p = filterGrouplikeByProperty(activePlaylist, property, value) + activePlaylist = updatePlaylistFormat(p) + }, + + 'f': util => util.alias('-filter'), + '-collapse-groups': function() { // --collapse-groups (alias: --collapse) // Collapses groups in the active playlist so that there is only one diff --git a/src/playlist-utils.js b/src/playlist-utils.js index 6452559..4abcfab 100644 --- a/src/playlist-utils.js +++ b/src/playlist-utils.js @@ -185,6 +185,36 @@ function collapseGrouplike(grouplike) { return {items: ret} } +function filterGrouplikeByProperty(grouplike, property, value) { + // Returns a copy of the original grouplike, only keeping tracks with the + // given property-value pair. (If the track's value for the given property + // is an array, this will check if that array includes the given value.) + + return Object.assign({}, grouplike, { + items: grouplike.items.map(item => { + if (isGroup(item)) { + const newGroup = filterGrouplikeByProperty(item, property, value) + if (newGroup.items.length) { + return newGroup + } else { + return false + } + } else if (isTrack(item)) { + const itemValue = item[property] + if (Array.isArray(itemValue) && itemValue.includes(value)) { + return item + } else if (item[property] === value) { + return item + } else { + return false + } + } else { + return item + } + }).filter(item => item !== false) + }) +} + function filterPlaylistByPathString(playlist, pathString) { // Calls filterGroupContentsByPath, taking an unparsed path string. @@ -403,6 +433,7 @@ module.exports = { updatePlaylistFormat, updateTrackFormat, flattenGrouplike, partiallyFlattenGrouplike, collapseGrouplike, + filterGrouplikeByProperty, filterPlaylistByPathString, filterGrouplikeByPath, removeGroupByPathString, removeGroupByPath, getPlaylistTreeString, -- cgit 1.3.0-6-gf8a5