sfletche
sfletche

Reputation: 49744

Polyfill for TextDecoder

I'm using fetch and have included the whatwg-fetch polyfill in my application.

I also employ TextDecoder as described in Jake Archibald's blog That's so fetch! to decode the response, but I'm not sure what polyfill to use.

(Currently Safari complains about ReferenceError: Can't find variable: TextDecoder)

I'm guessing there's a polyfill for TextDecoder, but I'm not finding it...

Upvotes: 16

Views: 20912

Answers (5)

CodingYourLife
CodingYourLife

Reputation: 8616

  • text-encoding: the currently no1 approved answer references a deprecated package
  • FastestSmallestTextEncoderDecoder: I ran into the same problems with this package as without this package. It is the smallest and fastest so you might consider it still. They have a nice comparison on why they are the best in their github readme.

My recommendation is text-encoding-shim: https://www.npmjs.com/package/text-encoding-shim

I tried a few things and this one eventually was not huge size and worked on usage of my library and in the jest tests. Maybe not the best but I wasted quite some time on my choice so maybe it helps someone.

Upvotes: 0

Felipe FC
Felipe FC

Reputation: 1

If all you want is to decode the array to utf8 simply add a function, no need of external libraries:

     function Utf8ArrayToStr(array) {
        var out, i, len, c;
        var char2, char3;

        out = "";
        len = array.length;
        i = 0;
        while(i < len) {
            c = array[i++];
            switch(c >> 4)
            {
                case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                // 0xxxxxxx
                out += String.fromCharCode(c);
                break;
                case 12: case 13:
                // 110x xxxx   10xx xxxx
                char2 = array[i++];
                out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
                break;
                case 14:
                    // 1110 xxxx  10xx xxxx  10xx xxxx
                    char2 = array[i++];
                    char3 = array[i++];
                    out += String.fromCharCode(((c & 0x0F) << 12) |
                        ((char2 & 0x3F) << 6) |
                        ((char3 & 0x3F) << 0));
                    break;
            }
        }

        return out;
    }

Upvotes: -1

Rosberg Linhares
Rosberg Linhares

Reputation: 3687

Nowadays you can use the FastestSmallestTextEncoderDecoder polyfill (1.5 KB), as recommended by the MDN website.

Upvotes: 8

Sphinxxx
Sphinxxx

Reputation: 13047

For quick client-side testing (without NPM):

<script src="https://unpkg.com/[email protected]/lib/encoding-indexes.js"></script>
<script src="https://unpkg.com/[email protected]/lib/encoding.js"></script>

..and then use TextDecoder like normal. MDN's example:

var win1251decoder = new TextDecoder('windows-1251');
var bytes = new Uint8Array([207, 240, 232, 226, 229, 242, 44, 32, 236, 232, 240, 33]);
console.log(win1251decoder.decode(bytes)); // Привет, мир!
<script src="https://unpkg.com/[email protected]/lib/encoding-indexes.js"></script>
<script src="https://unpkg.com/[email protected]/lib/encoding.js"></script>

Upvotes: 7

sfletche
sfletche

Reputation: 49744

I was able to solve this problem by using the text-encoding library

npm install text-encoding --save

along with

import encoding from 'text-encoding';
const decoder = new encoding.TextDecoder();

Upvotes: 15

Related Questions