|
| 1 | +/** |
| 2 | + * Decode / encode the legacy multi-byte encodings according to the |
| 3 | + * [Encoding standard](https://encoding.spec.whatwg.org/) |
| 4 | + * ([§10](https://encoding.spec.whatwg.org/#legacy-multi-byte-chinese-(simplified)-encodings), |
| 5 | + * [§11](https://encoding.spec.whatwg.org/#legacy-multi-byte-chinese-(traditional)-encodings), |
| 6 | + * [§12](https://encoding.spec.whatwg.org/#legacy-multi-byte-japanese-encodings), |
| 7 | + * [§13](https://encoding.spec.whatwg.org/#legacy-multi-byte-korean-encodings)). |
| 8 | + * |
| 9 | + * ```js |
| 10 | + * import { createMultibyteDecoder, createMultibyteEncoder } from '@exodus/bytes/multi-byte.js' |
| 11 | + * ``` |
| 12 | + * |
| 13 | + * Supports all legacy multi-byte encodings listed in the WHATWG Encoding standard: |
| 14 | + * `gbk`, `gb18030`, `big5`, `euc-jp`, `iso-2022-jp`, `shift_jis`, `euc-kr`. |
| 15 | + * |
| 16 | + * @module @exodus/bytes/multi-byte.js |
| 17 | + */ |
| 18 | + |
| 19 | +/// <reference types="node" /> |
| 20 | + |
| 21 | +import type { Uint8ArrayBuffer } from './array.js'; |
| 22 | + |
| 23 | +/** |
| 24 | + * Create a decoder for a supported legacy multi-byte `encoding`, given its lowercased name `encoding`. |
| 25 | + * |
| 26 | + * Returns a function `decode(arr, stream = false)` that decodes bytes to a string. |
| 27 | + * |
| 28 | + * The returned function will maintain internal state while `stream = true` is used, allowing it to |
| 29 | + * handle incomplete multi-byte sequences across multiple calls. |
| 30 | + * State is reset when `stream = false` or when the function is called without the `stream` parameter. |
| 31 | + * |
| 32 | + * @param encoding - The encoding name (e.g., 'gbk', 'gb18030', 'big5', 'euc-jp', 'iso-2022-jp', 'shift_jis', 'euc-kr') |
| 33 | + * @param loose - If true, replaces unmapped bytes with replacement character instead of throwing (default: false) |
| 34 | + * @returns A function that decodes bytes to string, with optional streaming support |
| 35 | + */ |
| 36 | +export function createMultibyteDecoder( |
| 37 | + encoding: string, |
| 38 | + loose?: boolean |
| 39 | +): (arr: Uint8ArrayBuffer, stream?: boolean) => string; |
| 40 | + |
| 41 | +/** |
| 42 | + * Create an encoder for a supported legacy multi-byte `encoding`, given its lowercased name `encoding`. |
| 43 | + * |
| 44 | + * Returns a function `encode(string)` that encodes a string to bytes. |
| 45 | + * |
| 46 | + * In `'fatal'` mode (default), will throw on non well-formed strings or any codepoints which could |
| 47 | + * not be encoded in the target encoding. |
| 48 | + * |
| 49 | + * @param encoding - The encoding name (e.g., 'gbk', 'gb18030', 'big5', 'euc-jp', 'iso-2022-jp', 'shift_jis', 'euc-kr') |
| 50 | + * @param options - Encoding options |
| 51 | + * @param options.mode - Encoding mode (default: 'fatal'). Currently, only 'fatal' mode is supported. |
| 52 | + * @returns A function that encodes string to bytes |
| 53 | + */ |
| 54 | +export function createMultibyteEncoder( |
| 55 | + encoding: string, |
| 56 | + options?: { mode?: 'fatal' } |
| 57 | +): (string: string) => Uint8ArrayBuffer; |
0 commit comments