Use String.prototype.padStart() in renaming utility examples

This commit is contained in:
Martchus 2021-02-16 01:13:34 +01:00
parent 4d3eff3455
commit 7f3fc590d7
2 changed files with 4 additions and 9 deletions

View File

@ -52,12 +52,7 @@ function notNull(value) {
}
// returns the string representation of \a pos using at least as many digits as \a total has
function appropriateDigitCount(pos, total) {
var res = pos + ""
var count = (total + "").length
while (res.length < count) {
res = "0" + res
}
return res
return pos.toString().padStart(total.toString().length, "0")
}
// returns a copy of the specified \a name with characters that might be avoided in file names striped out
function validFileName(name) {

View File

@ -25,8 +25,8 @@ if (!fileInfo.hasAudioTracks && !fileInfo.hasVideoTracks) {
const fieldsToInclude = [tag.albumartist || tag.artist, tag.album, tag.trackPos || infoFromFileName.trackPos, tag.title || infoFromFileName.title]
let newName = ""
for (let field of fieldsToInclude) {
if (typeof field === "number") {
for (field = field + "", count = (tag.trackTotal + "").length; field.length < count; field = "0" + field);
if (typeof field === "number" && tag.trackTotal) {
field = field.toString().padStart(tag.trackTotal.toString().length, "0")
}
if (field && field.length !== 0) {
newName = newName.concat(newName.length === 0 ? "" : " - ", field)
@ -35,4 +35,4 @@ for (let field of fieldsToInclude) {
newName = newName.concat(".", fileInfo.suitableSuffix || fileInfo.currentSuffix)
// apply new name
tageditor.rename(newName)
tageditor.rename(newName)