Kit
Functions

transformCodec

Call Signature

transformCodec<TOldFrom, TNewFrom, TTo, TSize>(codec, unmap): FixedSizeCodec<TNewFrom, TTo, TSize>

Transforms a codec by mapping its input and output values.

This function takes an existing Codec<A, B> and returns a Codec<C, D>, allowing:

  • Values of type C to be transformed into A before encoding.
  • Values of type B to be transformed into D after decoding.

This is useful for adapting codecs to work with different representations, handling default values, or converting between primitive and structured types.

Type Parameters

Type ParameterDescription
TOldFromThe original type expected by the codec.
TNewFromThe new type that will be transformed before encoding.
TTo-
TSize extends number-

Parameters

ParameterTypeDescription
codecFixedSizeCodec<TOldFrom, TTo, TSize>The codec to transform.
unmap(value) => TOldFromA function that converts values of TNewFrom into TOldFrom before encoding.

Returns

FixedSizeCodec<TNewFrom, TTo, TSize>

A new codec that encodes TNewFrom and decodes into TNewTo.

Example

Mapping a u32 codec to encode string lengths and decode them into 'x' characters.

const codec = transformCodec(
    getU32Codec(),
    (value: string) => value.length, // Encode string length
    (length) => 'x'.repeat(length)  // Decode length into a string of 'x's
);
 
const bytes = codec.encode("hello"); // 0x05000000 (stores length 5)
const value = codec.decode(bytes);   // "xxxxx"

Remarks

If only input transformation is needed, use transformEncoder. If only output transformation is needed, use transformDecoder.

const bytes = transformEncoder(getU32Encoder(), (value: string) => value.length).encode("hello");
const value = transformDecoder(getU32Decoder(), (length) => 'x'.repeat(length)).decode(bytes);

See

Call Signature

transformCodec<TOldFrom, TNewFrom, TTo>(codec, unmap): VariableSizeCodec<TNewFrom, TTo>

Transforms a codec by mapping its input and output values.

This function takes an existing Codec<A, B> and returns a Codec<C, D>, allowing:

  • Values of type C to be transformed into A before encoding.
  • Values of type B to be transformed into D after decoding.

This is useful for adapting codecs to work with different representations, handling default values, or converting between primitive and structured types.

Type Parameters

Type ParameterDescription
TOldFromThe original type expected by the codec.
TNewFromThe new type that will be transformed before encoding.
TTo-

Parameters

ParameterTypeDescription
codecVariableSizeCodec<TOldFrom, TTo>The codec to transform.
unmap(value) => TOldFromA function that converts values of TNewFrom into TOldFrom before encoding.

Returns

VariableSizeCodec<TNewFrom, TTo>

A new codec that encodes TNewFrom and decodes into TNewTo.

Example

Mapping a u32 codec to encode string lengths and decode them into 'x' characters.

const codec = transformCodec(
    getU32Codec(),
    (value: string) => value.length, // Encode string length
    (length) => 'x'.repeat(length)  // Decode length into a string of 'x's
);
 
const bytes = codec.encode("hello"); // 0x05000000 (stores length 5)
const value = codec.decode(bytes);   // "xxxxx"

Remarks

If only input transformation is needed, use transformEncoder. If only output transformation is needed, use transformDecoder.

const bytes = transformEncoder(getU32Encoder(), (value: string) => value.length).encode("hello");
const value = transformDecoder(getU32Decoder(), (length) => 'x'.repeat(length)).decode(bytes);

See

Call Signature

transformCodec<TOldFrom, TNewFrom, TTo>(codec, unmap): Codec<TNewFrom, TTo>

Transforms a codec by mapping its input and output values.

This function takes an existing Codec<A, B> and returns a Codec<C, D>, allowing:

  • Values of type C to be transformed into A before encoding.
  • Values of type B to be transformed into D after decoding.

This is useful for adapting codecs to work with different representations, handling default values, or converting between primitive and structured types.

Type Parameters

Type ParameterDescription
TOldFromThe original type expected by the codec.
TNewFromThe new type that will be transformed before encoding.
TTo-

Parameters

ParameterTypeDescription
codecCodec<TOldFrom, TTo>The codec to transform.
unmap(value) => TOldFromA function that converts values of TNewFrom into TOldFrom before encoding.

Returns

Codec<TNewFrom, TTo>

A new codec that encodes TNewFrom and decodes into TNewTo.

Example

Mapping a u32 codec to encode string lengths and decode them into 'x' characters.

const codec = transformCodec(
    getU32Codec(),
    (value: string) => value.length, // Encode string length
    (length) => 'x'.repeat(length)  // Decode length into a string of 'x's
);
 
const bytes = codec.encode("hello"); // 0x05000000 (stores length 5)
const value = codec.decode(bytes);   // "xxxxx"

Remarks

If only input transformation is needed, use transformEncoder. If only output transformation is needed, use transformDecoder.

const bytes = transformEncoder(getU32Encoder(), (value: string) => value.length).encode("hello");
const value = transformDecoder(getU32Decoder(), (length) => 'x'.repeat(length)).decode(bytes);

See

Call Signature

transformCodec<TOldFrom, TNewFrom, TOldTo, TNewTo, TSize>(codec, unmap, map): FixedSizeCodec<TNewFrom, TNewTo, TSize>

Transforms a codec by mapping its input and output values.

This function takes an existing Codec<A, B> and returns a Codec<C, D>, allowing:

  • Values of type C to be transformed into A before encoding.
  • Values of type B to be transformed into D after decoding.

This is useful for adapting codecs to work with different representations, handling default values, or converting between primitive and structured types.

Type Parameters

Type ParameterDescription
TOldFromThe original type expected by the codec.
TNewFromThe new type that will be transformed before encoding.
TOldToThe original type returned by the codec.
TNewToThe new type that will be transformed after decoding.
TSize extends number-

Parameters

ParameterTypeDescription
codecFixedSizeCodec<TOldFrom, TOldTo, TSize>The codec to transform.
unmap(value) => TOldFromA function that converts values of TNewFrom into TOldFrom before encoding.
map(value, bytes, offset) => TNewToA function that converts values of TOldTo into TNewTo after decoding (optional).

Returns

FixedSizeCodec<TNewFrom, TNewTo, TSize>

A new codec that encodes TNewFrom and decodes into TNewTo.

Example

Mapping a u32 codec to encode string lengths and decode them into 'x' characters.

const codec = transformCodec(
    getU32Codec(),
    (value: string) => value.length, // Encode string length
    (length) => 'x'.repeat(length)  // Decode length into a string of 'x's
);
 
const bytes = codec.encode("hello"); // 0x05000000 (stores length 5)
const value = codec.decode(bytes);   // "xxxxx"

Remarks

If only input transformation is needed, use transformEncoder. If only output transformation is needed, use transformDecoder.

const bytes = transformEncoder(getU32Encoder(), (value: string) => value.length).encode("hello");
const value = transformDecoder(getU32Decoder(), (length) => 'x'.repeat(length)).decode(bytes);

See

Call Signature

transformCodec<TOldFrom, TNewFrom, TOldTo, TNewTo>(codec, unmap, map): VariableSizeCodec<TNewFrom, TNewTo>

Transforms a codec by mapping its input and output values.

This function takes an existing Codec<A, B> and returns a Codec<C, D>, allowing:

  • Values of type C to be transformed into A before encoding.
  • Values of type B to be transformed into D after decoding.

This is useful for adapting codecs to work with different representations, handling default values, or converting between primitive and structured types.

Type Parameters

Type ParameterDescription
TOldFromThe original type expected by the codec.
TNewFromThe new type that will be transformed before encoding.
TOldToThe original type returned by the codec.
TNewToThe new type that will be transformed after decoding.

Parameters

ParameterTypeDescription
codecVariableSizeCodec<TOldFrom, TOldTo>The codec to transform.
unmap(value) => TOldFromA function that converts values of TNewFrom into TOldFrom before encoding.
map(value, bytes, offset) => TNewToA function that converts values of TOldTo into TNewTo after decoding (optional).

Returns

VariableSizeCodec<TNewFrom, TNewTo>

A new codec that encodes TNewFrom and decodes into TNewTo.

Example

Mapping a u32 codec to encode string lengths and decode them into 'x' characters.

const codec = transformCodec(
    getU32Codec(),
    (value: string) => value.length, // Encode string length
    (length) => 'x'.repeat(length)  // Decode length into a string of 'x's
);
 
const bytes = codec.encode("hello"); // 0x05000000 (stores length 5)
const value = codec.decode(bytes);   // "xxxxx"

Remarks

If only input transformation is needed, use transformEncoder. If only output transformation is needed, use transformDecoder.

const bytes = transformEncoder(getU32Encoder(), (value: string) => value.length).encode("hello");
const value = transformDecoder(getU32Decoder(), (length) => 'x'.repeat(length)).decode(bytes);

See

Call Signature

transformCodec<TOldFrom, TNewFrom, TOldTo, TNewTo>(codec, unmap, map): Codec<TNewFrom, TNewTo>

Transforms a codec by mapping its input and output values.

This function takes an existing Codec<A, B> and returns a Codec<C, D>, allowing:

  • Values of type C to be transformed into A before encoding.
  • Values of type B to be transformed into D after decoding.

This is useful for adapting codecs to work with different representations, handling default values, or converting between primitive and structured types.

Type Parameters

Type ParameterDescription
TOldFromThe original type expected by the codec.
TNewFromThe new type that will be transformed before encoding.
TOldToThe original type returned by the codec.
TNewToThe new type that will be transformed after decoding.

Parameters

ParameterTypeDescription
codecCodec<TOldFrom, TOldTo>The codec to transform.
unmap(value) => TOldFromA function that converts values of TNewFrom into TOldFrom before encoding.
map(value, bytes, offset) => TNewToA function that converts values of TOldTo into TNewTo after decoding (optional).

Returns

Codec<TNewFrom, TNewTo>

A new codec that encodes TNewFrom and decodes into TNewTo.

Example

Mapping a u32 codec to encode string lengths and decode them into 'x' characters.

const codec = transformCodec(
    getU32Codec(),
    (value: string) => value.length, // Encode string length
    (length) => 'x'.repeat(length)  // Decode length into a string of 'x's
);
 
const bytes = codec.encode("hello"); // 0x05000000 (stores length 5)
const value = codec.decode(bytes);   // "xxxxx"

Remarks

If only input transformation is needed, use transformEncoder. If only output transformation is needed, use transformDecoder.

const bytes = transformEncoder(getU32Encoder(), (value: string) => value.length).encode("hello");
const value = transformDecoder(getU32Decoder(), (length) => 'x'.repeat(length)).decode(bytes);

See