Kit
Functions

padRightCodec

padRightCodec<TCodec>(codec, offset): TCodec

Adds right padding to the given codec, extending the encoded and decoded value by offset bytes whilst increasing the size of the codec accordingly.

The extra bytes remain unused, ensuring that the next operation starts further along the byte array.

Type Parameters

Type Parameter
TCodec extends AnyCodec

Parameters

ParameterTypeDescription
codecTCodecThe codec to pad.
offsetnumberThe number of padding bytes to add after encoding and decoding.

Returns

TCodec

A new codec with right padding applied.

Example

const codec = padRightCodec(getU16Codec(), 2);
const bytes = codec.encode(0xffff); // 0xffff0000 (two extra bytes added)
const value = codec.decode(bytes);  // 0xffff (ignores padding bytes)

Remarks

If you only need to apply padding for encoding, use padRightEncoder. If you only need to apply padding for decoding, use padRightDecoder.

const bytes = padRightEncoder(getU16Encoder(), 2).encode(0xffff);
const value = padRightDecoder(getU16Decoder(), 2).decode(bytes);

See

On this page