Kit
Functions

transformDecoder

Call Signature

transformDecoder<TOldTo, TNewTo, TSize>(decoder, map): FixedSizeDecoder<TNewTo, TSize>

Transforms a decoder by mapping its output values.

This function takes an existing Decoder<A> and returns a Decoder<B>, allowing values of type A to be converted into values of type B after decoding. The transformation is applied via the map function.

This is useful for post-processing, type conversions, or enriching decoded data.

For more details, see transformCodec.

Type Parameters

Type ParameterDescription
TOldToThe original type returned by the decoder.
TNewToThe new type that will be transformed after decoding.
TSize extends number-

Parameters

ParameterTypeDescription
decoderFixedSizeDecoder<TOldTo, TSize>The decoder to transform.
map(value, bytes, offset) => TNewToA function that converts values of TOldTo into TNewTo after decoding.

Returns

FixedSizeDecoder<TNewTo, TSize>

A new decoder that decodes into TNewTo.

Example

Decoding a stored u32 length into a string of 'x' characters.

const decoder = transformDecoder(getU32Decoder(), (length) => 'x'.repeat(length));
decoder.decode(new Uint8Array([0x05, 0x00, 0x00, 0x00])); // "xxxxx"

See

Call Signature

transformDecoder<TOldTo, TNewTo>(decoder, map): VariableSizeDecoder<TNewTo>

Transforms a decoder by mapping its output values.

This function takes an existing Decoder<A> and returns a Decoder<B>, allowing values of type A to be converted into values of type B after decoding. The transformation is applied via the map function.

This is useful for post-processing, type conversions, or enriching decoded data.

For more details, see transformCodec.

Type Parameters

Type ParameterDescription
TOldToThe original type returned by the decoder.
TNewToThe new type that will be transformed after decoding.

Parameters

ParameterTypeDescription
decoderVariableSizeDecoder<TOldTo>The decoder to transform.
map(value, bytes, offset) => TNewToA function that converts values of TOldTo into TNewTo after decoding.

Returns

VariableSizeDecoder<TNewTo>

A new decoder that decodes into TNewTo.

Example

Decoding a stored u32 length into a string of 'x' characters.

const decoder = transformDecoder(getU32Decoder(), (length) => 'x'.repeat(length));
decoder.decode(new Uint8Array([0x05, 0x00, 0x00, 0x00])); // "xxxxx"

See

Call Signature

transformDecoder<TOldTo, TNewTo>(decoder, map): Decoder<TNewTo>

Transforms a decoder by mapping its output values.

This function takes an existing Decoder<A> and returns a Decoder<B>, allowing values of type A to be converted into values of type B after decoding. The transformation is applied via the map function.

This is useful for post-processing, type conversions, or enriching decoded data.

For more details, see transformCodec.

Type Parameters

Type ParameterDescription
TOldToThe original type returned by the decoder.
TNewToThe new type that will be transformed after decoding.

Parameters

ParameterTypeDescription
decoderDecoder<TOldTo>The decoder to transform.
map(value, bytes, offset) => TNewToA function that converts values of TOldTo into TNewTo after decoding.

Returns

Decoder<TNewTo>

A new decoder that decodes into TNewTo.

Example

Decoding a stored u32 length into a string of 'x' characters.

const decoder = transformDecoder(getU32Decoder(), (length) => 'x'.repeat(length));
decoder.decode(new Uint8Array([0x05, 0x00, 0x00, 0x00])); // "xxxxx"

See

On this page