Kit
Functions

resizeCodec

Call Signature

resizeCodec<TFrom, TTo, TSize, TNewSize>(codec, resize): FixedSizeCodec<TFrom, TTo, TNewSize>

Updates the size of a given codec.

This function modifies the size of both the codec using a provided transformation function. It is useful for adjusting the allocated byte size for encoding and decoding without altering the underlying data structure.

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

Type Parameters

Type ParameterDescription
TFromThe type of the value to encode.
TToThe type of the decoded value.
TSize extends numberThe original fixed size of the encoded/decoded value (for fixed-size codecs).
TNewSize extends numberThe new fixed size after resizing (for fixed-size codecs).

Parameters

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

Returns

FixedSizeCodec<TFrom, TTo, TNewSize>

A new codec with the updated size.

Examples

Expanding a u16 codec from 2 to 4 bytes.

const codec = resizeCodec(getU16Codec(), size => size + 2);
const bytes = codec.encode(0xffff); // 0xffff0000 (two extra bytes added)
const value = codec.decode(bytes);  // 0xffff (reads original two bytes)

Shrinking a u32 codec to only use 2 bytes.

const codec = resizeCodec(getU32Codec(), () => 2);
codec.fixedSize; // 2

Remarks

If you only need to resize an encoder, use resizeEncoder. If you only need to resize a decoder, use resizeDecoder.

const bytes = resizeEncoder(getU32Encoder(), (size) => size + 2).encode(0xffff);
const value = resizeDecoder(getU32Decoder(), (size) => size + 2).decode(bytes);

See

Call Signature

resizeCodec<TCodec>(codec, resize): TCodec

Updates the size of a given codec.

This function modifies the size of both the codec using a provided transformation function. It is useful for adjusting the allocated byte size for encoding and decoding without altering the underlying data structure.

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

Type Parameters

Type Parameter
TCodec extends AnyCodec

Parameters

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

Returns

TCodec

A new codec with the updated size.

Examples

Expanding a u16 codec from 2 to 4 bytes.

const codec = resizeCodec(getU16Codec(), size => size + 2);
const bytes = codec.encode(0xffff); // 0xffff0000 (two extra bytes added)
const value = codec.decode(bytes);  // 0xffff (reads original two bytes)

Shrinking a u32 codec to only use 2 bytes.

const codec = resizeCodec(getU32Codec(), () => 2);
codec.fixedSize; // 2

Remarks

If you only need to resize an encoder, use resizeEncoder. If you only need to resize a decoder, use resizeDecoder.

const bytes = resizeEncoder(getU32Encoder(), (size) => size + 2).encode(0xffff);
const value = resizeDecoder(getU32Decoder(), (size) => size + 2).decode(bytes);

See

On this page