Skip to content

Commit 2184097

Browse files
committed
chore: add utf32.d.ts (not exported yet)
1 parent 7a4347e commit 2184097

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

utf32.d.ts

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

0 commit comments

Comments
 (0)