- Notifications
You must be signed in to change notification settings - Fork10
Decode strings according to the WHATWG Encoding Standard
License
jsdom/whatwg-encoding
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This package provides a thin layer on top oficonv-lite which makes it expose some of the same primitives as theEncoding Standard.
constwhatwgEncoding=require("whatwg-encoding");console.assert(whatwgEncoding.labelToName("latin1")==="windows-1252");console.assert(whatwgEncoding.labelToName(" CYRILLic ")==="ISO-8859-5");console.assert(whatwgEncoding.isSupported("IBM866")===true);// Not supported by the Encoding Standardconsole.assert(whatwgEncoding.isSupported("UTF-32")===false);// In the Encoding Standard, but this package can't decode itconsole.assert(whatwgEncoding.isSupported("x-mac-cyrillic")===false);console.assert(whatwgEncoding.getBOMEncoding(newUint8Array([0xFE,0xFF]))==="UTF-16BE");console.assert(whatwgEncoding.getBOMEncoding(newUint8Array([0x48,0x69]))===null);console.assert(whatwgEncoding.decode(newUint8Array([0x48,0x69]),"UTF-8")==="Hi");
decode(uint8Array, fallbackEncodingName)
: performs thedecode algorithm (in which any BOM will override the passed fallback encoding), and returns the resulting stringlabelToName(label)
: performs theget an encoding algorithm and returns the resulting encoding's name, ornull
for failureisSupported(name)
: returns whether the encoding is one ofthe encodings of the Encoding Standard,and is an encoding that this package can decode (via iconv-lite)getBOMEncoding(uint8Array)
: sniffs the first 2–3 bytes of the suppliedUint8Array
, returning one of the encoding names"UTF-8"
,"UTF-16LE"
, or"UTF-16BE"
if the appropriate BOM is present, ornull
if no BOM is present
Since we rely on iconv-lite, we are limited to support only the encodings that they support. Currently we are missing support for:
- ISO-2022-JP
- ISO-8859-8-I
- replacement
- x-mac-cyrillic
- x-user-defined
Passing these encoding names will returnfalse
when callingisSupported
, and passing any of the possible labels for these encodings tolabelToName
will returnnull
.
This package was originally based on the excellent work of@nicolashenry,in jsdom. It has since been pulled out into this separate package.
If you are looking for a JavaScript implementation of the Encoding Standard'sTextEncoder
andTextDecoder
APIs, you'll want@inexorabletash'stext-encoding package. Node.js also has thembuilt-in.
About
Decode strings according to the WHATWG Encoding Standard