Skip to content

Instantly share code, notes, and snippets.

@idealwebsolutions
Created December 10, 2021 06:03
Show Gist options
  • Save idealwebsolutions/06935ca565a5f4352053c930cbcad0be to your computer and use it in GitHub Desktop.
Save idealwebsolutions/06935ca565a5f4352053c930cbcad0be to your computer and use it in GitHub Desktop.
some zero width functions
function textToZeroWidth (text, encoding = 'utf8') {
const buf = Buffer.from(text, encoding);
return buf.reduce((str, byte) => str + byte.toString(2).padStart(8, '0'), '')
.split('').map((binary) => binary === '0' ? '​' : '‌').join(' ');
};
function zeroWidthToText (zwChars) {
let message = '';
const b = zwChars.map((zw) => zw === '​' ? '0' : '1').join('');
for (let x = 0; x < Math.ceil(b.length / 8); x++) {
const start = x * 8;
const end = start + 8;
const byte = b.slice(start, end);
message += String.fromCharCode(parseInt(byte, 2).toString(10));
}
return message;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment