Kit
Functions

getOptionEncoder

Call Signature

getOptionEncoder<TFrom, TSize>(item, config): FixedSizeEncoder<OptionOrNullable<TFrom>, TSize>

Returns an encoder for optional values using the Option type.

This encoder serializes an OptionOrNullable 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 encoded as zeroes.
  • If noneValue is a byte array, None values are replaced with the provided constant.

Unlike getNullableEncoder, this encoder accepts both Option and Nullable values.

For more details, see getOptionCodec.

Type Parameters

Type ParameterDescription
TFromThe type of the main value being encoded.
TSize extends number-

Parameters

ParameterTypeDescription
itemFixedSizeEncoder<TFrom, TSize>The encoder for the value that may be present.
configOptionCodecConfig<NumberEncoder> & objectConfiguration options for encoding optional values.

Returns

FixedSizeEncoder<OptionOrNullable<TFrom>, TSize>

A FixedSizeEncoder or VariableSizeEncoder for encoding option values.

Example

Encoding an optional string.

const stringCodec = addCodecSizePrefix(getUtf8Codec(), getU32Codec());
const encoder = getOptionEncoder(stringCodec);
 
encoder.encode(some('Hi'));
encoder.encode('Hi');
// 0x01020000004869
//   | |       └-- utf8 string content ("Hi").
//   | └-- u32 string prefix (2 characters).
//   └-- 1-byte prefix (Some).
 
encoder.encode(none());
encoder.encode(null);
// 0x00
//   └-- 1-byte prefix (None).

See

getOptionCodec

Call Signature

getOptionEncoder<TFrom>(item, config): FixedSizeEncoder<OptionOrNullable<TFrom>>

Returns an encoder for optional values using the Option type.

This encoder serializes an OptionOrNullable 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 encoded as zeroes.
  • If noneValue is a byte array, None values are replaced with the provided constant.

Unlike getNullableEncoder, this encoder accepts both Option and Nullable values.

For more details, see getOptionCodec.

Type Parameters

Type ParameterDescription
TFromThe type of the main value being encoded.

Parameters

ParameterTypeDescription
itemFixedSizeEncoder<TFrom>The encoder for the value that may be present.
configOptionCodecConfig<FixedSizeNumberEncoder> & objectConfiguration options for encoding optional values.

Returns

FixedSizeEncoder<OptionOrNullable<TFrom>>

A FixedSizeEncoder or VariableSizeEncoder for encoding option values.

Example

Encoding an optional string.

const stringCodec = addCodecSizePrefix(getUtf8Codec(), getU32Codec());
const encoder = getOptionEncoder(stringCodec);
 
encoder.encode(some('Hi'));
encoder.encode('Hi');
// 0x01020000004869
//   | |       └-- utf8 string content ("Hi").
//   | └-- u32 string prefix (2 characters).
//   └-- 1-byte prefix (Some).
 
encoder.encode(none());
encoder.encode(null);
// 0x00
//   └-- 1-byte prefix (None).

See

getOptionCodec

Call Signature

getOptionEncoder<TFrom>(item, config): VariableSizeEncoder<OptionOrNullable<TFrom>>

Returns an encoder for optional values using the Option type.

This encoder serializes an OptionOrNullable 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 encoded as zeroes.
  • If noneValue is a byte array, None values are replaced with the provided constant.

Unlike getNullableEncoder, this encoder accepts both Option and Nullable values.

For more details, see getOptionCodec.

Type Parameters

Type ParameterDescription
TFromThe type of the main value being encoded.

Parameters

ParameterTypeDescription
itemFixedSizeEncoder<TFrom>The encoder for the value that may be present.
configOptionCodecConfig<NumberEncoder> & objectConfiguration options for encoding optional values.

Returns

VariableSizeEncoder<OptionOrNullable<TFrom>>

A FixedSizeEncoder or VariableSizeEncoder for encoding option values.

Example

Encoding an optional string.

const stringCodec = addCodecSizePrefix(getUtf8Codec(), getU32Codec());
const encoder = getOptionEncoder(stringCodec);
 
encoder.encode(some('Hi'));
encoder.encode('Hi');
// 0x01020000004869
//   | |       └-- utf8 string content ("Hi").
//   | └-- u32 string prefix (2 characters).
//   └-- 1-byte prefix (Some).
 
encoder.encode(none());
encoder.encode(null);
// 0x00
//   └-- 1-byte prefix (None).

See

getOptionCodec

Call Signature

getOptionEncoder<TFrom>(item, config?): VariableSizeEncoder<OptionOrNullable<TFrom>>

Returns an encoder for optional values using the Option type.

This encoder serializes an OptionOrNullable 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 encoded as zeroes.
  • If noneValue is a byte array, None values are replaced with the provided constant.

Unlike getNullableEncoder, this encoder accepts both Option and Nullable values.

For more details, see getOptionCodec.

Type Parameters

Type ParameterDescription
TFromThe type of the main value being encoded.

Parameters

ParameterTypeDescription
itemEncoder<TFrom>The encoder for the value that may be present.
config?OptionCodecConfig<NumberEncoder> & objectConfiguration options for encoding optional values.

Returns

VariableSizeEncoder<OptionOrNullable<TFrom>>

A FixedSizeEncoder or VariableSizeEncoder for encoding option values.

Example

Encoding an optional string.

const stringCodec = addCodecSizePrefix(getUtf8Codec(), getU32Codec());
const encoder = getOptionEncoder(stringCodec);
 
encoder.encode(some('Hi'));
encoder.encode('Hi');
// 0x01020000004869
//   | |       └-- utf8 string content ("Hi").
//   | └-- u32 string prefix (2 characters).
//   └-- 1-byte prefix (Some).
 
encoder.encode(none());
encoder.encode(null);
// 0x00
//   └-- 1-byte prefix (None).

See

getOptionCodec

On this page