« get me outta code hell

Add text/notes editor, using tui-text-editor - 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>2019-10-23 12:20:27 -0300
committerFlorrie <towerofnix@gmail.com>2019-10-23 12:20:27 -0300
commit147172fe720f8af8a219ba384c0407ca751e0321 (patch)
tree85b97ccb8ea210392493806ebf6d35e41615e76b /playlist-utils.js
parent1d1483a9dc18d7acf4267727893a281b99645c02 (diff)
Add text/notes editor, using tui-text-editor
:D!
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
 }