Skip to content

Instantly share code, notes, and snippets.

@annibal
Last active August 6, 2024 12:32
Show Gist options
  • Save annibal/06d5e30ee931a674d9a20eca4821e497 to your computer and use it in GitHub Desktop.
Save annibal/06d5e30ee931a674d9a20eca4821e497 to your computer and use it in GitHub Desktop.
Helper to replace regular characters of a string with their respective Unicode's 「Fullwidth Form 」 character

Transform FW Text into FW Text

Javascript helper function that replaces regular characters of a string with their respective unicode's 「Fullwidth Form 」 characters

Reference: https://symbl.cc/en/unicode/blocks/halfwidth-and-fullwidth-forms/#overview

Usage:

textRegular = "Sphinx Of Black Quartz - Judge My Vow";

textFW = replaceWithFullwidthChars(txt)
// 'Sphinx Of Black Quartz - Judge My Vow'

textFW2 = replaceWithFullwidthChars(txt.toUpperCase()).split("").join(" ")
// 'S P H I N X   O F   B L A C K   Q U A R T Z   -   J U D G E   M Y   V O W'

Another Usage:

const infix = replaceWithFullwidthChars("', '");
const prefix = replaceWithFullwidthChars("['");
const suffix = replaceWithFullwidthChars("']");
const textFW3 = [
  prefix,
  ...replaceWithFullwidthChars("86753-89").split("").join(infix),
  suffix
].join("");
// '['8', '6', '7', '5', '3', '-', '8', '9']'

Usage with prompt() and clipboard.writeText()

Plus an confirm() to select between the generated FW string, or that FW string but uppercase and with spaced characters:

const txtIn = prompt("Input text to FW")
const txtFw = replaceWithFullwidthChars(txtIn)
const txtFw2 = replaceWithFullwidthChars(txtIn.toUpperCase()).split("").join(" ")
const is1 = confirm(`🆗:\n${txtFw}\n\n🚫:\n${txtFw2}`)

document.body.focus();
oldFocus.focus();
setTimeout(() => {
  try {
    navigator.clipboard.writeText(is1 ? txtFw : txtFw2)
  } catch (e) {
    alert(`❌ Failed:\n${e}`)
  }
}, 50)
javascript:(()=>{const e=new Map("00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX YY ZZ aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz !! @@ ## $$ %% ^^ && ** (( )) -- __ ++ == ,, .. // || << >> ;; :: \"" '' \\\ ?? [[ ]] {{ }} %60` ~~".split(" ").map((e=>e.split("")))),t=t=>t.split("").map((t=>e.get(t)||t)).join(""),p=prompt("Input text to FW"),i=e=>%60<div style="display: flex; gap: 10px; margin: 5px; align-items: center; padding: 5px 10px"><button style="background: #d3efd2;line-height: 24px;border: 1px solid #999;border-radius: 4px;width: 36px;height: 36px;flex: 0 0 36px;display: flex;justify-content: center;align-items: center;" onclick="navigator.clipboard.writeText(this.parentElement.querySelector('p').innerText); this.parentElement.style.background='green';">📋</button><p style="line-height: 1; white-space: pre-wrap;">${e}</p></div>%60,n=(e,t)=>%60「${t?" ":""}${e}${t?" ":""}」%60;let o='<div style="position:fixed; z-index:9999999; left:50%; top:50px; background:#2e553c; color:#FFF; display:block; min-width: 200px; max-width: 80vw; transform: translateX(-50%); font-family: sans-serif; font-size: 18px; box-shadow: 0 0 80px 5px #FFF, 0 0 160px 5px #FFF, 0 0 240px 5px #FFF;"><div style="background: #30695f;font-size: 24px;display: flex;gap: 10px;align-items: center;border-bottom: 1px solid;padding: 5px 5px 5px 20px;"><span style="margin-right: auto; font-size: 11px;letter-spacing: 0.5px;font-family: monospace;opacity: 0.8;">FULLWIDTH TEXT RESULTS:</span><button style="background: #d3efd2;line-height: 24px;border: 1px solid #999;border-radius: 4px;width: 36px;height: 36px;display: flex;justify-content: center;align-items: center;" onclick="this.parentElement.parentElement.remove()">✖%EF%B8%8F</button></div>';o+=i(t(p)),o+=i(n(t(p))),o+=i(t(p).split("").join(" ")),o+=i(n(t(p).split("").join(" "),!0)),o+=i(t(p.toUpperCase())),o+=i(n(t(p.toUpperCase()))),o+=i(t(p.toUpperCase()).split("").join(" ")),o+=i(n(t(p.toUpperCase()).split("").join(" "),!0)),o+="</div>",document.body.innerHTML+=o})();
// EDIT
// Minified code, in javascript:(() => {})() syntax,
// To save as a bookmarklet:
javascript:(()=>{const t=document.activeElement;function n(t){const n=[];return t.split("").forEach((t=>{const e=o.get(t);e?n.push(e):n.push(t)})),n.join("")}const o=new Map("00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX YY ZZ aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz !! @@ ## $$ %% ^^ && ** (( )) -- __ ++ == ,, .. // || << >> ;; :: \"" '' \\\ ?? [[ ]] {{ }} `` ~~".split(" ").map((t=>t.split("")))),e=prompt("Input text to FW"),c=n(e),i=n(e.toUpperCase()).split("").join(" "),p=confirm(`🆗:\n${c}\n\n🚫:\n${i}`);document.body.focus(),t.focus(),setTimeout((()=>{try{navigator.clipboard.writeText(p?c:i)}catch(t){alert(`❌ Failed:\n${t}`)}}),50)})();
//
// also, smaller code for the character Map:
const mapCharToFW = new Map(
'00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX YY ZZ aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz !! @@ ## $$ %% ^^ && ** (( )) -- __ ++ == ,, .. // || << >> ;; :: "" \'' \\\ ?? [[ ]] {{ }} `` ~~'
.split(' ')
.map((x) => x.split(''))
)
// Reference:
// https://symbl.cc/en/unicode/blocks/halfwidth-and-fullwidth-forms/#overview
// Usage:
//
// const txt = "Sphinx Of Black Quartz - Judge My Vow";
//
// replaceWithFullwidthChars(txt)
// 'Sphinx Of Black Quartz - Judge My Vow'
//
// replaceWithFullwidthChars(txt.toUpperCase()).split("").join(" ")
// 'S P H I N X O F B L A C K Q U A R T Z - J U D G E M Y V O W'
// Another usage example:
//
// (() => {
// const infix = replaceWithFullwidthChars("', '");
// const prefix = replaceWithFullwidthChars("['");
// const suffix = replaceWithFullwidthChars("']");
// return [prefix, ...replaceWithFullwidthChars("86753-89").split("").join(infix), suffix].join("");
// })()
// '['8', '6', '7', '5', '3', '-', '8', '9']'
function replaceWithFullwidthChars(text) {
if (!(typeof text === 'string' || text instanceof String)) {
throw new TypeError("'text' must be a string")
}
const replacedArr = [];
text.split('').forEach((char) => {
const fwChar = mapCharToFW.get(char);
if (fwChar) {
replacedArr.push(fwChar);
} else {
replacedArr.push(char);
}
})
return replacedArr.join("")
}
const mapCharToFW = new Map([
[ "0", "0"], [ "1", "1"], [ "2", "2"],
[ "3", "3"], [ "4", "4"], [ "5", "5"],
[ "6", "6"], [ "7", "7"], [ "8", "8"],
[ "9", "9"],
[ "A", "A"], [ "B", "B"], [ "C", "C"],
[ "D", "D"], [ "E", "E"], [ "F", "F"],
[ "G", "G"], [ "H", "H"], [ "I", "I"],
[ "J", "J"], [ "K", "K"], [ "L", "L"],
[ "M", "M"], [ "N", "N"], [ "O", "O"],
[ "P", "P"], [ "Q", "Q"], [ "R", "R"],
[ "S", "S"], [ "T", "T"], [ "U", "U"],
[ "V", "V"], [ "W", "W"], [ "X", "X"],
[ "Y", "Y"], [ "Z", "Z"],
[ "a", "a"], [ "b", "b"], [ "c", "c"],
[ "d", "d"], [ "e", "e"], [ "f", "f"],
[ "g", "g"], [ "h", "h"], [ "i", "i"],
[ "j", "j"], [ "k", "k"], [ "l", "l"],
[ "m", "m"], [ "n", "n"], [ "o", "o"],
[ "p", "p"], [ "q", "q"], [ "r", "r"],
[ "s", "s"], [ "t", "t"], [ "u", "u"],
[ "v", "v"], [ "w", "w"], [ "x", "x"],
[ "y", "y"], [ "z", "z"],
[ "!", "!"], [ "@", "@"], [ "#", "#"],
[ "$", "$"], [ "%", "%"], [ "^", "^"],
[ "&", "&"], [ "*", "*"], [ "(", "("],
[ ")", ")"], [ "-", "-"], [ "_", "_"],
[ "+", "+"], [ "=", "="],
[ ",", ","], [ ".", "."], [ "/", "/"],
[ "|", "|"], [ "<", "<"], [ ">", ">"],
[ ";", ";"], [ ":", ":"], [ "\"", """],
[ "'", "'"], [ "\\", "\"], [ "?", "?"],
[ "[", "["], [ "]", "]"], [ "{", "{"],
[ "}", "}"], [ "`", "`"], [ "~", "~"]
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment