I\'m using the Zemanta API, which accepts up to 8 KB of text per call. I\'m extracting the text to send to Zemanta from Web pages using JavaScript, so I\'m looking for a fun
You can do something like this since unescape is partially deprecated
function byteCount( string ) {
// UTF8
return encodeURI(string).split(/%..|./).length - 1;
}
function truncateByBytes(string, byteSize) {
// UTF8
if (byteCount(string) > byteSize) {
const charsArray = string.split('');
let truncatedStringArray = [];
let bytesCounter = 0;
for (let i = 0; i < charsArray.length; i++) {
bytesCounter += byteCount(charsArray[i]);
if (bytesCounter <= byteSize) {
truncatedStringArray.push(charsArray[i]);
} else {
break;
}
}
return truncatedStringArray.join('');
}
return string;
}