From 80577b066352fe2dfecd706302e183a5705c193b Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 3 Apr 2021 15:29:05 -0300 Subject: getNameWithoutTrackNumber algorithm improvements --- playlist-utils.js | 47 +++++++++++++++++++++++++++++++++++++++-------- todo.txt | 3 +++ 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. -- cgit 1.3.0-6-gf8a5