Kit
Functions

getSetCodec

Call Signature

getSetCodec<TFrom, TTo>(item, config): FixedSizeCodec<Set<TFrom>, Set<TTo>, 0>

Returns a codec for encoding and decoding sets of items.

This codec serializes Set<T> values by encoding each item using the provided item codec. The number of items is stored as a prefix using a u32 codec by default.

Type Parameters

Type ParameterDefault typeDescription
TFrom-The type of the items in the set before encoding.
TToTFromThe type of the items in the set after decoding.

Parameters

ParameterTypeDescription
itemCodec<TFrom, TTo>The codec to use for each set item.
configSetCodecConfig<NumberCodec> & objectOptional configuration specifying the size strategy.

Returns

FixedSizeCodec<Set<TFrom>, Set<TTo>, 0>

A Codec<Set<TFrom>, Set<TTo>> for encoding and decoding sets.

Examples

Encoding and decoding a set of u8 numbers.

const codec = getSetCodec(getU8Codec());
const bytes = codec.encode(new Set([1, 2, 3]));
// 0x03000000010203
//   |       └-- 3 items of 1 byte each.
//   └-- 4-byte prefix indicating 3 items.
 
const value = codec.decode(bytes);
// new Set([1, 2, 3])

Using a u16 prefix for size.

const codec = getSetCodec(getU8Codec(), { size: getU16Codec() });
const bytes = codec.encode(new Set([1, 2, 3]));
// 0x0300010203
//   |   └-- 3 items of 1 byte each.
//   └-- 2-byte prefix indicating 3 items.

Using a fixed-size set.

const codec = getSetCodec(getU8Codec(), { size: 3 });
const bytes = codec.encode(new Set([1, 2, 3]));
// 0x010203
//   └-- Exactly 3 items of 1 byte each.

Using remainder to infer set size.

const codec = getSetCodec(getU8Codec(), { size: 'remainder' });
const bytes = codec.encode(new Set([1, 2, 3]));
// 0x010203
//   └-- 3 items of 1 byte each. The size is inferred from the remaining bytes.

Remarks

Separate getSetEncoder and getSetDecoder functions are available.

const bytes = getSetEncoder(getU8Encoder()).encode(new Set([1, 2, 3]));
const value = getSetDecoder(getU8Decoder()).decode(bytes);

See

Call Signature

getSetCodec<TFrom, TTo>(item, config): FixedSizeCodec<Set<TFrom>, Set<TTo>>

Returns a codec for encoding and decoding sets of items.

This codec serializes Set<T> values by encoding each item using the provided item codec. The number of items is stored as a prefix using a u32 codec by default.

Type Parameters

Type ParameterDefault typeDescription
TFrom-The type of the items in the set before encoding.
TToTFromThe type of the items in the set after decoding.

Parameters

ParameterTypeDescription
itemFixedSizeCodec<TFrom, TTo>The codec to use for each set item.
configSetCodecConfig<NumberCodec> & objectOptional configuration specifying the size strategy.

Returns

FixedSizeCodec<Set<TFrom>, Set<TTo>>

A Codec<Set<TFrom>, Set<TTo>> for encoding and decoding sets.

Examples

Encoding and decoding a set of u8 numbers.

const codec = getSetCodec(getU8Codec());
const bytes = codec.encode(new Set([1, 2, 3]));
// 0x03000000010203
//   |       └-- 3 items of 1 byte each.
//   └-- 4-byte prefix indicating 3 items.
 
const value = codec.decode(bytes);
// new Set([1, 2, 3])

Using a u16 prefix for size.

const codec = getSetCodec(getU8Codec(), { size: getU16Codec() });
const bytes = codec.encode(new Set([1, 2, 3]));
// 0x0300010203
//   |   └-- 3 items of 1 byte each.
//   └-- 2-byte prefix indicating 3 items.

Using a fixed-size set.

const codec = getSetCodec(getU8Codec(), { size: 3 });
const bytes = codec.encode(new Set([1, 2, 3]));
// 0x010203
//   └-- Exactly 3 items of 1 byte each.

Using remainder to infer set size.

const codec = getSetCodec(getU8Codec(), { size: 'remainder' });
const bytes = codec.encode(new Set([1, 2, 3]));
// 0x010203
//   └-- 3 items of 1 byte each. The size is inferred from the remaining bytes.

Remarks

Separate getSetEncoder and getSetDecoder functions are available.

const bytes = getSetEncoder(getU8Encoder()).encode(new Set([1, 2, 3]));
const value = getSetDecoder(getU8Decoder()).decode(bytes);

See

Call Signature

getSetCodec<TFrom, TTo>(item, config?): VariableSizeCodec<Set<TFrom>, Set<TTo>>

Returns a codec for encoding and decoding sets of items.

This codec serializes Set<T> values by encoding each item using the provided item codec. The number of items is stored as a prefix using a u32 codec by default.

Type Parameters

Type ParameterDefault typeDescription
TFrom-The type of the items in the set before encoding.
TToTFromThe type of the items in the set after decoding.

Parameters

ParameterTypeDescription
itemCodec<TFrom, TTo>The codec to use for each set item.
config?SetCodecConfig<NumberCodec>Optional configuration specifying the size strategy.

Returns

VariableSizeCodec<Set<TFrom>, Set<TTo>>

A Codec<Set<TFrom>, Set<TTo>> for encoding and decoding sets.

Examples

Encoding and decoding a set of u8 numbers.

const codec = getSetCodec(getU8Codec());
const bytes = codec.encode(new Set([1, 2, 3]));
// 0x03000000010203
//   |       └-- 3 items of 1 byte each.
//   └-- 4-byte prefix indicating 3 items.
 
const value = codec.decode(bytes);
// new Set([1, 2, 3])

Using a u16 prefix for size.

const codec = getSetCodec(getU8Codec(), { size: getU16Codec() });
const bytes = codec.encode(new Set([1, 2, 3]));
// 0x0300010203
//   |   └-- 3 items of 1 byte each.
//   └-- 2-byte prefix indicating 3 items.

Using a fixed-size set.

const codec = getSetCodec(getU8Codec(), { size: 3 });
const bytes = codec.encode(new Set([1, 2, 3]));
// 0x010203
//   └-- Exactly 3 items of 1 byte each.

Using remainder to infer set size.

const codec = getSetCodec(getU8Codec(), { size: 'remainder' });
const bytes = codec.encode(new Set([1, 2, 3]));
// 0x010203
//   └-- 3 items of 1 byte each. The size is inferred from the remaining bytes.

Remarks

Separate getSetEncoder and getSetDecoder functions are available.

const bytes = getSetEncoder(getU8Encoder()).encode(new Set([1, 2, 3]));
const value = getSetDecoder(getU8Decoder()).decode(bytes);

See

On this page