Created
October 26, 2023 04:07
-
-
Save peter-schmalfeldt/4c38b94cfc588b16535f6061acc0f37d to your computer and use it in GitHub Desktop.
Convert String to Emoji for SFCC XML Site Imports
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
/** | |
* Convert String to Emoji | |
* Most emoji's start with � | |
* If you have these in your SFCC Site Import XML File, you might get an import error. | |
* | |
* @usage convertStringToEmoji('��') => 🙂 | |
* @param {String} str Raw URL Encoded String | |
*/ | |
function convertStringToEmoji(str) { | |
// Regex matching either a surrogate or a character. | |
var re = /&#(\d+);|([^&])/g; | |
var match; | |
var charCodes = []; | |
// Find successive matches | |
while (match = re.exec(str)) { | |
if (match[1] != null) { | |
// Surrogate | |
charCodes.push(match[1]); | |
} | |
else { | |
// Unescaped character (assuming the code point is below 0x10000), | |
charCodes.push(match[2].charCodeAt(0)); | |
} | |
} | |
// Create string from UTF-16 code units. | |
return String.fromCharCode.apply(null, charCodes); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage Instructions
�
to see if that exists in the XML, if it does, that's an emoji��
��
to do a find and replaceconvertStringToEmoji('��')
�
This seems to fix all the issues with my imports. There are usually only a dozen or so, and most are the same, so it goes pretty quickly.
If you are using a browser, here is a clean function you can just paste in your Dev Console and press ENTER
ℹ️ TIP: In Google Chrome, you can use their native
copy
function to copy the output of a function to your clipboard, so you can do something like this: