summaryrefslogtreecommitdiffstats
path: root/src/text.js
blob: d267817107a73a5eb53cf6b6b4a496917904a562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class _my_TextEncoder {
    encode(text) {
        text = unescape(encodeURIComponent(text));

        var bytes = new Array( text.length );

        for (var b = 0; b < text.length; b++) {
            bytes[b] = text.charCodeAt(b);
        }

        return new Uint8Array(bytes);
    }
}

class _my_TextDecoder {
    decode(bytes) {
        return decodeURIComponent( escape( String.fromCharCode.apply(String, bytes) ) );
    }
}

var Zmodem = module.exports;

/**
 * A limited-use compatibility shim for TextEncoder and TextDecoder.
 * Useful because both Edge and node.js still lack support for these
 * as of October 2017.
 *
 * @exports Text
 */
Zmodem.Text = {
    Encoder: (typeof TextEncoder !== "undefined") ? TextEncoder : _my_TextEncoder,
    Decoder: (typeof TextDecoder !== "undefined") ? TextDecoder : _my_TextDecoder,
};