-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from rombat/fix/patch-matching-all-cases
fix: path matching to find tracks
- Loading branch information
Showing
8 changed files
with
1,197 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Normalize path to use forward slashes | ||
* @param {string} path | ||
* @returns {string} | ||
*/ | ||
const normalizePath = path => path.replace(/\\/g, '/'); | ||
|
||
/** | ||
* Get path segments | ||
* @param {string} path | ||
* @returns {string[]} | ||
*/ | ||
const getSegments = path => normalizePath(path).split('/'); | ||
|
||
/** | ||
* Find best match for a given MusicBee track | ||
* @param {object} mbTrack | ||
* @param {object[]} ndTracks | ||
* @returns {object|undefined} | ||
*/ | ||
const findBestMatch = (mbTrack, ndTracks) => { | ||
const mbTrackSegments = getSegments(mbTrack.filePath).reverse(); | ||
mbTrackSegments.unshift(mbTrack.filename); | ||
let bestMatch; | ||
let bestMatchScore = 0; | ||
|
||
ndTracks.filter(Boolean).forEach(ndTrack => { | ||
const ndTrackSegments = getSegments(ndTrack.toJSON().path).reverse(); | ||
let matchScore = 0; | ||
|
||
if (mbTrackSegments[0] !== ndTrackSegments[0]) { | ||
return; | ||
} | ||
for (let i = 1; i <= Math.min(mbTrackSegments.length, ndTrackSegments.length); i++) { | ||
if (mbTrackSegments[i] === ndTrackSegments[i]) { | ||
matchScore++; | ||
} else { | ||
break; | ||
} | ||
} | ||
if (matchScore > bestMatchScore) { | ||
bestMatchScore = matchScore; | ||
bestMatch = ndTrack; | ||
} | ||
}); | ||
|
||
return bestMatch; | ||
}; | ||
|
||
module.exports = { findBestMatch }; |
Oops, something went wrong.