Kit
Functions

padLeftCodec

padLeftCodec<TCodec>(codec, offset): TCodec

Adds left padding to the given codec, shifting the encoding and decoding positions forward by offset bytes whilst increasing the size of the codec accordingly.

This ensures that values are read and written at a later position in the byte array, while the padding bytes remain unused.

Type Parameters

Type Parameter
TCodec extends AnyCodec

Parameters

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

Returns

TCodec

A new codec with left padding applied.

Example

const codec = padLeftCodec(getU16Codec(), 2);
const bytes = codec.encode(0xffff); // 0x0000ffff (0xffff written at offset 2)
const value = codec.decode(bytes);  // 0xffff (reads from offset 2)

Remarks

If you only need to apply padding for encoding, use padLeftEncoder. If you only need to apply padding for decoding, use padLeftDecoder.

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

See

On this page