« get me outta code hell

getNameWithoutTrackNumber algorithm improvements - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <towerofnix@gmail.com>2021-04-03 15:29:05 -0300
committer(quasar) nebula <towerofnix@gmail.com>2021-04-03 15:29:05 -0300
commit80577b066352fe2dfecd706302e183a5705c193b (patch)
tree79527b5a4d0093dbae1a2902d235a07fcd1e265c
parentfe603bc08f14d8fb86d1d832850ee8709f307f75 (diff)
getNameWithoutTrackNumber algorithm improvements
-rw-r--r--playlist-utils.js47
-rw-r--r--todo.txt3
2 files changed, 42 insertions, 8 deletions
diff --git a/playlist-utils.js b/playlist-utils.js
index 68cba56..1015748 100644
--- a/playlist-utils.js
+++ b/playlist-utils.js
@@ -521,24 +521,29 @@ function getTrackIndexInParent(track) {
 const nameWithoutTrackNumberSymbol = Symbol('Cached name without track number')
 function getNameWithoutTrackNumber(track) {
   // A "part" is a series of numeric digits, separated from other parts by
-  // whitespace and dashes, always preceding either the first non-numeric/
-  // separator character or (if there are no such characters) the first word
-  // (i.e. last whitespace).
+  // whitespace, dashes, and dots, always preceding either the first non-
+  // numeric/separator character or (if there are no such characters) the
+  // first word (i.e. last whitespace).
   const getNumberOfParts = ({ name }) => {
-    const match = name.match(/[^0-9\-\s]/)
+    name = name.replace(/^[\-\s.]+$/, '')
+    const match = name.match(/[^0-9\-\s.]/)
     if (match) {
-      name = name.slice(0, match.index)
+      if (match.index === 0) {
+        return 0
+      } else {
+        name = name.slice(0, match.index)
+      }
     } else if (name.includes(' ')) {
       name = name.slice(0, name.lastIndexOf(' '))
     } else {
       return 0
     }
-    name = name.replace(/[\-\s]+$/, '')
-    return name.split(/[\-\s]+/g).length
+    name = name.replace(/[\-\s.]+$/, '')
+    return name.split(/[\-\s.]+/g).length
   }
 
   const removeParts = (name, numParts) => {
-    const regex = new RegExp(`([0-9]+[\\-\\s]+){${numParts},${numParts}}`)
+    const regex = new RegExp(String.raw`[\-\s.]{0,}([0-9]+[\-\s.]+){${numParts},${numParts}}`)
     return track.name.replace(regex, '')
   }
 
@@ -710,3 +715,29 @@ module.exports = {
   isGroup, isTrack,
   isOpenable, isPlayable
 }
+
+if (require.main === module) {
+  {
+    const group = updateGroupFormat({items: [
+      {name: '- 1.01 Hello World 425', downloaderArg: 'x'},
+      {name: '1.02 Aww Yeah 371', downloaderArg: 'x'},
+      {name: ' 1.03 Here Goes 472', downloaderArg: 'x'}
+    ]})
+
+    for (let i = 0; i < group.items.length; i++) {
+      console.log(group.items[i].name, '->', getNameWithoutTrackNumber(group.items[i]))
+    }
+  }
+
+  {
+    const group = updateGroupFormat({items: [
+      {name: 'BAM #1', downloaderArg: 'x'},
+      {name: 'BAM #2', downloaderArg: 'x'},
+      {name: 'BAM #3.1 - no', downloaderArg: 'x'}
+    ]})
+
+    for (let i = 0; i < group.items.length; i++) {
+      console.log(group.items[i].name, '->', getNameWithoutTrackNumber(group.items[i]))
+    }
+  }
+}
diff --git a/todo.txt b/todo.txt
index 34784cc..ed7c830 100644
--- a/todo.txt
+++ b/todo.txt
@@ -569,10 +569,13 @@ TODO: Multipage context menu doesn't work well in the queue - fix this by
 
 TODO: Names like "10. Banana" don't get cropped! Dots/dashes *after* a number
       apparently don't get caught. Oops.
+      (Done!)
 
 TODO: "BAM #45.3 - no" displays as "BAM #45.no" in the queue? Seems wrong!
+      (Done!)
 
 TODO: "Challenge 1 (Tricks)" etc in FP World 3 are "Challenge (Tricks)"! Bad.
+      (Done!)
 
 TODO: Pressing next track (shift+N) on the last track should start the first
       track, if the queue is being looped.