-
Notifications
You must be signed in to change notification settings - Fork 6
feat: add TypeScript definitions for utf16.js #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
37c2cec
38f7646
e2e20bc
394dd6c
f4116be
8639a02
ae462dd
e46ac4b
099ae6b
bea50c5
93ccc67
7761ebe
d149023
8aef664
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,4 @@ node_modules | |
| coverage | ||
| playground | ||
| *.tgz | ||
| package-lock.json | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /** | ||
| * UTF-16 encoding/decoding | ||
| * | ||
| * ```js | ||
| * import { utf16fromString, utf16toString } from '@exodus/bytes/utf16.js' | ||
| * | ||
| * // loose | ||
| * import { utf16fromStringLoose, utf16toStringLoose } from '@exodus/bytes/utf16.js' | ||
| * ``` | ||
| * | ||
| * _These methods by design encode/decode BOM (codepoint `U+FEFF` Byte Order Mark) as-is._ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot and here
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed module-level JSDoc formatting with backslash continuation (d149023) |
||
| * _If you need BOM handling or detection, use `@exodus/bytes/encoding.js`_ | ||
| * | ||
| * @module @exodus/bytes/utf16.js | ||
| */ | ||
|
|
||
| /// <reference types="node" /> | ||
|
|
||
| import type { Uint8ArrayBuffer } from './array.js'; | ||
|
|
||
| /** | ||
| * Output format for UTF-16 encoding | ||
| */ | ||
| export type Utf16Format = 'uint16' | 'uint8-le' | 'uint8-be'; | ||
|
|
||
| /** | ||
| * Encode a string to UTF-16 bytes (strict mode) | ||
| * | ||
| * Throws on invalid Unicode (unpaired surrogates) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot sync these docs into README.md
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added detailed UTF-16 API documentation to README.md matching the UTF-8 section format (93ccc67) |
||
| * | ||
| * @param string - The string to encode | ||
| * @param format - Output format (default: 'uint16') | ||
| * @returns The encoded bytes | ||
| */ | ||
| export function utf16fromString(string: string, format?: 'uint16'): Uint16Array; | ||
| export function utf16fromString(string: string, format: 'uint8-le'): Uint8ArrayBuffer; | ||
| export function utf16fromString(string: string, format: 'uint8-be'): Uint8ArrayBuffer; | ||
| export function utf16fromString(string: string, format?: Utf16Format): Uint16Array | Uint8ArrayBuffer; | ||
|
|
||
| /** | ||
| * Encode a string to UTF-16 bytes (loose mode) | ||
| * | ||
| * Replaces invalid Unicode (unpaired surrogates) with replacement codepoints `U+FFFD` | ||
| * per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification. | ||
| * | ||
| * _Such replacement is a non-injective function, is irreversible and causes collisions._ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot this misses a newline. Ensure this is synced with README
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed JSDoc formatting to use backslash continuation matching UTF-8 pattern and README (7761ebe) |
||
| * _Prefer using strict throwing methods for cryptography applications._ | ||
| * | ||
| * @param string - The string to encode | ||
| * @param format - Output format (default: 'uint16') | ||
| * @returns The encoded bytes | ||
| */ | ||
| export function utf16fromStringLoose(string: string, format?: 'uint16'): Uint16Array; | ||
| export function utf16fromStringLoose(string: string, format: 'uint8-le'): Uint8ArrayBuffer; | ||
| export function utf16fromStringLoose(string: string, format: 'uint8-be'): Uint8ArrayBuffer; | ||
| export function utf16fromStringLoose(string: string, format?: Utf16Format): Uint16Array | Uint8ArrayBuffer; | ||
|
|
||
| /** | ||
| * Decode UTF-16 bytes to a string (strict mode) | ||
| * | ||
| * Throws on invalid UTF-16 byte sequences | ||
| * | ||
| * @param arr - The bytes to decode | ||
| * @param format - Input format (default: 'uint16') | ||
| * @returns The decoded string | ||
| */ | ||
| export function utf16toString(arr: Uint16Array, format?: 'uint16'): string; | ||
| export function utf16toString(arr: Uint8ArrayBuffer, format: 'uint8-le'): string; | ||
| export function utf16toString(arr: Uint8ArrayBuffer, format: 'uint8-be'): string; | ||
| export function utf16toString(arr: Uint16Array | Uint8ArrayBuffer, format?: Utf16Format): string; | ||
|
|
||
| /** | ||
| * Decode UTF-16 bytes to a string (loose mode) | ||
| * | ||
| * Replaces invalid UTF-16 byte sequences with replacement codepoints `U+FFFD` | ||
| * per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification. | ||
| * | ||
| * _Such replacement is a non-injective function, is irreversible and causes collisions._ | ||
| * _Prefer using strict throwing methods for cryptography applications._ | ||
| * | ||
| * @param arr - The bytes to decode | ||
| * @param format - Input format (default: 'uint16') | ||
| * @returns The decoded string | ||
| */ | ||
| export function utf16toStringLoose(arr: Uint16Array, format?: 'uint16'): string; | ||
| export function utf16toStringLoose(arr: Uint8ArrayBuffer, format: 'uint8-le'): string; | ||
| export function utf16toStringLoose(arr: Uint8ArrayBuffer, format: 'uint8-be'): string; | ||
| export function utf16toStringLoose(arr: Uint16Array | Uint8ArrayBuffer, format?: Utf16Format): string; | ||
Uh oh!
There was an error while loading. Please reload this page.