« get me outta code hell

Add --filter - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2017-09-28 22:00:45 -0300
committerFlorrie <towerofnix@gmail.com>2017-09-28 22:00:45 -0300
commit94c68dbdca1bb57086cd5921f87f47b8a30e21fa (patch)
treed3deb7b744e648639fdbf0e76aca326e8235a51a /src
parenteadd4ad2e26f47c529ffcbc3c96685947ee8b81c (diff)
Add --filter
Diffstat (limited to 'src')
-rwxr-xr-xsrc/play.js16
-rw-r--r--src/playlist-utils.js31
2 files changed, 46 insertions, 1 deletions
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 <property> <value>  (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,