export const spliceTitleByLength = (text, length) => {
let textArray = Array.from(text);
let textLength = 0, i = 0;
for (i = 0; i < textArray.length; i++) {
const c = textArray[i];
textLength += encodeUtf8(c).length;
if (textLength > length) break;
}
let newTextArray = textArray.slice(0,i);
let newText = "";
for (let j = 0; j < newTextArray.length; j++)
{
newText += newTextArray[j] ;
}
return newText;
}
export const encodeUtf8 = (text) => {
const code = encodeURIComponent(text);
const bytes = [];
for (let i = 0; i < code.length; i++) {
const c = code.charAt(i);
if (c === '%') {
const hex = code.charAt(i + 1) + code.charAt(i + 2);
const hexVal = parseInt(hex, 16);
bytes.push(hexVal);
i += 2;
} else bytes.push(c.charCodeAt(0));
}
return bytes;
}