Build in the behavior to ignore decodeUTF8 errors

Makes the code clearer and more explicit in intent.
This commit is contained in:
Samuel Mannehed
2019-12-23 10:25:02 +01:00
committed by Lauri Kasanen
parent c3ef9ff557
commit ce94d92e18
2 changed files with 15 additions and 12 deletions

View File

@@ -7,8 +7,19 @@
*/
// Decode from UTF-8
export function decodeUTF8(utf8string) {
return decodeURIComponent(escape(utf8string));
export function decodeUTF8(utf8string, allowLatin1=false) {
try {
return decodeURIComponent(escape(utf8string));
} catch (e) {
if (e instanceof URIError) {
if (allowLatin1) {
// If we allow Latin1 we can ignore any decoding fails
// and in these cases return the original string
return utf8string;
}
}
throw e;
}
}
// Encode to UTF-8