getPredicateCodec

Call Signature

function getPredicateCodec<TFrom, TTo, TSize>(
    encodePredicate,
    decodePredicate,
    ifTrue,
    ifFalse,
): FixedSizeCodec<TFrom, TTo, TSize>;

Returns a codec that selects between two codecs based on predicates.

This codec uses boolean predicate functions to determine which of two codecs to use for encoding and decoding. If the encoding predicate returns true for a value, the ifTrue codec is used to encode it; otherwise ifFalse. Similarly, if the decoding predicate returns true for the bytes, the ifTrue codec is used to decode them.

Type Parameters

Type ParameterDescription
TFromThe type of the value to encode.
TToThe type of the value to decode.
TSize extends number-

Parameters

ParameterTypeDescription
encodePredicate(value) => booleanA function that returns true or false for a given value.
decodePredicate(value) => booleanA function that returns true or false for a given byte array.
ifTrueFixedSizeCodec<TFrom, TTo, TSize>The codec to use when the respective predicate returns true.
ifFalseFixedSizeCodec<TFrom, TTo, TSize>The codec to use when the respective predicate returns false.

Returns

FixedSizeCodec<TFrom, TTo, TSize>

A Codec based on the provided codecs.

Example

Encoding and decoding small and large numbers differently.

const codec = getPredicateCodec(
  (n: number) => n < 256,
  bytes => bytes.length === 1,
  getU8Codec(),
  getU32Codec()
);
 
const smallBytes = codec.encode(42);
// 0x2a (encoded as u8)
 
const largeBytes = codec.encode(1000);
// 0xe8030000 (encoded as u32)
 
codec.decode(smallBytes); // 42
codec.decode(largeBytes); // 1000

See

Call Signature

function getPredicateCodec<TFrom, TTo>(
    encodePredicate,
    decodePredicate,
    ifTrue,
    ifFalse,
): FixedSizeCodec<TFrom, TTo>;

Returns a codec that selects between two codecs based on predicates.

This codec uses boolean predicate functions to determine which of two codecs to use for encoding and decoding. If the encoding predicate returns true for a value, the ifTrue codec is used to encode it; otherwise ifFalse. Similarly, if the decoding predicate returns true for the bytes, the ifTrue codec is used to decode them.

Type Parameters

Type ParameterDescription
TFromThe type of the value to encode.
TToThe type of the value to decode.

Parameters

ParameterTypeDescription
encodePredicate(value) => booleanA function that returns true or false for a given value.
decodePredicate(value) => booleanA function that returns true or false for a given byte array.
ifTrueFixedSizeCodec<TFrom, TTo>The codec to use when the respective predicate returns true.
ifFalseFixedSizeCodec<TFrom, TTo>The codec to use when the respective predicate returns false.

Returns

FixedSizeCodec<TFrom, TTo>

A Codec based on the provided codecs.

Example

Encoding and decoding small and large numbers differently.

const codec = getPredicateCodec(
  (n: number) => n < 256,
  bytes => bytes.length === 1,
  getU8Codec(),
  getU32Codec()
);
 
const smallBytes = codec.encode(42);
// 0x2a (encoded as u8)
 
const largeBytes = codec.encode(1000);
// 0xe8030000 (encoded as u32)
 
codec.decode(smallBytes); // 42
codec.decode(largeBytes); // 1000

See

Call Signature

function getPredicateCodec<TFrom, TTo>(
    encodePredicate,
    decodePredicate,
    ifTrue,
    ifFalse,
): VariableSizeCodec<TFrom, TTo>;

Returns a codec that selects between two codecs based on predicates.

This codec uses boolean predicate functions to determine which of two codecs to use for encoding and decoding. If the encoding predicate returns true for a value, the ifTrue codec is used to encode it; otherwise ifFalse. Similarly, if the decoding predicate returns true for the bytes, the ifTrue codec is used to decode them.

Type Parameters

Type ParameterDescription
TFromThe type of the value to encode.
TToThe type of the value to decode.

Parameters

ParameterTypeDescription
encodePredicate(value) => booleanA function that returns true or false for a given value.
decodePredicate(value) => booleanA function that returns true or false for a given byte array.
ifTrueVariableSizeCodec<TFrom, TTo>The codec to use when the respective predicate returns true.
ifFalseVariableSizeCodec<TFrom, TTo>The codec to use when the respective predicate returns false.

Returns

VariableSizeCodec<TFrom, TTo>

A Codec based on the provided codecs.

Example

Encoding and decoding small and large numbers differently.

const codec = getPredicateCodec(
  (n: number) => n < 256,
  bytes => bytes.length === 1,
  getU8Codec(),
  getU32Codec()
);
 
const smallBytes = codec.encode(42);
// 0x2a (encoded as u8)
 
const largeBytes = codec.encode(1000);
// 0xe8030000 (encoded as u32)
 
codec.decode(smallBytes); // 42
codec.decode(largeBytes); // 1000

See

Call Signature

function getPredicateCodec<TFrom, TTo>(
    encodePredicate,
    decodePredicate,
    ifTrue,
    ifFalse,
): Codec<TFrom, TTo>;

Returns a codec that selects between two codecs based on predicates.

This codec uses boolean predicate functions to determine which of two codecs to use for encoding and decoding. If the encoding predicate returns true for a value, the ifTrue codec is used to encode it; otherwise ifFalse. Similarly, if the decoding predicate returns true for the bytes, the ifTrue codec is used to decode them.

Type Parameters

Type ParameterDescription
TFromThe type of the value to encode.
TToThe type of the value to decode.

Parameters

ParameterTypeDescription
encodePredicate(value) => booleanA function that returns true or false for a given value.
decodePredicate(value) => booleanA function that returns true or false for a given byte array.
ifTrueCodec<TFrom, TTo>The codec to use when the respective predicate returns true.
ifFalseCodec<TFrom, TTo>The codec to use when the respective predicate returns false.

Returns

Codec<TFrom, TTo>

A Codec based on the provided codecs.

Example

Encoding and decoding small and large numbers differently.

const codec = getPredicateCodec(
  (n: number) => n < 256,
  bytes => bytes.length === 1,
  getU8Codec(),
  getU32Codec()
);
 
const smallBytes = codec.encode(42);
// 0x2a (encoded as u8)
 
const largeBytes = codec.encode(1000);
// 0xe8030000 (encoded as u32)
 
codec.decode(smallBytes); // 42
codec.decode(largeBytes); // 1000

See

On this page