Kit
Functions

getOptionDecoder

Call Signature

getOptionDecoder<TTo, TSize>(item, config): FixedSizeDecoder<Option<TTo>, TSize>

Returns a decoder for optional values using the Option type.

This decoder deserializes an Option<T> value using a configurable approach:

  • By default, a u8 prefix is used (0 = None, 1 = Some). This can be customized or disabled.
  • If noneValue: 'zeroes' is set, None values are identified by zeroes.
  • If noneValue is a byte array, None values match the provided constant.

Unlike getNullableDecoder, this decoder always outputs an Option type.

For more details, see getOptionCodec.

Type Parameters

Type ParameterDescription
TToThe type of the main value being decoded.
TSize extends number-

Parameters

ParameterTypeDescription
itemFixedSizeDecoder<TTo, TSize>The decoder for the value that may be present.
configOptionCodecConfig<NumberDecoder> & objectConfiguration options for decoding optional values.

Returns

FixedSizeDecoder<Option<TTo>, TSize>

A FixedSizeDecoder or VariableSizeDecoder for decoding option values.

Example

Decoding an optional string with a size prefix.

const stringCodec = addCodecSizePrefix(getUtf8Codec(), getU32Codec());
const decoder = getOptionDecoder(stringCodec);
 
decoder.decode(new Uint8Array([0x01, 0x02, 0x00, 0x00, 0x00, 0x48, 0x69]));
// some('Hi')
 
decoder.decode(new Uint8Array([0x00]));
// none()

See

getOptionCodec

Call Signature

getOptionDecoder<TTo>(item, config): FixedSizeDecoder<Option<TTo>>

Returns a decoder for optional values using the Option type.

This decoder deserializes an Option<T> value using a configurable approach:

  • By default, a u8 prefix is used (0 = None, 1 = Some). This can be customized or disabled.
  • If noneValue: 'zeroes' is set, None values are identified by zeroes.
  • If noneValue is a byte array, None values match the provided constant.

Unlike getNullableDecoder, this decoder always outputs an Option type.

For more details, see getOptionCodec.

Type Parameters

Type ParameterDescription
TToThe type of the main value being decoded.

Parameters

ParameterTypeDescription
itemFixedSizeDecoder<TTo>The decoder for the value that may be present.
configOptionCodecConfig<FixedSizeNumberDecoder> & objectConfiguration options for decoding optional values.

Returns

FixedSizeDecoder<Option<TTo>>

A FixedSizeDecoder or VariableSizeDecoder for decoding option values.

Example

Decoding an optional string with a size prefix.

const stringCodec = addCodecSizePrefix(getUtf8Codec(), getU32Codec());
const decoder = getOptionDecoder(stringCodec);
 
decoder.decode(new Uint8Array([0x01, 0x02, 0x00, 0x00, 0x00, 0x48, 0x69]));
// some('Hi')
 
decoder.decode(new Uint8Array([0x00]));
// none()

See

getOptionCodec

Call Signature

getOptionDecoder<TTo>(item, config): VariableSizeDecoder<Option<TTo>>

Returns a decoder for optional values using the Option type.

This decoder deserializes an Option<T> value using a configurable approach:

  • By default, a u8 prefix is used (0 = None, 1 = Some). This can be customized or disabled.
  • If noneValue: 'zeroes' is set, None values are identified by zeroes.
  • If noneValue is a byte array, None values match the provided constant.

Unlike getNullableDecoder, this decoder always outputs an Option type.

For more details, see getOptionCodec.

Type Parameters

Type ParameterDescription
TToThe type of the main value being decoded.

Parameters

ParameterTypeDescription
itemFixedSizeDecoder<TTo>The decoder for the value that may be present.
configOptionCodecConfig<NumberDecoder> & objectConfiguration options for decoding optional values.

Returns

VariableSizeDecoder<Option<TTo>>

A FixedSizeDecoder or VariableSizeDecoder for decoding option values.

Example

Decoding an optional string with a size prefix.

const stringCodec = addCodecSizePrefix(getUtf8Codec(), getU32Codec());
const decoder = getOptionDecoder(stringCodec);
 
decoder.decode(new Uint8Array([0x01, 0x02, 0x00, 0x00, 0x00, 0x48, 0x69]));
// some('Hi')
 
decoder.decode(new Uint8Array([0x00]));
// none()

See

getOptionCodec

Call Signature

getOptionDecoder<TTo>(item, config?): VariableSizeDecoder<Option<TTo>>

Returns a decoder for optional values using the Option type.

This decoder deserializes an Option<T> value using a configurable approach:

  • By default, a u8 prefix is used (0 = None, 1 = Some). This can be customized or disabled.
  • If noneValue: 'zeroes' is set, None values are identified by zeroes.
  • If noneValue is a byte array, None values match the provided constant.

Unlike getNullableDecoder, this decoder always outputs an Option type.

For more details, see getOptionCodec.

Type Parameters

Type ParameterDescription
TToThe type of the main value being decoded.

Parameters

ParameterTypeDescription
itemDecoder<TTo>The decoder for the value that may be present.
config?OptionCodecConfig<NumberDecoder> & objectConfiguration options for decoding optional values.

Returns

VariableSizeDecoder<Option<TTo>>

A FixedSizeDecoder or VariableSizeDecoder for decoding option values.

Example

Decoding an optional string with a size prefix.

const stringCodec = addCodecSizePrefix(getUtf8Codec(), getU32Codec());
const decoder = getOptionDecoder(stringCodec);
 
decoder.decode(new Uint8Array([0x01, 0x02, 0x00, 0x00, 0x00, 0x48, 0x69]));
// some('Hi')
 
decoder.decode(new Uint8Array([0x00]));
// none()

See

getOptionCodec

On this page