Kit
Functions

getBooleanCodec

Call Signature

getBooleanCodec(): FixedSizeCodec<boolean, boolean, 1>

Returns a codec for encoding and decoding boolean values.

By default, booleans are stored as a u8 (1 for true, 0 for false). The size option allows customizing the number codec used for storage.

Returns

FixedSizeCodec<boolean, boolean, 1>

A FixedSizeCodec<boolean, boolean, N> where N is the size of the number codec.

Examples

Encoding and decoding booleans using a u8 (default).

const codec = getBooleanCodec();
 
codec.encode(false); // 0x00
codec.encode(true);  // 0x01
 
codec.decode(new Uint8Array([0x00])); // false
codec.decode(new Uint8Array([0x01])); // true

Encoding and decoding booleans using a custom number codec.

const codec = getBooleanCodec({ size: getU16Codec() });
 
codec.encode(false); // 0x0000
codec.encode(true);  // 0x0100
 
codec.decode(new Uint8Array([0x00, 0x00])); // false
codec.decode(new Uint8Array([0x01, 0x00])); // true

Remarks

Separate getBooleanEncoder and getBooleanDecoder functions are available.

const bytes = getBooleanEncoder().encode(true);
const value = getBooleanDecoder().decode(bytes);

See

Call Signature

getBooleanCodec<TSize>(config): FixedSizeCodec<boolean, boolean, TSize>

Returns a codec for encoding and decoding boolean values.

By default, booleans are stored as a u8 (1 for true, 0 for false). The size option allows customizing the number codec used for storage.

Type Parameters

Type Parameter
TSize extends number

Parameters

ParameterTypeDescription
configBooleanCodecConfig<NumberCodec> & objectConfiguration options for encoding and decoding booleans.

Returns

FixedSizeCodec<boolean, boolean, TSize>

A FixedSizeCodec<boolean, boolean, N> where N is the size of the number codec.

Examples

Encoding and decoding booleans using a u8 (default).

const codec = getBooleanCodec();
 
codec.encode(false); // 0x00
codec.encode(true);  // 0x01
 
codec.decode(new Uint8Array([0x00])); // false
codec.decode(new Uint8Array([0x01])); // true

Encoding and decoding booleans using a custom number codec.

const codec = getBooleanCodec({ size: getU16Codec() });
 
codec.encode(false); // 0x0000
codec.encode(true);  // 0x0100
 
codec.decode(new Uint8Array([0x00, 0x00])); // false
codec.decode(new Uint8Array([0x01, 0x00])); // true

Remarks

Separate getBooleanEncoder and getBooleanDecoder functions are available.

const bytes = getBooleanEncoder().encode(true);
const value = getBooleanDecoder().decode(bytes);

See

Call Signature

getBooleanCodec(config): VariableSizeCodec<boolean>

Returns a codec for encoding and decoding boolean values.

By default, booleans are stored as a u8 (1 for true, 0 for false). The size option allows customizing the number codec used for storage.

Parameters

ParameterTypeDescription
configBooleanCodecConfig<NumberCodec>Configuration options for encoding and decoding booleans.

Returns

VariableSizeCodec<boolean>

A FixedSizeCodec<boolean, boolean, N> where N is the size of the number codec.

Examples

Encoding and decoding booleans using a u8 (default).

const codec = getBooleanCodec();
 
codec.encode(false); // 0x00
codec.encode(true);  // 0x01
 
codec.decode(new Uint8Array([0x00])); // false
codec.decode(new Uint8Array([0x01])); // true

Encoding and decoding booleans using a custom number codec.

const codec = getBooleanCodec({ size: getU16Codec() });
 
codec.encode(false); // 0x0000
codec.encode(true);  // 0x0100
 
codec.decode(new Uint8Array([0x00, 0x00])); // false
codec.decode(new Uint8Array([0x01, 0x00])); // true

Remarks

Separate getBooleanEncoder and getBooleanDecoder functions are available.

const bytes = getBooleanEncoder().encode(true);
const value = getBooleanDecoder().decode(bytes);

See

On this page