Created
October 9, 2019 13:45
-
-
Save sounisi5011/49beb9f9c9986329b19cb0613aa54652 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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