Open normalize-git-branch-name in Script Kit

// Name: Normalize GIT branch name
// Description: Copy text and paste it to normalized GIT branch name
// Author: Marin Muštra
// LinkedIn: https://www.linkedin.com/in/marin-mustra
import '@johnlindquist/kit';
const delimiterChar = '-';
const illegalChar = '';
const mergableChars = [delimiterChar, illegalChar];
const shouldLowerCase = true;
const input = await paste();
let branchName = '';
// Sanitize references
// https://github.com/microsoft/vscode/blob/main/extensions/git/src/commands.ts#L2182
// https://github.com/gitextensions/gitextensions/blob/6eab7392839c4d103bad1581fba5eaf6f008d766/GitCommands/Git/GitBranchNameNormaliser.cs
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);