JSFiddle - React, Tailwind, and code Playground
by Eugene Vilder
JavaScript
function byteLength(str) {
// returns the byte length of an utf8 string
var s = str.length;
for (var i=str.length-1; i>=0; i--) {
var code = str.charCodeAt(i);
if (code > 0x7f && code <= 0x7ff) s++;
else if (code > 0x7ff && code <= 0xffff) s+=2;
if (code >= 0xDC00 && code <= 0xDFFF) i--; //trail surrogate
}
return s;
}
const archive = (text) => {
const m = {};
for (let i in text) {
const c = text[i];
if (!m[c]) {
m[c] = [];
}
m[c].push(Number(i));
}
console.log(m);
return JSON.stringify(m);
}
const text = "6135gz7236gx5v238e7zh392874n6xb3847e6";
const output = archive(text);
console.log('----', byteLength(text), byteLength(output));