I need a function that can check if a file or blob object is valid UTF-8. I can get the text and check for � characters, but if the string has that character to begin with, the function would mark it as invalid.
function isUTF8(blob) {
return new Promise(async resolve => {
const text = await blob.text();
resolve(!~text.indexOf("�"));
});
}
// "�" is valid utf-8 but the function returns false
isUTF8(new Blob(["�"])).then(console.log);
// returns true
isUTF8(new Blob(["example"])).then(console.log);