Apply Qt QML/JavaScript coding style to example.js

This commit is contained in:
Martchus 2018-11-15 21:34:26 +01:00
parent c0c413cddc
commit 39b22e70bf
1 changed files with 76 additions and 78 deletions

View File

@ -1,197 +1,195 @@
//
// This is an example script demonstrating how the renaming tool can be used. // This is an example script demonstrating how the renaming tool can be used.
//
//
// script configuration // script configuration
//
// specifies the separator between artist, album and track number // specifies the separator between artist, album and track number
var separator = ", "; var separator = ", "
// specifies the separator between title and other fields // specifies the separator between title and other fields
var lastSeparator = " - "; var lastSeparator = " - "
// specifies whether the artist name should be included // specifies whether the artist name should be included
var includeArtist = true; var includeArtist = true
// specifies whether the album name should be included // specifies whether the album name should be included
var includeAlbum = true; var includeAlbum = true
// specifies whether the title should be included // specifies whether the title should be included
var includeTitle = true; var includeTitle = true
// specifies the "distribution directory" // specifies the "distribution directory"
//var distDir = false; // don't move files around //var distDir = false; // don't move files around
var distDir = "/path/to/my/music-collection"; // move files to an appropriate subdirectory under this path var distDir = "/path/to/my/music-collection" // move files to an appropriate subdirectory under this path
// directory used to store collections which contain songs from multiple artists // directory used to store collections which contain songs from multiple artists
var collectionsDir = "collections"; var collectionsDir = "collections"
// directory used to store miscellaneous songs by miscellaneous artists // directory used to store miscellaneous songs by miscellaneous artists
var miscDir = "misc"; var miscDir = "misc"
// directory used for miscellaneous songs by specific artist // directory used for miscellaneous songs by specific artist
var miscAlbumDir = "misc"; var miscAlbumDir = "misc"
// condition to move files to miscDir // condition to move files to miscDir
var isMiscFile = function (tag) { return tag.comment === "misc"; }; var isMiscFile = function (tag) {
return tag.comment === "misc"
}
// condition to consider files part of a collection which contains songs from multiple artists // condition to consider files part of a collection which contains songs from multiple artists
var isPartOfCollection = function (tag) { return tag.comment === "collection"; } var isPartOfCollection = function (tag) {
return tag.comment === "collection"
// }
// helper functions // helper functions
//
// returns whether the specified \a value is not undefined and not an empty string. // returns whether the specified \a value is not undefined and not an empty string.
function notEmpty(value) { function notEmpty(value) {
return value !== undefined && value !== ""; return value !== undefined && value !== ""
} }
// returns whether the specified \a value is not undefined and not zero. // returns whether the specified \a value is not undefined and not zero.
function notNull(value) { function notNull(value) {
return value !== undefined && value !== 0; return value !== undefined && value !== 0
} }
// 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 + ""; var res = pos + ""
var count = (total + "").length; var count = (total + "").length
while (res.length < count) { while (res.length < count) {
res = "0" + res; res = "0" + res
} }
return 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) {
return name !== undefined ? name.replace(/[\/\\]/gi, " - ").replace(/[<>?!*|:\"\n\f\r]/gi, "") : ""; return name !== undefined ? name.replace(/[\/\\]/gi, " - ").replace(
/[<>?!*|:\"\n\f\r]/gi, "") : ""
} }
// returns a copy of the specified \a name with characters that might be avoided in directory names striped out. // returns a copy of the specified \a name with characters that might be avoided in directory names striped out.
function validDirectoryName(name) { function validDirectoryName(name) {
return name !== undefined ? name.replace(/[\/\\]/gi, " - ").replace(/[<>?!*|:\".\n\f\r]/gi, "") : ""; return name !== undefined ? name.replace(/[\/\\]/gi, " - ").replace(
/[<>?!*|:\".\n\f\r]/gi, "") : ""
} }
//
// actual script // actual script
//
// skip directories in this example script // skip directories in this example script
if (!tageditor.isFile) { if (!tageditor.isFile) {
tageditor.skip(); tageditor.skip()
return; return
} }
// parse file using the built-in parseFileInfo function // parse file using the built-in parseFileInfo function
var fileInfo = tageditor.parseFileInfo(tageditor.currentPath); var fileInfo = tageditor.parseFileInfo(tageditor.currentPath)
var tag = fileInfo.tag; var tag = fileInfo.tag
// deduce title and track number from the file name using the built-in parseFileName function (as fallback if tags missing) // deduce title and track number from the file name using the built-in parseFileName function (as fallback if tags missing)
var infoFromFileName = tageditor.parseFileName(fileInfo.currentBaseName); var infoFromFileName = tageditor.parseFileName(fileInfo.currentBaseName)
// skip hidden and "desktop.ini" files // skip hidden and "desktop.ini" files
if (fileInfo.currentName.indexOf(".") === 0 || fileInfo.currentName === "desktop.ini") { if (fileInfo.currentName.indexOf(".") === 0
tageditor.skip(); || fileInfo.currentName === "desktop.ini") {
return; tageditor.skip()
return
} }
}
// skip files which don't contain audio or video tracks // skip files which don't contain audio or video tracks
if (!fileInfo.hasAudioTracks && !fileInfo.hasVideoTracks) { if (!fileInfo.hasAudioTracks && !fileInfo.hasVideoTracks) {
tageditor.skip(); tageditor.skip()
return; return
} }
// filter backup and temporary files by putting them in a separate directory // filter backup and temporary files by putting them in a separate directory
if (fileInfo.currentSuffix === "bak") { if (fileInfo.currentSuffix === "bak") {
tageditor.move("backups"); tageditor.move("backups")
return; return
} }
if (fileInfo.currentSuffix === "tmp") { if (fileInfo.currentSuffix === "tmp") {
tageditor.move("temp"); tageditor.move("temp")
return; return
} }
// define an array for the fields to be joined later // define an array for the fields to be joined later
var fields = []; var fields = []
// get the artist, remove invalid characters and add it to fields array // get the artist, remove invalid characters and add it to fields array
var artist = validFileName(tag.artist); var artist = validFileName(tag.artist)
if (includeArtist && !isPartOfCollection(tag) && notEmpty(artist)) { if (includeArtist && !isPartOfCollection(tag) && notEmpty(artist)) {
fields.push(artist); fields.push(artist)
} }
// get the album and remove invalid characters and add it to fields array // get the album and remove invalid characters and add it to fields array
var album = validFileName(tag.album); var album = validFileName(tag.album)
if (includeAlbum && notEmpty(tag.album)) { if (includeAlbum && notEmpty(tag.album)) {
fields.push(album); fields.push(album)
} }
// get the track/disk position and add it to fields array // get the track/disk position and add it to fields array
// use the value from the tag if possible; otherwise the value deduced from the filename // use the value from the tag if possible; otherwise the value deduced from the filename
if (notNull(tag.trackPos)) { if (notNull(tag.trackPos)) {
var pos = []; var pos = []
// push the disk position // push the disk position
if (notNull(tag.diskPos) && notNull(tag.diskTotal) && tag.diskTotal >= 2) { if (notNull(tag.diskPos) && notNull(tag.diskTotal) && tag.diskTotal >= 2) {
pos.push(appropriateDigitCount(tag.diskPos, tag.diskTotal)); pos.push(appropriateDigitCount(tag.diskPos, tag.diskTotal))
} }
// push the track count // push the track count
if (notNull(tag.trackTotal)) { if (notNull(tag.trackTotal)) {
pos.push(appropriateDigitCount(tag.trackPos, tag.trackTotal)); pos.push(appropriateDigitCount(tag.trackPos, tag.trackTotal))
} else { } else {
pos.push(appropriateDigitCount(tag.trackPos, 10)); pos.push(appropriateDigitCount(tag.trackPos, 10))
} }
fields.push(pos.join("-")); fields.push(pos.join("-"))
} else if (notNull(infoFromFileName.trackPos)) { } else if (notNull(infoFromFileName.trackPos)) {
fields.push(appropriateDigitCount(infoFromFileName.trackPos, 10)); fields.push(appropriateDigitCount(infoFromFileName.trackPos, 10))
} }
// join the first part of the new name // join the first part of the new name
var newName = fields.join(separator); var newName = fields.join(separator)
// get the title and append it // get the title and append it
var title = validFileName(tag.title); var title = validFileName(tag.title)
if (includeTitle) { if (includeTitle) {
// use value from file name if the tag has no title information // use value from file name if the tag has no title information
if (!notEmpty(title)) { if (!notEmpty(title)) {
title = validFileName(infoFromFileName.title); title = validFileName(infoFromFileName.title)
} }
if (newName.length > 0) { if (newName.length > 0) {
newName = newName.concat(lastSeparator, title); newName = newName.concat(lastSeparator, title)
} else { } else {
newName = newName.concat(title); newName = newName.concat(title)
} }
} }
// append an appropriate suffix // append an appropriate suffix
var suffix = ""; var suffix = ""
if (notEmpty(fileInfo.suitableSuffix)) { if (notEmpty(fileInfo.suitableSuffix)) {
// get a suitable suffix from the file info object if available // get a suitable suffix from the file info object if available
suffix = fileInfo.suitableSuffix; suffix = fileInfo.suitableSuffix
} else if (notEmpty(fileInfo.currentSuffix)) { } else if (notEmpty(fileInfo.currentSuffix)) {
// or just use the current suffix otherwise // or just use the current suffix otherwise
suffix = fileInfo.currentSuffix; suffix = fileInfo.currentSuffix
} }
if (notEmpty(suffix)) { if (notEmpty(suffix)) {
newName = newName.concat(".", suffix); newName = newName.concat(".", suffix)
} }
// apply new name // apply new name
tageditor.rename(newName); tageditor.rename(newName)
// set the distribution directory // set the distribution directory
if (!distDir) { if (!distDir) {
return; return
} }
var path = [distDir]; var path = [distDir]
var artist = validDirectoryName(tag.artist); var artist = validDirectoryName(tag.artist)
if (isPartOfCollection(tag)) { if (isPartOfCollection(tag)) {
path.push(collectionsDir); path.push(collectionsDir)
} else if (isMiscFile(tag)) { } else if (isMiscFile(tag)) {
path.push(miscDir); path.push(miscDir)
} else if (notEmpty(artist)) { } else if (notEmpty(artist)) {
path.push(artist); path.push(artist)
} else { } else {
path.push(misc); path.push(misc)
} }
var album = validDirectoryName(tag.album); var album = validDirectoryName(tag.album)
if (notEmpty(album)) { if (notEmpty(album)) {
if (notEmpty(tag.year)) { if (notEmpty(tag.year)) {
path.push([tag.year.split("-")[0], album].join(" - ")); path.push([tag.year.split("-")[0], album].join(" - "))
} else { } else {
path.push(album); path.push(album)
} }
} else if (notEmpty(artist) && !isMiscFile(tag)) { } else if (notEmpty(artist) && !isMiscFile(tag)) {
path.push(miscAlbumDir); path.push(miscAlbumDir)
} }
if (tag.diskTotal >= 2) { if (tag.diskTotal >= 2) {
path.push("Disk " + appropriateDigitCount(tag.diskPos, tag.diskTotal)); path.push("Disk " + appropriateDigitCount(tag.diskPos, tag.diskTotal))
} }
// apply new relative directory // apply new relative directory
tageditor.move(path.join("/")); tageditor.move(path.join("/"))