Kit
Functions

resizeDecoder

Call Signature

resizeDecoder<TFrom, TSize, TNewSize>(decoder, resize): FixedSizeDecoder<TFrom, TNewSize>

Updates the size of a given decoder.

This function modifies the size of a decoder using a provided transformation function. For fixed-size decoders, it updates the fixedSize property to reflect the new size. Variable-size decoders remain unchanged, as their size is determined dynamically.

If the new size is negative, an error will be thrown.

For more details, see resizeCodec.

Type Parameters

Type ParameterDescription
TFrom-
TSize extends numberThe original fixed size of the decoded value.
TNewSize extends numberThe new fixed size after resizing.

Parameters

ParameterTypeDescription
decoderFixedSizeDecoder<TFrom, TSize>The decoder whose size will be updated.
resize(size) => TNewSizeA function that takes the current size and returns the new size.

Returns

FixedSizeDecoder<TFrom, TNewSize>

A new decoder with the updated size.

Examples

Expanding a u16 decoder to read 4 bytes instead of 2.

const decoder = resizeDecoder(getU16Decoder(), size => size + 2);
decoder.fixedSize; // 4

Shrinking a u32 decoder to only read 2 bytes.

const decoder = resizeDecoder(getU32Decoder(), () => 2);
decoder.fixedSize; // 2

See

Call Signature

resizeDecoder<TDecoder>(decoder, resize): TDecoder

Updates the size of a given decoder.

This function modifies the size of a decoder using a provided transformation function. For fixed-size decoders, it updates the fixedSize property to reflect the new size. Variable-size decoders remain unchanged, as their size is determined dynamically.

If the new size is negative, an error will be thrown.

For more details, see resizeCodec.

Type Parameters

Type Parameter
TDecoder extends AnyDecoder

Parameters

ParameterTypeDescription
decoderTDecoderThe decoder whose size will be updated.
resize(size) => numberA function that takes the current size and returns the new size.

Returns

TDecoder

A new decoder with the updated size.

Examples

Expanding a u16 decoder to read 4 bytes instead of 2.

const decoder = resizeDecoder(getU16Decoder(), size => size + 2);
decoder.fixedSize; // 4

Shrinking a u32 decoder to only read 2 bytes.

const decoder = resizeDecoder(getU32Decoder(), () => 2);
decoder.fixedSize; // 2

See

On this page