« get me outta code hell

Make getItemPathString work the way it's meant to - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2017-09-13 15:14:39 -0300
committerFlorrie <towerofnix@gmail.com>2017-09-13 15:14:39 -0300
commit13a11233ea07155a8c491c82d6ab2873b2616bd4 (patch)
treeae79b4dcb2102d982872190f743ef335552910ff
parent3f9f999f417d16e70bb5d6aac1461babf71a22f0 (diff)
Make getItemPathString work the way it's meant to
-rw-r--r--src/playlist-utils.js105
-rw-r--r--todo.txt4
2 files changed, 102 insertions, 7 deletions
diff --git a/src/playlist-utils.js b/src/playlist-utils.js
index 479b0c5..25660a1 100644
--- a/src/playlist-utils.js
+++ b/src/playlist-utils.js
@@ -261,18 +261,28 @@ function getItemPath(item) {
 
 function getItemPathString(item) {
   // Gets the playlist path of an item by following its parent chain.
+  //
   // Returns a string in format Foo/Bar/Baz, where Foo and Bar are group
-  // names, and Baz is the name of the item. Unnamed parents (except for the
-  // top one) are considered to have the name '(Unnamed)'.
+  // names, and Baz is the name of the item.
+  //
+  // Unnamed parents are given the name '(Unnamed)'.
+  // Always ignores the root (top) group.
+  //
+  // Requires that the given item be from a playlist processed by
+  // updateGroupFormat.
+
+  const displayName = item.name || '(Unnamed)'
 
   if (item[parentSymbol]) {
-    if (item[parentSymbol].name || item[parentSymbol][parentSymbol]) {
-      return getItemPathString(item[parentSymbol]) + '/' + item.name
+    // Check if the parent is not the top level group.
+    // The top-level group is never included in the output path.
+    if (item[parentSymbol][parentSymbol]) {
+      return getItemPathString(item[parentSymbol]) + '/' + displayName
     } else {
-      return item.name
+      return displayName
     }
   } else {
-    return item.name || '(Unnamed)'
+    return displayName
   }
 }
 
@@ -335,8 +345,89 @@ module.exports = {
   filterPlaylistByPathString, filterGrouplikeByPath,
   removeGroupByPathString, removeGroupByPath,
   getPlaylistTreeString,
-  getItemPath, getItemPathString,
+  getItemPathString,
   parsePathString,
   isGroup, isTrack,
   safeUnlink
 }
+
+if (require.main === module) {
+  console.log('getItemPathString')
+
+  {
+    console.log('- (root with name) should output a/b/c/Foo')
+
+    const playlist = updateGroupFormat(
+      {name: 'root', items: [
+        {name: 'a', items: [
+          {name: 'b', items: [
+            {name: 'c', items: [
+              {name: 'Foo'}
+            ]}
+          ]}
+        ]}
+      ]}
+    )
+
+    const deepTrack = playlist.items[0].items[0].items[0].items[0]
+
+    const path = getItemPathString(deepTrack)
+    if (path === 'a/b/c/Foo') {
+      console.log('  ..good.')
+    } else {
+      console.log('  ..BAD! result:', path)
+    }
+  }
+
+  {
+    console.log('- (root without name) should output a/b/c/Foo')
+
+    const playlist = updateGroupFormat(
+      {items: [
+        {name: 'a', items: [
+          {name: 'b', items: [
+            {name: 'c', items: [
+              {name: 'Foo'}
+            ]}
+          ]}
+        ]}
+      ]}
+    )
+
+    const deepTrack = playlist.items[0].items[0].items[0].items[0]
+
+    const path = getItemPathString(deepTrack)
+    if (path === 'a/b/c/Foo') {
+      console.log('  ..good.')
+    } else {
+      console.log('  ..BAD! result:', path)
+    }
+  }
+
+  {
+    console.log('- (sub-group without name) should output a/b/(Unnamed)/c/Foo')
+
+    const playlist = updateGroupFormat(
+      {items: [
+        {name: 'a', items: [
+          {name: 'b', items: [
+            {items: [
+              {name: 'c', items: [
+                {name: 'Foo'}
+              ]}
+            ]}
+          ]}
+        ]}
+      ]}
+    )
+
+    const deepTrack = playlist.items[0].items[0].items[0].items[0].items[0]
+
+    const path = getItemPathString(deepTrack)
+    if (path === 'a/b/(Unnamed)/c/Foo') {
+      console.log('  ..good.')
+    } else {
+      console.log('  ..BAD! result:', path)
+    }
+  }
+}
diff --git a/todo.txt b/todo.txt
index 6395146..c97be0b 100644
--- a/todo.txt
+++ b/todo.txt
@@ -351,3 +351,7 @@ TODO: Some way to control how verbose http-music is.. most people probably
 
 TODO: Make the natural sort in crawl-local ignore capitalization case.
       (Done!)
+
+TODO: Make the '@ ...' part of track-info show the path to the track, rather
+      than the track name again (this is a bug!).
+      (Done!)