From 7f3fc590d777f47bba302b5812ba9041793a49ad Mon Sep 17 00:00:00 2001 From: Martchus Date: Tue, 16 Feb 2021 01:13:34 +0100 Subject: [PATCH] Use String.prototype.padStart() in renaming utility examples --- resources/scripts/renamefiles/advanced-example.js | 7 +------ resources/scripts/renamefiles/simple-example.js | 6 +++--- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/resources/scripts/renamefiles/advanced-example.js b/resources/scripts/renamefiles/advanced-example.js index 024f252..81ca094 100644 --- a/resources/scripts/renamefiles/advanced-example.js +++ b/resources/scripts/renamefiles/advanced-example.js @@ -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) { diff --git a/resources/scripts/renamefiles/simple-example.js b/resources/scripts/renamefiles/simple-example.js index e628c72..c11c3ab 100644 --- a/resources/scripts/renamefiles/simple-example.js +++ b/resources/scripts/renamefiles/simple-example.js @@ -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) \ No newline at end of file +tageditor.rename(newName)