Skip to content

Instantly share code, notes, and snippets.

@sounisi5011
Created October 9, 2019 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sounisi5011/49beb9f9c9986329b19cb0613aa54652 to your computer and use it in GitHub Desktop.
Save sounisi5011/49beb9f9c9986329b19cb0613aa54652 to your computer and use it in GitHub Desktop.
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