Files
UT-Registration-Plus/src/pages/background/lib/handleDuplicate.ts
doprz e987fbbe8e feat: add eslint-plugin-tsdoc (#430)
* feat: add eslint-plugin-tsdoc

* feat(doc): update current jsdoc to tsdoc specification

* chore: update deps

* feat: update warn to error for jsdoc and tsdoc

* chore(doc): lint
2024-11-16 00:20:36 -06:00

39 lines
1.4 KiB
TypeScript

import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
/**
* Duplicates a new schedule with the given name.
* Assumes that each schedule has a unique name.
*
* @param scheduleName - The name of the schedule to handle duplication for
* @param schedules - The list of UserSchedules to find existing names
* @returns The new name for the schedule, of the form `{baseName}({index})`
*/
export default async function handleDuplicate(scheduleName: string): Promise<string> {
const schedules = await UserScheduleStore.get('schedules');
// No point in checking for duplicates if the name is unique
if (schedules.find(schedule => schedule.name === scheduleName) === undefined) {
return scheduleName;
}
// Regex for matching `{baseName}({index})`, where match[1] = baseName, match[2] = (index)
const regex = /^(.+?)(\(\d+\))?$/;
// Extract base name and existing index
const match = scheduleName.match(regex);
const baseName = match && match[1] ? match[1].trim() : scheduleName;
// Extract number from parentheses and increment
let index = match && match[2] ? parseInt(match[2].slice(1, -1), 10) + 1 : 1;
let newName: string;
// Increment until an unused index is found
do {
newName = `${baseName} (${index++})`;
// eslint-disable-next-line @typescript-eslint/no-loop-func
} while (schedules.find(schedule => schedule.name === newName));
return newName;
}