« get me outta code hell

http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/playlist-utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/playlist-utils.js')
-rw-r--r--src/playlist-utils.js27
1 files changed, 26 insertions, 1 deletions
diff --git a/src/playlist-utils.js b/src/playlist-utils.js
index 013f56a..c2f6aae 100644
--- a/src/playlist-utils.js
+++ b/src/playlist-utils.js
@@ -453,6 +453,30 @@ function isSameTrack(track1, track2) {
   return false
 }
 
+function getTrackIndexInParent(track) {
+  if (parentSymbol in track === false) {
+    throw new Error(
+      'getTrackIndexInParent called with a track that has no parent!'
+    )
+  }
+
+  const parent = track[parentSymbol]
+
+  let i = 0, foundTrack = false;
+  for (; i < parent.items.length; i++) {
+    if (isSameTrack(track, parent.items[i])) {
+      foundTrack = true
+      break
+    }
+  }
+
+  if (foundTrack === false) {
+    return [-1, parent.items.length]
+  } else {
+    return [i, parent.items.length]
+  }
+}
+
 function isGroup(obj) {
   return !!(obj && obj.items)
 
@@ -501,7 +525,7 @@ async function safeUnlink(file, playlist) {
 }
 
 module.exports = {
-  parentSymbol,
+  parentSymbol, oldSymbol,
   updatePlaylistFormat, updateTrackFormat,
   flattenGrouplike,
   partiallyFlattenGrouplike, collapseGrouplike,
@@ -512,6 +536,7 @@ module.exports = {
   getItemPathString,
   parsePathString,
   isSameTrack,
+  getTrackIndexInParent,
   isGroup, isTrack,
   safeUnlink
 }