Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
export function pathQuote(pathstr: string): string {
if (!/[\s"'`]/.test(pathstr) && !/^[(<[{]|[)>\]}]$/.test(pathstr)) {
return pathstr;
}
if (/'/.test(pathstr)) {
if (/"/.test(pathstr)) {
if (/`/.test(pathstr)) {
const match = pathstr.match(/<+|>+/g);
if (match) {
const bracketLen =
match.reduce(
(maxlen, brackets) =>
Math.max(maxlen, brackets.length),
0,
) + 1;
return `${'<'.repeat(bracketLen)} ${pathstr} ${'>'.repeat(
bracketLen,
)}`;
} else {
return `<${pathstr}>`;
}
} else {
return `\`${pathstr}\``;
}
} else {
return `"${pathstr}"`;
}
}
return `'${pathstr}'`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment