Convert String to ISO-8859-1

722 Views Asked by At

In my JavaScript app, I want to give the user a button to download data in either UTF-8 or in ISO-8859-1 (Latin-1).

The only working solution I have found so far is using iconv-lite.

var iconv = require('iconv-lite');
iconv.encode(utf8String, 'iso-8859-1');

However the library is very large and I would be happy to have a smaller solution. Do you have any idea how to achieve that or get only the relevant part from iconv-lite?

Here is what I've tried so far:

Using TextEncoder

var utf8Array = new TextEncoder().encode(utf8String);
var iso88591String = new TextDecoder("iso-8859-1").decode(utf8Array);

I still get an UTF-8 String as a result.

Using escape

decodeURIComponent(escape(utf8String));

Throws an error "malformed URI"

Lib Utf form here: https://github.com/DesWurstes/utf.js

console.log(ToString(FromUTF8(utf8)));

Returns binary data.

0

There are 0 best solutions below