Skip to content

Commit 7818be1

Browse files
CopilotChALkeR
andauthored
feat: add TypeScript type definitions for multi-byte.js (#31)
Co-authored-by: Nikita Skovoroda <chalkerx@gmail.com>
1 parent c6bf7fe commit 7818be1

3 files changed

Lines changed: 78 additions & 7 deletions

File tree

README.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,17 +306,18 @@ const windows1252fromString = createSinglebyteEncoder('windows-1252', { mode: 'f
306306

307307
### `@exodus/bytes/multi-byte.js`
308308

309-
```js
310-
import { createMultibyteDecoder } from '@exodus/bytes/multi-byte.js'
311-
```
312-
313-
Decode the legacy multi-byte encodings according to the [Encoding standard](https://encoding.spec.whatwg.org/)
309+
Decode / encode the legacy multi-byte encodings according to the
310+
[Encoding standard](https://encoding.spec.whatwg.org/)
314311
([§10](https://encoding.spec.whatwg.org/#legacy-multi-byte-chinese-(simplified)-encodings),
315312
[§11](https://encoding.spec.whatwg.org/#legacy-multi-byte-chinese-(traditional)-encodings),
316313
[§12](https://encoding.spec.whatwg.org/#legacy-multi-byte-japanese-encodings),
317314
[§13](https://encoding.spec.whatwg.org/#legacy-multi-byte-korean-encodings)).
318315

319-
Supports all legacy multi-byte encodings listed in the standard:
316+
```js
317+
import { createMultibyteDecoder, createMultibyteEncoder } from '@exodus/bytes/multi-byte.js'
318+
```
319+
320+
Supports all legacy multi-byte encodings listed in the WHATWG Encoding standard:
320321
`gbk`, `gb18030`, `big5`, `euc-jp`, `iso-2022-jp`, `shift_jis`, `euc-kr`.
321322

322323
#### `createMultibyteDecoder(encoding, loose = false)`
@@ -325,7 +326,18 @@ Create a decoder for a supported legacy multi-byte `encoding`, given its lowerca
325326

326327
Returns a function `decode(arr, stream = false)` that decodes bytes to a string.
327328

328-
That function will have state while `stream = true` is used.
329+
The returned function will maintain internal state while `stream = true` is used, allowing it to
330+
handle incomplete multi-byte sequences across multiple calls.
331+
State is reset when `stream = false` or when the function is called without the `stream` parameter.
332+
333+
#### `createMultibyteEncoder(encoding, { mode = 'fatal' })`
334+
335+
Create an encoder for a supported legacy multi-byte `encoding`, given its lowercased name `encoding`.
336+
337+
Returns a function `encode(string)` that encodes a string to bytes.
338+
339+
In `'fatal'` mode (default), will throw on non well-formed strings or any codepoints which could
340+
not be encoded in the target encoding.
329341

330342
### `@exodus/bytes/bigint.js`
331343

multi-byte.d.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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;

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
"/index.js",
106106
"/index.d.ts",
107107
"/multi-byte.js",
108+
"/multi-byte.d.ts",
108109
"/multi-byte.node.js",
109110
"/single-byte.js",
110111
"/single-byte.d.ts",
@@ -153,6 +154,7 @@
153154
"default": "./hex.js"
154155
},
155156
"./multi-byte.js": {
157+
"types": "./multi-byte.d.ts",
156158
"node": "./multi-byte.node.js",
157159
"default": "./multi-byte.js"
158160
},

0 commit comments

Comments
 (0)