Use String.prototype.padStart() in renaming utility examples
This commit is contained in:
parent
4d3eff3455
commit
7f3fc590d7
|
@ -52,12 +52,7 @@ function notNull(value) {
|
||||||
}
|
}
|
||||||
// returns the string representation of \a pos using at least as many digits as \a total has
|
// returns the string representation of \a pos using at least as many digits as \a total has
|
||||||
function appropriateDigitCount(pos, total) {
|
function appropriateDigitCount(pos, total) {
|
||||||
var res = pos + ""
|
return pos.toString().padStart(total.toString().length, "0")
|
||||||
var count = (total + "").length
|
|
||||||
while (res.length < count) {
|
|
||||||
res = "0" + res
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
}
|
}
|
||||||
// returns a copy of the specified \a name with characters that might be avoided in file names striped out
|
// returns a copy of the specified \a name with characters that might be avoided in file names striped out
|
||||||
function validFileName(name) {
|
function validFileName(name) {
|
||||||
|
|
|
@ -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]
|
const fieldsToInclude = [tag.albumartist || tag.artist, tag.album, tag.trackPos || infoFromFileName.trackPos, tag.title || infoFromFileName.title]
|
||||||
let newName = ""
|
let newName = ""
|
||||||
for (let field of fieldsToInclude) {
|
for (let field of fieldsToInclude) {
|
||||||
if (typeof field === "number") {
|
if (typeof field === "number" && tag.trackTotal) {
|
||||||
for (field = field + "", count = (tag.trackTotal + "").length; field.length < count; field = "0" + field);
|
field = field.toString().padStart(tag.trackTotal.toString().length, "0")
|
||||||
}
|
}
|
||||||
if (field && field.length !== 0) {
|
if (field && field.length !== 0) {
|
||||||
newName = newName.concat(newName.length === 0 ? "" : " - ", field)
|
newName = newName.concat(newName.length === 0 ? "" : " - ", field)
|
||||||
|
|
Loading…
Reference in New Issue