UNPKG

2.09 kBTypeScriptView Raw
1/**
2 * Performs the [decode](https://encoding.spec.whatwg.org/#decode) algorithm (in which any BOM will override
3 * the passed fallback encoding).
4 *
5 * @returns The resulting string.
6 * @throws `RangeError` if an unsupported encoding is provided for `fallbackEncodingName`.
7 *
8 * @example
9 * import { decode } from "whatwg-encoding";
10 *
11 * console.assert(decode(new Uint8Array([0x48, 0x69]), "UTF-8") === "Hi");
12 */
13export function decode(uint8Array: Uint8Array, fallbackEncodingName: string): string;
14/**
15 * Performs the [get an encoding](https://encoding.spec.whatwg.org/#concept-encoding-get) algorithm.
16 *
17 * @returns The resulting encoding's name, or `null` for failure.
18 *
19 * @example
20 * import { labelToName } from "whatwg-encoding";
21 *
22 * console.assert(labelToName("latin1") === "windows-1252");
23 * console.assert(labelToName(" CYRILLic ") === "ISO-8859-5");
24 */
25export function labelToName(label: string): string | null;
26/**
27 * Checks whether the encoding is one of [the encodings](https://encoding.spec.whatwg.org/#names-and-labels)
28 * of the Encoding Standard, _and_ is an encoding that this package can decode (via `iconv-lite`).
29 *
30 * @example
31 * import { isSupported } from "whatwg-encoding";
32 *
33 * console.assert(isSupported("IBM866") === true);
34 *
35 * // Not supported by the Encoding Standard
36 * console.assert(isSupported("UTF-32") === false);
37 *
38 * // In the Encoding Standard, but this package can't decode it
39 * console.assert(isSupported("x-mac-cyrillic") === false);
40 */
41export function isSupported(name: string): boolean;
42/**
43 * Sniffs the first 2–3 bytes of the supplied `Uint8Array`.
44 *
45 * @returns One of the encoding names if the appropriate BOM is present, or `null` if no BOM is present.
46 *
47 * @example
48 * import { getBOMEncoding } from "whatwg-encoding";
49 *
50 * console.assert(getBOMEncoding(new Uint8Array([0xFE, 0xFF])) === "UTF-16BE");
51 * console.assert(getBOMEncoding(new Uint8Array([0x48, 0x69])) === null);
52 */
53export function getBOMEncoding(uint8Array: Uint8Array): BOMEncoding | null;
54
55export type BOMEncoding = "UTF-16BE" | "UTF-16LE" | "UTF-8";