« 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.js38
1 files changed, 37 insertions, 1 deletions
diff --git a/playlist-utils.js b/playlist-utils.js
index de2c3f8..9c6f0ed 100644
--- a/playlist-utils.js
+++ b/playlist-utils.js
@@ -560,6 +560,15 @@ function isTrack(obj) {
   return !!(obj && obj.downloaderArg)
 }
 
+function isPlayable(obj) {
+  return isGroup(obj) || isTrack(obj)
+}
+
+function isOpenable(obj) {
+  return !!(obj && obj.url)
+}
+
+
 function searchForItem(grouplike, value, preferredStartIndex = -1) {
   if (value.length) {
     // We prioritize searching past the index that the user opened the jump
@@ -600,6 +609,31 @@ function searchForItem(grouplike, value, preferredStartIndex = -1) {
   return null
 }
 
+function getCorrespondingFileForItem(item, extension) {
+  if (!(item && item.url)) {
+    return null
+  }
+
+  const checkExtension = item => item.url && path.extname(item.url) === extension
+
+  if (isPlayable(item)) {
+    const parent = item[parentSymbol]
+
+    if (!parent) {
+      return null
+    }
+
+    const basename = path.basename(item.url, path.extname(item.url))
+    return parent.items.find(item => checkExtension(item) && path.basename(item.url, extension) === basename)
+  }
+
+  if (checkExtension(item)) {
+    return item
+  }
+
+  return null
+}
+
 module.exports = {
   parentSymbol,
   updatePlaylistFormat, updateGroupFormat, updateTrackFormat,
@@ -618,5 +652,7 @@ module.exports = {
   getTrackIndexInParent,
   getNameWithoutTrackNumber,
   searchForItem,
-  isGroup, isTrack
+  getCorrespondingFileForItem,
+  isGroup, isTrack,
+  isOpenable, isPlayable
 }