Kit
Functions

transformEncoder

Call Signature

transformEncoder<TOldFrom, TNewFrom, TSize>(encoder, unmap): FixedSizeEncoder<TNewFrom, TSize>

Transforms an encoder by mapping its input values.

This function takes an existing Encoder<A> and returns an Encoder<B>, allowing values of type B to be converted into values of type A before encoding. The transformation is applied via the unmap function.

This is useful for handling type conversions, applying default values, or structuring data before encoding.

For more details, see transformCodec.

Type Parameters

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

Parameters

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

Returns

FixedSizeEncoder<TNewFrom, TSize>

A new encoder that accepts TNewFrom values and transforms them before encoding.

Example

Encoding a string by counting its characters and storing the length as a u32.

const encoder = transformEncoder(getU32Encoder(), (value: string) => value.length);
encoder.encode("hello"); // 0x05000000 (stores length 5)

See

Call Signature

transformEncoder<TOldFrom, TNewFrom>(encoder, unmap): VariableSizeEncoder<TNewFrom>

Transforms an encoder by mapping its input values.

This function takes an existing Encoder<A> and returns an Encoder<B>, allowing values of type B to be converted into values of type A before encoding. The transformation is applied via the unmap function.

This is useful for handling type conversions, applying default values, or structuring data before encoding.

For more details, see transformCodec.

Type Parameters

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

Parameters

ParameterTypeDescription
encoderVariableSizeEncoder<TOldFrom>The encoder to transform.
unmap(value) => TOldFromA function that converts values of TNewFrom into TOldFrom before encoding.

Returns

VariableSizeEncoder<TNewFrom>

A new encoder that accepts TNewFrom values and transforms them before encoding.

Example

Encoding a string by counting its characters and storing the length as a u32.

const encoder = transformEncoder(getU32Encoder(), (value: string) => value.length);
encoder.encode("hello"); // 0x05000000 (stores length 5)

See

Call Signature

transformEncoder<TOldFrom, TNewFrom>(encoder, unmap): Encoder<TNewFrom>

Transforms an encoder by mapping its input values.

This function takes an existing Encoder<A> and returns an Encoder<B>, allowing values of type B to be converted into values of type A before encoding. The transformation is applied via the unmap function.

This is useful for handling type conversions, applying default values, or structuring data before encoding.

For more details, see transformCodec.

Type Parameters

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

Parameters

ParameterTypeDescription
encoderEncoder<TOldFrom>The encoder to transform.
unmap(value) => TOldFromA function that converts values of TNewFrom into TOldFrom before encoding.

Returns

Encoder<TNewFrom>

A new encoder that accepts TNewFrom values and transforms them before encoding.

Example

Encoding a string by counting its characters and storing the length as a u32.

const encoder = transformEncoder(getU32Encoder(), (value: string) => value.length);
encoder.encode("hello"); // 0x05000000 (stores length 5)

See

On this page