What is the proper way to "boldify" my text (convert normal characters) to corresponding special "Bold (serif)" symbols?
When I run it in my browser as boldify("Hello World!")
, I expect:
"𝐇𝐞𝐥𝐥𝐨 𝐖𝐨𝐫𝐥𝐝!"
But I get (When ran inside google developer console in Opera):
"𝐇𝐞𝐥𝐥𝐨 𝐖𝐨𝐫𝐥𝐝!function(_0x3298c7,_0x4d4daa){return this[_0x543a('0x9f')](new RegExp(_0x3298c7,'g'),_0x4d4daa);}"
What is this javascript function string that gets appended at the end? Why does that happen?
The function:
function boldify(txt) {
let input = txt;
let bold_input = "";
let font = {
'q': [55349, 56362],
'w': [55349, 56368],
'e': [55349, 56350],
'r': [55349, 56363],
't': [55349, 56365],
'z': [55349, 56371],
'u': [55349, 56366],
'i': [55349, 56354],
'o': [55349, 56360],
'p': [55349, 56361],
'a': [55349, 56346],
's': [55349, 56364],
'd': [55349, 56349],
'f': [55349, 56351],
'g': [55349, 56352],
'h': [55349, 56353],
'j': [55349, 56355],
'k': [55349, 56356],
'l': [55349, 56357],
'y': [55349, 56370],
'x': [55349, 56369],
'c': [55349, 56348],
'v': [55349, 56367],
'b': [55349, 56347],
'n': [55349, 56359],
'm': [55349, 56358],
'Q': [55349, 56336],
'W': [55349, 56342],
'E': [55349, 56324],
'R': [55349, 56337],
'T': [55349, 56339],
'Z': [55349, 56345],
'U': [55349, 56340],
'O': [55349, 56328],
'P': [55349, 56334],
'A': [55349, 56335],
'S': [55349, 56338],
'D': [55349, 56323],
'F': [55349, 56325],
'G': [55349, 56326],
'H': [55349, 56327],
'J': [55349, 56329],
'K': [55349, 56330],
'L': [55349, 56331],
'Y': [55349, 56344],
'X': [55349, 56343],
'C': [55349, 56322],
'V': [55349, 56341],
'B': [55349, 56321],
'N': [55349, 56333],
'M': [55349, 56332],
'1': [55349, 57295],
'2': [55349, 57296],
'3': [55349, 57297],
'4': [55349, 57298],
'5': [55349, 57299],
'6': [55349, 57300],
'7': [55349, 57301],
'8': [55349, 57302],
'9': [55349, 57303],
'0': [55349, 57294]
};
for (i in input) {
let char = input[i];
let char_code = font[char];
if (char_code !== undefined) {
let bold_char = String.fromCharCode(char_code[0], char_code[1]);
bold_input += bold_char;
} else {
bold_input += char;
}
}
return (bold_input);
}
console.log(boldify("Hello World!"))
Either don't use for(i in input)
construct - the most direct replacement will be for ... of
for (const char of input) { ... }
... or check that it only processes characters, and not the other props of the string with...
for (i in input) if (input.hasOwnProperty(i)) { ... }
Otherwise it'll start collecting functions added to String prototype. The very first function - formatUnicorn
in my case (Chrome) - is added to the output.
As a sidenote, the function can be greatly simplified. For example, the same number - 55349 - is used as the first character code for all the boldings; it's a waste of time and space adding it to each element of the 'mapping' array, as it can be inlined in String.fromCharCode(...)
call.
But even an array is a bit of waste here: if you pay attention, you'll see that there are just three sequences - one for a-z
, another for A-Z
, and final for 0-9
. These values should be stored, but not the intermediate ones.
Finally, you can target only the characters you have a replacement for with a simple regex.
For example (just a concept):
function _getAdditionalCode(char) {
const charCode = char.charCodeAt(0);
return charCode + (
charCode >= 97 ? 56346 - 97 : // 'a'..'z'
charCode >= 65 ? 56320 - 65 : // 'A'..'Z'
57294 - 48 // '0'..'9'
);
}
function boldify(txt) {
return txt.replace(/[a-zA-Z0-9]/g,
ch => String.fromCharCode(55349, _getAdditionalCode(ch)));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With