Kit
Functions

getEnumCodec

Call Signature

getEnumCodec<TEnum>(constructor, config?): FixedSizeCodec<GetEnumFrom<TEnum>, GetEnumTo<TEnum>, 1>

Returns a codec for encoding and decoding enums.

This codec serializes enums as a numerical discriminator, allowing them to be efficiently stored and reconstructed from binary data.

By default, the discriminator is derived from the positional index of the enum variant, but it can be configured to use the enum's numeric values instead.

Type Parameters

Type ParameterDescription
TEnum extends EnumLookupObjectThe TypeScript enum or object mapping enum keys to values.

Parameters

ParameterTypeDescription
constructorTEnumThe constructor of the enum.
config?Omit<EnumCodecConfig<NumberCodec>, "size">Configuration options for encoding and decoding the enum.

Returns

FixedSizeCodec<GetEnumFrom<TEnum>, GetEnumTo<TEnum>, 1>

A FixedSizeCodec or VariableSizeCodec for encoding and decoding enums.

Examples

Encoding and decoding enums using positional indexes.

enum Direction { Up, Down, Left, Right }
const codec = getEnumCodec(Direction);
 
codec.encode(Direction.Up);    // 0x00
codec.encode(Direction.Down);  // 0x01
codec.encode(Direction.Left);  // 0x02
codec.encode(Direction.Right); // 0x03
 
codec.decode(new Uint8Array([0x00])); // Direction.Up
codec.decode(new Uint8Array([0x01])); // Direction.Down
codec.decode(new Uint8Array([0x02])); // Direction.Left
codec.decode(new Uint8Array([0x03])); // Direction.Right

Encoding and decoding enums using their numeric values.

enum GameDifficulty { Easy = 1, Normal = 4, Hard = 7, Expert = 9 }
const codec = getEnumCodec(GameDifficulty, { useValuesAsDiscriminators: true });
 
codec.encode(GameDifficulty.Easy);   // 0x01
codec.encode(GameDifficulty.Normal); // 0x04
codec.encode(GameDifficulty.Hard);   // 0x07
codec.encode(GameDifficulty.Expert); // 0x09
 
codec.decode(new Uint8Array([0x01])); // GameDifficulty.Easy
codec.decode(new Uint8Array([0x04])); // GameDifficulty.Normal
codec.decode(new Uint8Array([0x07])); // GameDifficulty.Hard
codec.decode(new Uint8Array([0x09])); // GameDifficulty.Expert

Note that, when using values as discriminators, the enum values must be numerical. Otherwise, an error will be thrown.

enum GameDifficulty { Easy = 'EASY', Normal = 'NORMAL', Hard = 'HARD' }
getEnumCodec(GameDifficulty, { useValuesAsDiscriminators: true }); // Throws an error.

Using a custom discriminator size.

enum Status { Pending, Approved, Rejected }
const codec = getEnumCodec(Status, { size: getU16Codec() });
 
codec.encode(Status.Pending);  // 0x0000
codec.encode(Status.Approved); // 0x0100
codec.encode(Status.Rejected); // 0x0200
 
codec.decode(new Uint8Array([0x00, 0x00])); // Status.Pending
codec.decode(new Uint8Array([0x01, 0x00])); // Status.Approved
codec.decode(new Uint8Array([0x02, 0x00])); // Status.Rejected

Remarks

Separate getEnumEncoder and getEnumDecoder functions are available.

const bytes = getEnumEncoder(Direction).encode(Direction.Up);
const value = getEnumDecoder(Direction).decode(bytes);

See

Call Signature

getEnumCodec<TEnum, TSize>(constructor, config): FixedSizeCodec<GetEnumFrom<TEnum>, GetEnumTo<TEnum>, TSize>

Returns a codec for encoding and decoding enums.

This codec serializes enums as a numerical discriminator, allowing them to be efficiently stored and reconstructed from binary data.

By default, the discriminator is derived from the positional index of the enum variant, but it can be configured to use the enum's numeric values instead.

Type Parameters

Type ParameterDescription
TEnum extends EnumLookupObjectThe TypeScript enum or object mapping enum keys to values.
TSize extends number-

Parameters

ParameterTypeDescription
constructorTEnumThe constructor of the enum.
configEnumCodecConfig<NumberCodec> & objectConfiguration options for encoding and decoding the enum.

Returns

FixedSizeCodec<GetEnumFrom<TEnum>, GetEnumTo<TEnum>, TSize>

A FixedSizeCodec or VariableSizeCodec for encoding and decoding enums.

Examples

Encoding and decoding enums using positional indexes.

enum Direction { Up, Down, Left, Right }
const codec = getEnumCodec(Direction);
 
codec.encode(Direction.Up);    // 0x00
codec.encode(Direction.Down);  // 0x01
codec.encode(Direction.Left);  // 0x02
codec.encode(Direction.Right); // 0x03
 
codec.decode(new Uint8Array([0x00])); // Direction.Up
codec.decode(new Uint8Array([0x01])); // Direction.Down
codec.decode(new Uint8Array([0x02])); // Direction.Left
codec.decode(new Uint8Array([0x03])); // Direction.Right

Encoding and decoding enums using their numeric values.

enum GameDifficulty { Easy = 1, Normal = 4, Hard = 7, Expert = 9 }
const codec = getEnumCodec(GameDifficulty, { useValuesAsDiscriminators: true });
 
codec.encode(GameDifficulty.Easy);   // 0x01
codec.encode(GameDifficulty.Normal); // 0x04
codec.encode(GameDifficulty.Hard);   // 0x07
codec.encode(GameDifficulty.Expert); // 0x09
 
codec.decode(new Uint8Array([0x01])); // GameDifficulty.Easy
codec.decode(new Uint8Array([0x04])); // GameDifficulty.Normal
codec.decode(new Uint8Array([0x07])); // GameDifficulty.Hard
codec.decode(new Uint8Array([0x09])); // GameDifficulty.Expert

Note that, when using values as discriminators, the enum values must be numerical. Otherwise, an error will be thrown.

enum GameDifficulty { Easy = 'EASY', Normal = 'NORMAL', Hard = 'HARD' }
getEnumCodec(GameDifficulty, { useValuesAsDiscriminators: true }); // Throws an error.

Using a custom discriminator size.

enum Status { Pending, Approved, Rejected }
const codec = getEnumCodec(Status, { size: getU16Codec() });
 
codec.encode(Status.Pending);  // 0x0000
codec.encode(Status.Approved); // 0x0100
codec.encode(Status.Rejected); // 0x0200
 
codec.decode(new Uint8Array([0x00, 0x00])); // Status.Pending
codec.decode(new Uint8Array([0x01, 0x00])); // Status.Approved
codec.decode(new Uint8Array([0x02, 0x00])); // Status.Rejected

Remarks

Separate getEnumEncoder and getEnumDecoder functions are available.

const bytes = getEnumEncoder(Direction).encode(Direction.Up);
const value = getEnumDecoder(Direction).decode(bytes);

See

Call Signature

getEnumCodec<TEnum>(constructor, config?): VariableSizeCodec<GetEnumFrom<TEnum>, GetEnumTo<TEnum>>

Returns a codec for encoding and decoding enums.

This codec serializes enums as a numerical discriminator, allowing them to be efficiently stored and reconstructed from binary data.

By default, the discriminator is derived from the positional index of the enum variant, but it can be configured to use the enum's numeric values instead.

Type Parameters

Type ParameterDescription
TEnum extends EnumLookupObjectThe TypeScript enum or object mapping enum keys to values.

Parameters

ParameterTypeDescription
constructorTEnumThe constructor of the enum.
config?EnumCodecConfig<NumberCodec>Configuration options for encoding and decoding the enum.

Returns

VariableSizeCodec<GetEnumFrom<TEnum>, GetEnumTo<TEnum>>

A FixedSizeCodec or VariableSizeCodec for encoding and decoding enums.

Examples

Encoding and decoding enums using positional indexes.

enum Direction { Up, Down, Left, Right }
const codec = getEnumCodec(Direction);
 
codec.encode(Direction.Up);    // 0x00
codec.encode(Direction.Down);  // 0x01
codec.encode(Direction.Left);  // 0x02
codec.encode(Direction.Right); // 0x03
 
codec.decode(new Uint8Array([0x00])); // Direction.Up
codec.decode(new Uint8Array([0x01])); // Direction.Down
codec.decode(new Uint8Array([0x02])); // Direction.Left
codec.decode(new Uint8Array([0x03])); // Direction.Right

Encoding and decoding enums using their numeric values.

enum GameDifficulty { Easy = 1, Normal = 4, Hard = 7, Expert = 9 }
const codec = getEnumCodec(GameDifficulty, { useValuesAsDiscriminators: true });
 
codec.encode(GameDifficulty.Easy);   // 0x01
codec.encode(GameDifficulty.Normal); // 0x04
codec.encode(GameDifficulty.Hard);   // 0x07
codec.encode(GameDifficulty.Expert); // 0x09
 
codec.decode(new Uint8Array([0x01])); // GameDifficulty.Easy
codec.decode(new Uint8Array([0x04])); // GameDifficulty.Normal
codec.decode(new Uint8Array([0x07])); // GameDifficulty.Hard
codec.decode(new Uint8Array([0x09])); // GameDifficulty.Expert

Note that, when using values as discriminators, the enum values must be numerical. Otherwise, an error will be thrown.

enum GameDifficulty { Easy = 'EASY', Normal = 'NORMAL', Hard = 'HARD' }
getEnumCodec(GameDifficulty, { useValuesAsDiscriminators: true }); // Throws an error.

Using a custom discriminator size.

enum Status { Pending, Approved, Rejected }
const codec = getEnumCodec(Status, { size: getU16Codec() });
 
codec.encode(Status.Pending);  // 0x0000
codec.encode(Status.Approved); // 0x0100
codec.encode(Status.Rejected); // 0x0200
 
codec.decode(new Uint8Array([0x00, 0x00])); // Status.Pending
codec.decode(new Uint8Array([0x01, 0x00])); // Status.Approved
codec.decode(new Uint8Array([0x02, 0x00])); // Status.Rejected

Remarks

Separate getEnumEncoder and getEnumDecoder functions are available.

const bytes = getEnumEncoder(Direction).encode(Direction.Up);
const value = getEnumDecoder(Direction).decode(bytes);

See

On this page