import '@johnlindquist/kit';
const delimiterChar = '-';
const illegalChar = '';
const mergableChars = [delimiterChar, illegalChar];
const shouldLowerCase = true;
const input = await paste();
let branchName = '';
const getSanitizedInput = () => {
const delimiterPattern = /\s+|_+|-+/g;
const illegalPattern = /^-+|^\.|\/\.|\.\.|~|\^|:|\/$|\.lock$|\.lock\/|\\|\*|\?|@{|^@$|\.$|\[|\]$|^\/|\/$/g;
const isInvalidChar =
(!delimiterPattern.test(delimiterChar) && illegalPattern.test(delimiterChar)) ||
(!delimiterPattern.test(illegalChar) && illegalPattern.test(illegalChar));
if (isInvalidChar) {
throw new Error('Invalid delimiter/illegal character!');
}
let sanitized = input.trim().replace(delimiterPattern, delimiterChar).replace(illegalPattern, illegalChar);
mergableChars?.forEach((char) => char && (sanitized = sanitized.replace(new RegExp(`\\${char}+`, 'g'), char)));
if (sanitized.length === 1) {
sanitized = sanitized.replace(illegalPattern, illegalChar);
}
return shouldLowerCase ? sanitized.toLowerCase() : sanitized;
};
try {
branchName = getSanitizedInput();
if (!branchName) {
throw new Error('Invalid input!');
}
} catch (error) {
const hint = `${error.message}`;
await editor({ hint, input, description: 'ERROR', readOnly: true, lineNumbers: 'on' });
exit();
}
await setSelectedText(branchName);