Kit
Functions

fixEncoderSize

fixEncoderSize<TFrom, TSize>(encoder, fixedBytes): FixedSizeEncoder<TFrom, TSize>

Creates a fixed-size encoder from a given encoder.

The resulting encoder ensures that encoded values always have the specified number of bytes. If the original encoded value is larger than fixedBytes, it is truncated. If it is smaller, it is padded with trailing zeroes.

For more details, see fixCodecSize.

Type Parameters

Type ParameterDescription
TFromThe type of the value to encode.
TSize extends numberThe fixed size of the encoded value in bytes.

Parameters

ParameterTypeDescription
encoderEncoder<TFrom>The encoder to wrap into a fixed-size encoder.
fixedBytesTSizeThe fixed number of bytes to write.

Returns

FixedSizeEncoder<TFrom, TSize>

A FixedSizeEncoder that ensures a consistent output size.

Example

const encoder = fixEncoderSize(getUtf8Encoder(), 4);
encoder.encode("Hello"); // 0x48656c6c (truncated)
encoder.encode("Hi");    // 0x48690000 (padded)
encoder.encode("Hiya");  // 0x48697961 (same length)

Remarks

If you need a full codec with both encoding and decoding, use fixCodecSize.

See

On this page