getPredicateDecoder

Call Signature

function getPredicateDecoder<TTo, TSize>(
    predicate,
    ifTrue,
    ifFalse,
): FixedSizeDecoder<TTo, TSize>;

Returns a decoder that selects between two decoders based on a predicate.

This decoder uses a boolean predicate function on the raw bytes to determine which of two decoders to use. If the predicate returns true, the ifTrue decoder is used; otherwise, the ifFalse decoder is used.

Type Parameters

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

Parameters

ParameterTypeDescription
predicate(value) => booleanA function that returns true or false for a given byte array.
ifTrueFixedSizeDecoder<TTo, TSize>The decoder to use when the predicate returns true.
ifFalseFixedSizeDecoder<TTo, TSize>The decoder to use when the predicate returns false.

Returns

FixedSizeDecoder<TTo, TSize>

A Decoder based on the provided decoders.

Example

Decoding small and large numbers based on byte length.

const decoder = getPredicateDecoder(
  bytes => bytes.length === 1,
  getU8Decoder(),
  getU32Decoder()
);
 
decoder.decode(new Uint8Array([0x2a]));
// 42 (decoded as u8)
 
decoder.decode(new Uint8Array([0xe8, 0x03, 0x00, 0x00]));
// 1000 (decoded as u32)

See

getPredicateCodec

Call Signature

function getPredicateDecoder<TTo>(
    predicate,
    ifTrue,
    ifFalse,
): FixedSizeDecoder<TTo>;

Returns a decoder that selects between two decoders based on a predicate.

This decoder uses a boolean predicate function on the raw bytes to determine which of two decoders to use. If the predicate returns true, the ifTrue decoder is used; otherwise, the ifFalse decoder is used.

Type Parameters

Type ParameterDescription
TToThe type of the value to decode.

Parameters

ParameterTypeDescription
predicate(value) => booleanA function that returns true or false for a given byte array.
ifTrueFixedSizeDecoder<TTo>The decoder to use when the predicate returns true.
ifFalseFixedSizeDecoder<TTo>The decoder to use when the predicate returns false.

Returns

FixedSizeDecoder<TTo>

A Decoder based on the provided decoders.

Example

Decoding small and large numbers based on byte length.

const decoder = getPredicateDecoder(
  bytes => bytes.length === 1,
  getU8Decoder(),
  getU32Decoder()
);
 
decoder.decode(new Uint8Array([0x2a]));
// 42 (decoded as u8)
 
decoder.decode(new Uint8Array([0xe8, 0x03, 0x00, 0x00]));
// 1000 (decoded as u32)

See

getPredicateCodec

Call Signature

function getPredicateDecoder<TTo>(
    predicate,
    ifTrue,
    ifFalse,
): VariableSizeDecoder<TTo>;

Returns a decoder that selects between two decoders based on a predicate.

This decoder uses a boolean predicate function on the raw bytes to determine which of two decoders to use. If the predicate returns true, the ifTrue decoder is used; otherwise, the ifFalse decoder is used.

Type Parameters

Type ParameterDescription
TToThe type of the value to decode.

Parameters

ParameterTypeDescription
predicate(value) => booleanA function that returns true or false for a given byte array.
ifTrueVariableSizeDecoder<TTo>The decoder to use when the predicate returns true.
ifFalseVariableSizeDecoder<TTo>The decoder to use when the predicate returns false.

Returns

VariableSizeDecoder<TTo>

A Decoder based on the provided decoders.

Example

Decoding small and large numbers based on byte length.

const decoder = getPredicateDecoder(
  bytes => bytes.length === 1,
  getU8Decoder(),
  getU32Decoder()
);
 
decoder.decode(new Uint8Array([0x2a]));
// 42 (decoded as u8)
 
decoder.decode(new Uint8Array([0xe8, 0x03, 0x00, 0x00]));
// 1000 (decoded as u32)

See

getPredicateCodec

Call Signature

function getPredicateDecoder<TTo>(
    predicate,
    ifTrue,
    ifFalse,
): Decoder<TTo>;

Returns a decoder that selects between two decoders based on a predicate.

This decoder uses a boolean predicate function on the raw bytes to determine which of two decoders to use. If the predicate returns true, the ifTrue decoder is used; otherwise, the ifFalse decoder is used.

Type Parameters

Type ParameterDescription
TToThe type of the value to decode.

Parameters

ParameterTypeDescription
predicate(value) => booleanA function that returns true or false for a given byte array.
ifTrueDecoder<TTo>The decoder to use when the predicate returns true.
ifFalseDecoder<TTo>The decoder to use when the predicate returns false.

Returns

Decoder<TTo>

A Decoder based on the provided decoders.

Example

Decoding small and large numbers based on byte length.

const decoder = getPredicateDecoder(
  bytes => bytes.length === 1,
  getU8Decoder(),
  getU32Decoder()
);
 
decoder.decode(new Uint8Array([0x2a]));
// 42 (decoded as u8)
 
decoder.decode(new Uint8Array([0xe8, 0x03, 0x00, 0x00]));
// 1000 (decoded as u32)

See

getPredicateCodec

On this page