| 1 | # Decode According to the WHATWG Encoding Standard
|
|---|
| 2 |
|
|---|
| 3 | This package provides a thin layer on top of [iconv-lite](https://github.com/ashtuchkin/iconv-lite) which makes it expose some of the same primitives as the [Encoding Standard](https://encoding.spec.whatwg.org/).
|
|---|
| 4 |
|
|---|
| 5 | ```js
|
|---|
| 6 | const whatwgEncoding = require("whatwg-encoding");
|
|---|
| 7 |
|
|---|
| 8 | console.assert(whatwgEncoding.labelToName("latin1") === "windows-1252");
|
|---|
| 9 | console.assert(whatwgEncoding.labelToName(" CYRILLic ") === "ISO-8859-5");
|
|---|
| 10 |
|
|---|
| 11 | console.assert(whatwgEncoding.isSupported("IBM866") === true);
|
|---|
| 12 |
|
|---|
| 13 | // Not supported by the Encoding Standard
|
|---|
| 14 | console.assert(whatwgEncoding.isSupported("UTF-32") === false);
|
|---|
| 15 |
|
|---|
| 16 | // In the Encoding Standard, but this package can't decode it
|
|---|
| 17 | console.assert(whatwgEncoding.isSupported("x-mac-cyrillic") === false);
|
|---|
| 18 |
|
|---|
| 19 | console.assert(whatwgEncoding.getBOMEncoding(new Buffer([0xFE, 0xFF])) === "UTF-16BE");
|
|---|
| 20 | console.assert(whatwgEncoding.getBOMEncoding(new Buffer([0x48, 0x69])) === null);
|
|---|
| 21 |
|
|---|
| 22 | console.assert(whatwgEncoding.decode(new Buffer([0x48, 0x69]), "UTF-8") === "Hi");
|
|---|
| 23 | ```
|
|---|
| 24 |
|
|---|
| 25 | ## API
|
|---|
| 26 |
|
|---|
| 27 | - `decode(buffer, fallbackEncodingName)`: performs the [decode](https://encoding.spec.whatwg.org/#decode) algorithm (in which any BOM will override the passed fallback encoding), and returns the resulting string
|
|---|
| 28 | - `labelToName(label)`: performs the [get an encoding](https://encoding.spec.whatwg.org/#concept-encoding-get) algorithm and returns the resulting encoding's name, or `null` for failure
|
|---|
| 29 | - `isSupported(name)`: returns whether the encoding is one of [the encodings](https://encoding.spec.whatwg.org/#names-and-labels) of the Encoding Standard, _and_ is an encoding that this package can decode (via iconv-lite)
|
|---|
| 30 | - `getBOMEncoding(buffer)`: sniffs the first 2–3 bytes of the supplied `Buffer`, returning one of the encoding names `"UTF-8"`, `"UTF-16LE"`, or `"UTF-16BE"` if the appropriate BOM is present, or `null` if no BOM is present
|
|---|
| 31 |
|
|---|
| 32 | ## Unsupported encodings
|
|---|
| 33 |
|
|---|
| 34 | Since we rely on iconv-lite, we are limited to support only the encodings that they support. Currently we are missing support for:
|
|---|
| 35 |
|
|---|
| 36 | - ISO-2022-JP
|
|---|
| 37 | - ISO-8859-8-I
|
|---|
| 38 | - replacement
|
|---|
| 39 | - x-mac-cyrillic
|
|---|
| 40 | - x-user-defined
|
|---|
| 41 |
|
|---|
| 42 | Passing these encoding names will return `false` when calling `isSupported`, and passing any of the possible labels for these encodings to `labelToName` will return `null`.
|
|---|
| 43 |
|
|---|
| 44 | ## Credits
|
|---|
| 45 |
|
|---|
| 46 | This package was originally based on the excellent work of [@nicolashenry](https://github.com/nicolashenry), [in jsdom](https://github.com/tmpvar/jsdom/blob/7ce11776ce161e8d5921a7a183585327400f786b/lib/jsdom/living/helpers/encoding.js). It has since been pulled out into this separate package.
|
|---|
| 47 |
|
|---|
| 48 | ## Alternatives
|
|---|
| 49 |
|
|---|
| 50 | If you are looking for a JavaScript implementation of the Encoding Standard's `TextEncoder` and `TextDecoder` APIs, you'll want [@inexorabletash](https://github.com/inexorabletash)'s [text-encoding](https://github.com/inexorabletash/text-encoding) package.
|
|---|