|
| 1 | +# Encoding API |
| 2 | + |
| 3 | +Elide supports the standard JavaScript types [`TextEncoder`](#textencoder) and [`TextDecoder`](#textdecoder). These |
| 4 | +classes can be used to obtain byte-arrays of strings in different encodings, or to obtain strings from byte arrays with |
| 5 | +different encodings. |
| 6 | + |
| 7 | +## Supported Encodings |
| 8 | + |
| 9 | +Elide supports several encodings via the `TextEncoder` and `TextDecoder` constructors: |
| 10 | + |
| 11 | +| Support | Charset | Encoding (Label) | Notes | |
| 12 | +|-----------------------|------------------|------------------|------------------| |
| 13 | +| 🟢 Supported. | `ISO_8859_1` | `iso-8859-1` | | |
| 14 | +| 🟢 Supported. | `ASCII` | `ascii` | | |
| 15 | +| 🟢 Supported. | `UTF_8` | `utf-8` | Default encoding | |
| 16 | +| 🔴 Not supported yet. | `UTF_16` | `utf-16` | | |
| 17 | +| 🔴 Not supported yet. | `UTF_32` | `utf-32` | | |
| 18 | +| 🔴 Not supported yet. | (Other charsets) | | | |
| 19 | + |
| 20 | +## Encoding API | Classes |
| 21 | + |
| 22 | +[`TextEncoder`](#textencoder) Encode strings into byte array representations |
| 23 | +: 🟢 Supported. |
| 24 | + |
| 25 | +[`TextDecoder`](#textdecoder) Decode byte arrays into string representations |
| 26 | +: 🟢 Supported. |
| 27 | + |
| 28 | +## `TextEncoder` |
| 29 | + |
| 30 | +```Javascript |
| 31 | +// create an encoder; by default, it uses utf-8 |
| 32 | +const encoder = new TextEncoder(); |
| 33 | +encoder.encoding // returns 'utf-8' |
| 34 | +encoder.encode("hello") // returns `Uint8Array([...])` |
| 35 | +``` |
| 36 | + |
| 37 | +The `TextEncoder` class behaves very similarly to how it behaves in a browser, or under Node.js; the only difference is |
| 38 | +that Elide supports additional encodings, on top of the default, which is `utf-8`. |
| 39 | + |
| 40 | +> Read more about [`TextEncoder` on MDN](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder) |
| 41 | +
|
| 42 | +### `TextEncoder` Properties |
| 43 | + |
| 44 | +[`TextEncoder.encoding`](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder/encoding) |
| 45 | +: 🟢 Supported. |
| 46 | + |
| 47 | +### `TextEncoder` Methods |
| 48 | + |
| 49 | +[`TextEncoder.encode`](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder/encode) |
| 50 | +: 🟢 Supported. |
| 51 | + |
| 52 | +[`TextEncoder.encodeInto`](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder/encodeInto) |
| 53 | +: 🟢 Supported. |
| 54 | + |
| 55 | +## `TextDecoder` |
| 56 | + |
| 57 | +```Javascript |
| 58 | +// create a decoder; by default, it uses utf-8 |
| 59 | +const decoder = new TextDecoder(); |
| 60 | +decoder.encoding // returns 'utf-8' |
| 61 | +const input = new Uint8Array([104, 101, 108, 108, 111]); |
| 62 | +decoder.decode(input) // returns 'hello' |
| 63 | +``` |
| 64 | + |
| 65 | +The `TextDecoder` class behaves identically to how it behaves in a browser, or under Node.js. |
| 66 | + |
| 67 | +> Read more about [`TextDecoder` on MDN](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder) |
| 68 | +
|
| 69 | +### `TextDecoder` Properties |
| 70 | + |
| 71 | +[`TextDecoder.encoding`](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder/encoding) |
| 72 | +: 🟢 Supported. |
| 73 | + |
| 74 | +### `TextDecoder` Methods |
| 75 | + |
| 76 | +[`TextDecoder.decode`](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder/decode) |
| 77 | +: 🟢 Supported. |
0 commit comments