Created
August 12, 2021 23:55
-
-
Save alexweininger/8ee33e9967e73e7a7ed3b76a3265a679 to your computer and use it in GitHub Desktop.
Trim quotes from a string
This file contains hidden or 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 trimQuotes(str: string): string { | |
str = str.trim(); | |
const arr = Array.from(str); | |
const quotationMark = '"'; | |
const first = arr.findIndex(char => char !== quotationMark); | |
const last = arr.reverse().findIndex(char => char !== quotationMark); | |
return (first === -1 && last === -1) ? '' : str.substring(first, str.length - last); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment