-
-
Save fabiolimace/d759bd2cf94d70b908d890c96020e313 to your computer and use it in GitHub Desktop.
Normalize all UTF quotes in Javascript
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
/** | |
* Will normalize quotes in a given string. There are many variations of quotes | |
* in the unicode character set, this function attempts to convert any variation | |
* of quote to the standard Quotation Mark - U+0022 Standard Universal: " | |
* | |
* @param {string} str The string to normalize | |
* @return {string} Normalized string. | |
* @see https://unicode-table.com/en/sets/quotation-marks/ | |
*/ | |
helpers.stdQuote = (str) => { | |
const allQuotes = [ | |
'“', // U+201c | |
'”', // U+201d | |
'«', // U+00AB | |
'»', // U+00BB | |
'„', // U+201E | |
'“', // U+201C | |
'‟', // U+201F | |
'”', // U+201D | |
'❝', // U+275D | |
'❞', // U+275E | |
'〝', // U+301D | |
'〞', // U+301E | |
'〟', // U+301F | |
'"', // U+FF02 | |
]; | |
const stdQuote = '"'; // U+0022 | |
const normalized = allQuotes.reduce((strNorm, quoteChar) => { | |
// eslint-disable-next-line security/detect-non-literal-regexp | |
const re = new RegExp(quoteChar, 'g'); | |
return strNorm.replace(re, stdQuote); | |
}, str); | |
return normalized; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment