2024-11-09 18:36:24 -05:00
|
|
|
|
|
|
|
|
|
2024-10-03 15:50:53 -04:00
|
|
|
// utils.js
|
2024-11-09 18:36:24 -05:00
|
|
|
export async function generateUniqueTitle(baseTitle:string, existsCallback:CallableFunction) {
|
2024-10-03 15:50:53 -04:00
|
|
|
console.log(`generateUniqueTitle(${baseTitle})`);
|
|
|
|
|
let newTitle = baseTitle;
|
|
|
|
|
let counter = 1;
|
|
|
|
|
|
|
|
|
|
const titleRegex = /(.*?)(\((\d+)\))?$/;
|
|
|
|
|
const match = baseTitle.match(titleRegex);
|
|
|
|
|
if (match) {
|
|
|
|
|
baseTitle = match[1].trim();
|
|
|
|
|
counter = match[3] ? parseInt(match[3], 10) + 1 : 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the base title does not end with a parentheses expression, start with "(1)"
|
2024-11-09 18:36:24 -05:00
|
|
|
if (match != null && match[2] != null) {
|
2024-10-03 15:50:53 -04:00
|
|
|
newTitle = `${baseTitle} (${counter})`;
|
|
|
|
|
} else {
|
|
|
|
|
// else increment the counter in the parentheses expression as a first try
|
|
|
|
|
newTitle = `${baseTitle} (${counter})`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log(`first check of newTitle: ${newTitle}`);
|
|
|
|
|
|
|
|
|
|
while (await existsCallback(newTitle)) {
|
|
|
|
|
counter++;
|
|
|
|
|
newTitle = `${baseTitle} (${counter})`;
|
|
|
|
|
console.log(`trying newTitle: ${newTitle}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newTitle;
|
2024-11-09 18:36:24 -05:00
|
|
|
}
|