refineFields<TCoders extends FieldCoders,THost extends [K in keyof TCoders]: Uint8Array>(coders: TCoders): Refiner<THost, Omit<THost, keyof TCoders> & DecodedFields<TCoders>, []>
Creates a Refiner that swaps named Uint8Array fields of a
host record for the typed values produced by their sub-coders. Fields not
in coders are passed through unchanged.
Pair with refine to apply the refinement to a host coder, or
use as an arm of refineSwitch for layered formats where the
host's discriminator picks which sub-coder to use.
Refine a payload field with a nested coder
Refine a payload field with a nested coder
import { assertEquals } from "@std/assert"; import { bytes, refine, refineFields, struct, u16be, u8 } from "@hertzg/binstruct"; const inner = struct({ a: u8(), b: u8() }); const outer = struct({ tag: u16be(), payload: bytes(2) }); const coder = refine(outer, refineFields({ payload: inner }))(); const buffer = new Uint8Array(4); coder.encode({ tag: 0xbeef, payload: { a: 1, b: 2 } }, buffer); const [decoded] = coder.decode(buffer); assertEquals(decoded, { tag: 0xbeef, payload: { a: 1, b: 2 } });
TCoders extends FieldCoders
Map of field name to sub-coder.
THost extends [K in keyof TCoders]: Uint8Array
Host record; constrained so each refined field is
Uint8Array on the unrefined side.
coders: TCoders
Map of field names to per-field sub-coders.