Skip to content

Instantly share code, notes, and snippets.

@Spuffynism
Last active July 8, 2018 00:05
Show Gist options
  • Select an option

  • Save Spuffynism/d48091f0d478712b83d366c89cfe67d2 to your computer and use it in GitHub Desktop.

Select an option

Save Spuffynism/d48091f0d478712b83d366c89cfe67d2 to your computer and use it in GitHub Desktop.
Squeeze utf-8 characters in half their space
let squeeze = s => s.replace(/../g,c=>String.fromCharCode(c[a='charCodeAt']()|c[a](1)<<7));
let unsqueeze = s => s.replace(/./g,c=>String.fromCharCode((c=c.charCodeAt())&127,c>>7));
console.log(unsqueeze("㟦ᑲᝣ㓷㩤㹨㒽Ჽ㒻ᚭᒻ᝸㓦㙬㋒㩣ᨨᠰ㒫ᢪᠰ⦫㨨ᔩᠳᘰᠴᘰᠵ᤬ᠰᶩ") ===
"for(c.width|=i=9;i--;)x.fillRect(400+i*100+S(t)*300,400,50,200);");
console.log(squeeze("for(c.width|=i=9;i--;)x.fillRect(400+i*100+S(t)*300,400,50,200);") ===
"㟦ᑲᝣ㓷㩤㹨㒽Ჽ㒻ᚭᒻ᝸㓦㙬㋒㩣ᨨᠰ㒫ᢪᠰ⦫㨨ᔩᠳᘰᠴᘰᠵ᤬ᠰᶩ");
console.log("㟦ᑲᝣ㓷㩤㹨㒽Ჽ㒻ᚭᒻ᝸㓦㙬㋒㩣ᨨᠰ㒫ᢪᠰ⦫㨨ᔩᠳᘰᠴᘰᠵ᤬ᠰᶩ".length === 32);
console.log("for(c.width|=i=9;i--;)x.fillRect(400+i*100+S(t)*300,400,50,200);".length === 64);
// codegolf example
eval("㟦ᑲᝣ㓷㩤㹨㒽Ჽ㒻ᚭᒻ᝸㓦㙬㋒㩣ᨨᠰ㒫ᢪᠰ⦫㨨ᔩᠳᘰᠴᘰᠵ᤬ᠰᶩ".replace(/./g,c=>String.fromCharCode((c=c.charCodeAt())&127,c>>7)))
eval([..."㟦ᑲᝣ㓷㩤㹨㒽Ჽ㒻ᚭᒻ᝸㓦㙬㋒㩣ᨨᠰ㒫ᢪᠰ⦫㨨ᔩᠳᘰᠴᘰᠵ᤬ᠰᶩ"].map(c=>String.fromCharCode((c=c.charCodeAt())&127,c>>7)).join``)
eval([].map.call("㟦ᑲᝣ㓷㩤㹨㒽Ჽ㒻ᚭᒻ᝸㓦㙬㋒㩣ᨨᠰ㒫ᢪᠰ⦫㨨ᔩᠳᘰᠴᘰᠵ᤬ᠰᶩ",c=>String.fromCharCode((c=c.charCodeAt())&127,c>>7)).join``)
eval([..." 㟦ᑲᝣ㓷㩤㹨㒽Ჽ㒻ᚭᒻ᝸㓦㙬㋒㩣ᨨᠰ㒫ᢪᠰ⦫㨨ᔩᠳᘰᠴᘰᠵ᤬ᠰᶩ"].reduce((a,c)=>a+=String.fromCharCode((c=c.charCodeAt())&127,c>>7)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment