-
Notifications
You must be signed in to change notification settings - Fork 5
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
37c2cec
Initial plan
Copilot 38f7646
Add TypeScript types for utf16.js
Copilot e2e20bc
Verify TypeScript types and tests
Copilot 394dd6c
Remove package-lock.json and add to .gitignore
Copilot f4116be
Address code review feedback: fix typos and formatting
Copilot 8639a02
Use Uint16ArrayBuffer type for consistency with Uint8ArrayBuffer
Copilot ae462dd
Move Uint16ArrayBuffer type to array.d.ts
Copilot e46ac4b
Remove unrelated .gitignore change
Copilot 099ae6b
Remove blank line between type definitions
Copilot bea50c5
Remove duplicate comment for Uint16ArrayBuffer
Copilot 93ccc67
Add detailed UTF-16 API documentation to README
Copilot 7761ebe
Fix formatting: use backslash continuation in JSDoc comments
Copilot d149023
Fix module-level JSDoc formatting with backslash continuation
Copilot 8aef664
Add note about throwing on non-even byte length
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /** | ||
| * 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._\ | ||
| * _If you need BOM handling or detection, use `@exodus/bytes/encoding.js`_ | ||
| * | ||
| * @module @exodus/bytes/utf16.js | ||
| */ | ||
|
|
||
| /// <reference types="node" /> | ||
|
|
||
| import type { Uint8ArrayBuffer, Uint16ArrayBuffer } 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'): Uint16ArrayBuffer; | ||
| 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): Uint16ArrayBuffer | 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.\ | ||
| * 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'): Uint16ArrayBuffer; | ||
| 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): Uint16ArrayBuffer | Uint8ArrayBuffer; | ||
|
|
||
| /** | ||
| * Decode UTF-16 bytes to a string (strict mode) | ||
| * | ||
| * Throws on invalid UTF-16 byte sequences | ||
| * | ||
| * Throws on non-even byte length. | ||
| * | ||
| * @param arr - The bytes to decode | ||
| * @param format - Input format (default: 'uint16') | ||
| * @returns The decoded string | ||
| */ | ||
| export function utf16toString(arr: Uint16ArrayBuffer, 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: Uint16ArrayBuffer | 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._ | ||
| * | ||
| * Throws on non-even byte length. | ||
| * | ||
| * @param arr - The bytes to decode | ||
| * @param format - Input format (default: 'uint16') | ||
| * @returns The decoded string | ||
| */ | ||
| export function utf16toStringLoose(arr: Uint16ArrayBuffer, 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: Uint16ArrayBuffer | Uint8ArrayBuffer, format?: Utf16Format): string; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot here and in README, for
utf16toStringLooseThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added note about throwing on non-even byte length to both
utf16toStringandutf16toStringLoosein utf16.d.ts and README.md (8aef664)