« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/playlist-utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'playlist-utils.js')
-rw-r--r--playlist-utils.js15
1 files changed, 14 insertions, 1 deletions
diff --git a/playlist-utils.js b/playlist-utils.js
index f20ec98..64c1df4 100644
--- a/playlist-utils.js
+++ b/playlist-utils.js
@@ -175,6 +175,19 @@ function flattenGrouplike(grouplike) {
   }
 }
 
+function countTotalItems(grouplike) {
+  // Returns the total number of items in a grouplike, including items in any
+  // descendant groups. Basically the same as flattenGrouplike().items.length.
+
+  return grouplike.items.map(item => {
+    if (isGroup(item)) {
+      return countTotalItems(item)
+    } else {
+      return 1
+    }
+  }).reduce((a, b) => a + b, 0)
+}
+
 function collectGrouplikeChildren(grouplike, filter = null) {
   // Collects all descendants of a grouplike into a single flat array.
   // Can be passed a filter function, which will decide whether or not to add
@@ -492,7 +505,7 @@ module.exports = {
   updatePlaylistFormat, updateGroupFormat, updateTrackFormat,
   cloneGrouplike,
   filterTracks,
-  flattenGrouplike,
+  flattenGrouplike, countTotalItems,
   partiallyFlattenGrouplike, collapseGrouplike,
   filterGrouplikeByProperty,
   filterPlaylistByPathString, filterGrouplikeByPath,