Skip to content

Commit c6bf7fe

Browse files
authored
feat: add TypeScript definitions for utf16.js (#30)
1 parent c84c526 commit c6bf7fe

File tree

4 files changed

+129
-3
lines changed

4 files changed

+129
-3
lines changed

README.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,44 @@ import { utf16fromString, utf16toString } from '@exodus/bytes/utf16.js'
154154
import { utf16fromStringLoose, utf16toStringLoose } from '@exodus/bytes/utf16.js'
155155
```
156156

157-
_These methods by design encode/decode BOM (codepoint `U+FEFF` Byte Order Mark) as-is._
157+
_These methods by design encode/decode BOM (codepoint `U+FEFF` Byte Order Mark) as-is._\
158158
_If you need BOM handling or detection, use `@exodus/bytes/encoding.js`_
159159

160160
#### `utf16fromString(string, format = 'uint16')`
161+
162+
Encode a string to UTF-16 bytes (strict mode)
163+
164+
Throws on invalid Unicode (unpaired surrogates)
165+
161166
#### `utf16fromStringLoose(string, format = 'uint16')`
162-
#### `utf16toString(arr, 'uint16')`
163-
#### `utf16toStringLoose(arr, 'uint16')`
167+
168+
Encode a string to UTF-16 bytes (loose mode)
169+
170+
Replaces invalid Unicode (unpaired surrogates) with replacement codepoints `U+FFFD`
171+
per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
172+
173+
_Such replacement is a non-injective function, is irreversible and causes collisions.\
174+
Prefer using strict throwing methods for cryptography applications._
175+
176+
#### `utf16toString(arr, format = 'uint16')`
177+
178+
Decode UTF-16 bytes to a string (strict mode)
179+
180+
Throws on invalid UTF-16 byte sequences
181+
182+
Throws on non-even byte length.
183+
184+
#### `utf16toStringLoose(arr, format = 'uint16')`
185+
186+
Decode UTF-16 bytes to a string (loose mode)
187+
188+
Replaces invalid UTF-16 byte sequences with replacement codepoints `U+FFFD`
189+
per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
190+
191+
_Such replacement is a non-injective function, is irreversible and causes collisions.\
192+
Prefer using strict throwing methods for cryptography applications._
193+
194+
Throws on non-even byte length.
164195

165196
### `@exodus/bytes/single-byte.js`
166197

array.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// < TypeScript 5.7 doesn't support templates for Uint8Array.
1616
// So this type is defined as a workaround to evaluate to Uint8Array<ArrayBuffer> on all versions of TypeScript.
1717
export type Uint8ArrayBuffer = ReturnType<typeof Uint8Array.from>;
18+
export type Uint16ArrayBuffer = ReturnType<typeof Uint16Array.from>;
1819

1920
/**
2021
* Output format for typed array conversions

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"/single-byte.d.ts",
111111
"/single-byte.node.js",
112112
"/utf16.js",
113+
"/utf16.d.ts",
113114
"/utf16.node.js",
114115
"/utf8.js",
115116
"/utf8.d.ts",
@@ -176,6 +177,7 @@
176177
"default": "./encoding-browser.js"
177178
},
178179
"./utf16.js": {
180+
"types": "./utf16.d.ts",
179181
"node": "./utf16.node.js",
180182
"default": "./utf16.js"
181183
},

utf16.d.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* UTF-16 encoding/decoding
3+
*
4+
* ```js
5+
* import { utf16fromString, utf16toString } from '@exodus/bytes/utf16.js'
6+
*
7+
* // loose
8+
* import { utf16fromStringLoose, utf16toStringLoose } from '@exodus/bytes/utf16.js'
9+
* ```
10+
*
11+
* _These methods by design encode/decode BOM (codepoint `U+FEFF` Byte Order Mark) as-is._\
12+
* _If you need BOM handling or detection, use `@exodus/bytes/encoding.js`_
13+
*
14+
* @module @exodus/bytes/utf16.js
15+
*/
16+
17+
/// <reference types="node" />
18+
19+
import type { Uint8ArrayBuffer, Uint16ArrayBuffer } from './array.js';
20+
21+
/**
22+
* Output format for UTF-16 encoding
23+
*/
24+
export type Utf16Format = 'uint16' | 'uint8-le' | 'uint8-be';
25+
26+
/**
27+
* Encode a string to UTF-16 bytes (strict mode)
28+
*
29+
* Throws on invalid Unicode (unpaired surrogates)
30+
*
31+
* @param string - The string to encode
32+
* @param format - Output format (default: 'uint16')
33+
* @returns The encoded bytes
34+
*/
35+
export function utf16fromString(string: string, format?: 'uint16'): Uint16ArrayBuffer;
36+
export function utf16fromString(string: string, format: 'uint8-le'): Uint8ArrayBuffer;
37+
export function utf16fromString(string: string, format: 'uint8-be'): Uint8ArrayBuffer;
38+
export function utf16fromString(string: string, format?: Utf16Format): Uint16ArrayBuffer | Uint8ArrayBuffer;
39+
40+
/**
41+
* Encode a string to UTF-16 bytes (loose mode)
42+
*
43+
* Replaces invalid Unicode (unpaired surrogates) with replacement codepoints `U+FFFD`
44+
* per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
45+
*
46+
* _Such replacement is a non-injective function, is irreversible and causes collisions.\
47+
* Prefer using strict throwing methods for cryptography applications._
48+
*
49+
* @param string - The string to encode
50+
* @param format - Output format (default: 'uint16')
51+
* @returns The encoded bytes
52+
*/
53+
export function utf16fromStringLoose(string: string, format?: 'uint16'): Uint16ArrayBuffer;
54+
export function utf16fromStringLoose(string: string, format: 'uint8-le'): Uint8ArrayBuffer;
55+
export function utf16fromStringLoose(string: string, format: 'uint8-be'): Uint8ArrayBuffer;
56+
export function utf16fromStringLoose(string: string, format?: Utf16Format): Uint16ArrayBuffer | Uint8ArrayBuffer;
57+
58+
/**
59+
* Decode UTF-16 bytes to a string (strict mode)
60+
*
61+
* Throws on invalid UTF-16 byte sequences
62+
*
63+
* Throws on non-even byte length.
64+
*
65+
* @param arr - The bytes to decode
66+
* @param format - Input format (default: 'uint16')
67+
* @returns The decoded string
68+
*/
69+
export function utf16toString(arr: Uint16ArrayBuffer, format?: 'uint16'): string;
70+
export function utf16toString(arr: Uint8ArrayBuffer, format: 'uint8-le'): string;
71+
export function utf16toString(arr: Uint8ArrayBuffer, format: 'uint8-be'): string;
72+
export function utf16toString(arr: Uint16ArrayBuffer | Uint8ArrayBuffer, format?: Utf16Format): string;
73+
74+
/**
75+
* Decode UTF-16 bytes to a string (loose mode)
76+
*
77+
* Replaces invalid UTF-16 byte sequences with replacement codepoints `U+FFFD`
78+
* per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
79+
*
80+
* _Such replacement is a non-injective function, is irreversible and causes collisions.\
81+
* Prefer using strict throwing methods for cryptography applications._
82+
*
83+
* Throws on non-even byte length.
84+
*
85+
* @param arr - The bytes to decode
86+
* @param format - Input format (default: 'uint16')
87+
* @returns The decoded string
88+
*/
89+
export function utf16toStringLoose(arr: Uint16ArrayBuffer, format?: 'uint16'): string;
90+
export function utf16toStringLoose(arr: Uint8ArrayBuffer, format: 'uint8-le'): string;
91+
export function utf16toStringLoose(arr: Uint8ArrayBuffer, format: 'uint8-be'): string;
92+
export function utf16toStringLoose(arr: Uint16ArrayBuffer | Uint8ArrayBuffer, format?: Utf16Format): string;

0 commit comments

Comments
 (0)