Kit
Functions

getHiddenSuffixCodec

Call Signature

getHiddenSuffixCodec<TFrom, TTo>(codec, suffixedCodecs): FixedSizeCodec<TFrom, TTo>

Returns a codec that encodes and decodes values with a hidden suffix.

  • Encoding: Appends hidden data after encoding the main value.
  • Decoding: Skips the hidden suffix after decoding the main value.

This is useful for any implicit metadata that should be present in binary formats but omitted from the API.

Type Parameters

Type ParameterDescription
TFromThe type of the main value being encoded.
TToThe type of the main value being decoded.

Parameters

ParameterTypeDescription
codecFixedSizeCodec<TFrom, TTo>The codec for the main value.
suffixedCodecsreadonly FixedSizeCodec<void>[]A list of void codecs that produce the hidden suffix.

Returns

FixedSizeCodec<TFrom, TTo>

A FixedSizeCodec or VariableSizeCodec for encoding and decoding values with a hidden suffix.

Example

Encoding and decoding a value with suffixed constants.

const codec = getHiddenSuffixCodec(getUtf8Codec(), [
  getConstantCodec(new Uint8Array([1, 2, 3])),
  getConstantCodec(new Uint8Array([4, 5, 6])),
]);
 
const bytes = codec.encode('Hello');
// 0x48656c6c6f010203040506
//   |         |     └-- Our second hidden suffix.
//   |         └-- Our first hidden suffix.
//   └-- Our encoded value ("Hello").
 
codec.decode(bytes);
// 'Hello'

Remarks

If all you need is padding zeroes after a value, consider using padRightCodec instead.

Separate getHiddenSuffixEncoder and getHiddenSuffixDecoder functions are available.

const bytes = getHiddenSuffixEncoder(getUtf8Encoder(), [
  getConstantEncoder(new Uint8Array([1, 2, 3])),
  getConstantEncoder(new Uint8Array([4, 5, 6])),
]).encode('Hello');
 
const value = getHiddenSuffixDecoder(getUtf8Decoder(), [
  getConstantDecoder(new Uint8Array([1, 2, 3])),
  getConstantDecoder(new Uint8Array([4, 5, 6])),
]).decode(bytes);

See

Call Signature

getHiddenSuffixCodec<TFrom, TTo>(codec, suffixedCodecs): VariableSizeCodec<TFrom, TTo>

Returns a codec that encodes and decodes values with a hidden suffix.

  • Encoding: Appends hidden data after encoding the main value.
  • Decoding: Skips the hidden suffix after decoding the main value.

This is useful for any implicit metadata that should be present in binary formats but omitted from the API.

Type Parameters

Type ParameterDescription
TFromThe type of the main value being encoded.
TToThe type of the main value being decoded.

Parameters

ParameterTypeDescription
codecCodec<TFrom, TTo>The codec for the main value.
suffixedCodecsreadonly Codec<void>[]A list of void codecs that produce the hidden suffix.

Returns

VariableSizeCodec<TFrom, TTo>

A FixedSizeCodec or VariableSizeCodec for encoding and decoding values with a hidden suffix.

Example

Encoding and decoding a value with suffixed constants.

const codec = getHiddenSuffixCodec(getUtf8Codec(), [
  getConstantCodec(new Uint8Array([1, 2, 3])),
  getConstantCodec(new Uint8Array([4, 5, 6])),
]);
 
const bytes = codec.encode('Hello');
// 0x48656c6c6f010203040506
//   |         |     └-- Our second hidden suffix.
//   |         └-- Our first hidden suffix.
//   └-- Our encoded value ("Hello").
 
codec.decode(bytes);
// 'Hello'

Remarks

If all you need is padding zeroes after a value, consider using padRightCodec instead.

Separate getHiddenSuffixEncoder and getHiddenSuffixDecoder functions are available.

const bytes = getHiddenSuffixEncoder(getUtf8Encoder(), [
  getConstantEncoder(new Uint8Array([1, 2, 3])),
  getConstantEncoder(new Uint8Array([4, 5, 6])),
]).encode('Hello');
 
const value = getHiddenSuffixDecoder(getUtf8Decoder(), [
  getConstantDecoder(new Uint8Array([1, 2, 3])),
  getConstantDecoder(new Uint8Array([4, 5, 6])),
]).decode(bytes);

See

On this page