Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
coverage
playground
*.tgz
package-lock.json
Comment thread
ChALkeR marked this conversation as resolved.
Outdated
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"/single-byte.d.ts",
"/single-byte.node.js",
"/utf16.js",
"/utf16.d.ts",
"/utf16.node.js",
"/utf8.js",
"/utf8.d.ts",
Expand Down Expand Up @@ -176,6 +177,7 @@
"default": "./encoding-browser.js"
},
"./utf16.js": {
"types": "./utf16.d.ts",
"node": "./utf16.node.js",
"default": "./utf16.js"
},
Expand Down
88 changes: 88 additions & 0 deletions utf16.d.ts
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._
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot and here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot sync these docs into README.md

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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._
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot this misses a newline. Ensure this is synced with README

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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;