Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ history.

## Unreleased

- `types/uint8array`: `toVec` attempts the conversion instead of precomputing a
byte-count bound; behavior and error message unchanged
[#1543](https://github.com/functionalscript/functionalscript/pull/1543)
- `media/json/schema`: `toJsonSchema` supports recursive schemas — it converts
through `fjs/types/rtti/data` (new `dataToJsonSchema`) and emits named
recursion as `$defs`/`$ref`; output is canonical, so `anyOf` members and
Expand Down
30 changes: 17 additions & 13 deletions fjs/types/uint8array/module.f.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,38 @@
* @module
*/

import { assert, assertNotNullish } from '../../asserts/module.f.mjs'
import { assertNotNullish } from '../../asserts/module.f.mjs'
import { utf8, utf8ToString } from '../../text/module.f.mjs'
import { maxLengthBytes, msb, tryU8ListToVec, u8List, u8ListToVec } from '../bit_vec/module.f.mjs'
import { msb, tryU8ListToVec, u8List } from '../bit_vec/module.f.mjs'
/** @import { Vec } from '../bit_vec/types.ts' */
import { compose } from '../function/module.f.mjs'
import { flat, fromArrayLike, iterable, map } from '../list/module.f.mjs'
/** @import { List } from '../list/types.ts' */

const u8ListToVecMsb = u8ListToVec(msb)
const tryU8ListToVecMsb = tryU8ListToVec(msb)
const u8ListMsb = u8List(msb)

const m = map(fromArrayLike)

/**
* Converts a Uint8Array into an MSB-first bit vector.
* Concatenates a list of `Uint8Array` values into one MSB-first bit vector.
*
* @type {(input: Uint8Array) => Vec}
* Throws if the result would exceed `maxLength`. The bound is not precomputed:
* `tryU8ListToVec` attempts the real conversion and reports `null` when it does
* not fit (AGENTS.md §5.6).
*
* @type {(input: List<Uint8Array>) => Vec}
*/
export const toVec = input => {
assert(input.length <= maxLengthBytes, "the array is too big")
return u8ListToVecMsb(fromArrayLike(input))
}

const m = map(fromArrayLike)

/** @type {(input: List<Uint8Array>) => Vec} */
export const listToVec = input =>
assertNotNullish(tryU8ListToVecMsb(flat(m(input))), "the array is too big")

/**
* Converts a Uint8Array into an MSB-first bit vector.
*
* @type {(input: Uint8Array) => Vec}
*/
export const toVec = input => listToVec([input])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add the required changelog entry

When this exported-code change is released, the Unreleased section will contain no record of it because the diff changes module.f.mjs but never updates CHANGELOG.md. Repository workflow requires every code-changing PR, unlike documentation-only work, to add a changelog entry using the real PR number, so add that entry before merging.

AGENTS.md reference: AGENTS.md:L132-L135

Useful? React with 👍 / 👎.


/**
* Converts an MSB-first bit vector into a Uint8Array.
*
Expand Down
36 changes: 0 additions & 36 deletions fjs/types/uint8array/todo/tovec-precomputed-bound.md

This file was deleted.

Loading