Kit
Functions

resizeEncoder

Call Signature

resizeEncoder<TFrom, TSize, TNewSize>(encoder, resize): FixedSizeEncoder<TFrom, TNewSize>

Updates the size of a given encoder.

This function modifies the size of an encoder using a provided transformation function. For fixed-size encoders, it updates the fixedSize property, and for variable-size encoders, it adjusts the size calculation based on the encoded value.

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

For more details, see resizeCodec.

Type Parameters

Type ParameterDescription
TFromThe type of the value to encode.
TSize extends numberThe original fixed size of the encoded value.
TNewSize extends numberThe new fixed size after resizing.

Parameters

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

Returns

FixedSizeEncoder<TFrom, TNewSize>

A new encoder with the updated size.

Examples

Increasing the size of a u16 encoder by 2 bytes.

const encoder = resizeEncoder(getU16Encoder(), size => size + 2);
encoder.encode(0xffff); // 0xffff0000 (two extra bytes added)

Shrinking a u32 encoder to only use 2 bytes.

const encoder = resizeEncoder(getU32Encoder(), () => 2);
encoder.fixedSize; // 2

See

Call Signature

resizeEncoder<TEncoder>(encoder, resize): TEncoder

Updates the size of a given encoder.

This function modifies the size of an encoder using a provided transformation function. For fixed-size encoders, it updates the fixedSize property, and for variable-size encoders, it adjusts the size calculation based on the encoded value.

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

For more details, see resizeCodec.

Type Parameters

Type Parameter
TEncoder extends AnyEncoder

Parameters

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

Returns

TEncoder

A new encoder with the updated size.

Examples

Increasing the size of a u16 encoder by 2 bytes.

const encoder = resizeEncoder(getU16Encoder(), size => size + 2);
encoder.encode(0xffff); // 0xffff0000 (two extra bytes added)

Shrinking a u32 encoder to only use 2 bytes.

const encoder = resizeEncoder(getU32Encoder(), () => 2);
encoder.fixedSize; // 2

See

On this page