« get me outta code hell

cloneGrouplike - 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:
authorFlorrie <towerofnix@gmail.com>2018-07-06 13:08:14 -0300
committerFlorrie <towerofnix@gmail.com>2018-07-06 13:08:14 -0300
commit75a7b9d21af673e2acfed0acf1768e5d76d6c89a (patch)
tree434c3a751b5b4b2b0dce79c412c196718309a3c8 /playlist-utils.js
parent288597ec9ae419c8dc2737a985de3f72a61ad247 (diff)
cloneGrouplike
Diffstat (limited to 'playlist-utils.js')
-rw-r--r--playlist-utils.js23
1 files changed, 23 insertions, 0 deletions
diff --git a/playlist-utils.js b/playlist-utils.js
index 92e62b1..f20ec98 100644
--- a/playlist-utils.js
+++ b/playlist-utils.js
@@ -104,6 +104,28 @@ function updateTrackFormat(track) {
   return Object.assign(defaultTrack, trackObj)
 }
 
+function cloneGrouplike(grouplike) {
+  const newGrouplike = {
+    name: grouplike.name,
+    items: grouplike.items.map(item => {
+      if (isGroup(item)) {
+        return cloneGrouplike(item)
+      } else {
+        return {
+          name: item.name,
+          downloaderArg: item.downloaderArg
+        }
+      }
+    })
+  }
+
+  for (const item of newGrouplike.items) {
+    item[parentSymbol] = newGrouplike
+  }
+
+  return newGrouplike
+}
+
 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
@@ -468,6 +490,7 @@ function isTrack(obj) {
 module.exports = {
   parentSymbol,
   updatePlaylistFormat, updateGroupFormat, updateTrackFormat,
+  cloneGrouplike,
   filterTracks,
   flattenGrouplike,
   partiallyFlattenGrouplike, collapseGrouplike,