-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenumerateTrackTitles.js
30 lines (28 loc) · 1.26 KB
/
enumerateTrackTitles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Renames all tracks using their absolute track number and a customizable prefix.
* @param {string} prefix Prefix of the track number.
* @param {Object} flags
* @param {boolean} flags.append Append the number (including the given prefix) to the current title.
* @param {boolean} flags.padNumbers Enable padding of numbers with leading zeros.
*/
export function enumerateTrackTitles(prefix = '', flags = {}) {
let $trackTitles = $('input.track-name');
/* setup padding of numbers to the same length */
const maxTrackDigits = $trackTitles.length.toString().length;
const paddedNumberFormat = new Intl.NumberFormat('en', { minimumIntegerDigits: maxTrackDigits });
$trackTitles.each((index, input) => {
let trackNumber = index + 1;
if (flags.padNumbers) {
trackNumber = paddedNumberFormat.format(trackNumber);
}
let newTitle = prefix + trackNumber;
if (flags.append) {
// append the "prefix" and the track number to the old title as a suffix
newTitle = (input.value + newTitle)
// ... if it already ended with an alternative punctuation mark, remove any following comma from the "prefix"
.replace(/([.!?]),/, '$1');
}
console.debug(newTitle);
$(input).val(newTitle);
}).trigger('change'); // necessary if the tracklist is not the active tab
}