getPatternMatchEncoder

Call Signature

function getPatternMatchEncoder<TFrom, TSize>(
    patterns,
): FixedSizeEncoder<TFrom, TSize>;

Returns an encoder that selects which variant encoder to use based on pattern matching.

This encoder evaluates the value against a series of predicate functions in order, and uses the first matching encoder to encode the value.

Type Parameters

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

Parameters

ParameterTypeDescription
patternsFixedSizePatternMatchEncoderEntry<TFrom, TFrom, TSize>[]An array of [predicate, encoder] pairs. Predicates are tested in order and the first matching encoder is used to encode the value. Note that predicates can be either type predicates that narrow the type of the value, or boolean predicates. If using type predicates, the encoder can be for the narrowed type.

Returns

FixedSizeEncoder<TFrom, TSize>

An encoder that selects the appropriate variant based on the matched pattern.

Throws

Throws a SOLANA_ERROR__CODECS__INVALID_PATTERN_MATCH_VALUE error if the value does not match any of the specified patterns.

Example

Encoding values using pattern matching.

const encoder = getPatternMatchEncoder([
  [(n: number) => n < 256, getU8Encoder()],
  [(n: number) => n < 2 ** 16, getU16Encoder()],
  [(n: number) => n < 2 ** 32, getU32Encoder()]
]);
 
encoder.encode(42);
// 0x2a
//  └── Small number encoded as u8
 
encoder.encode(1000);
// 0xe803
//   └── Medium number encoded as u16
 
encoder.encode(100_000);
// 0xa0860100
//   └── Large number encoded as u32
 
ender.encode(2 ** 32 + 1);
// Throws an error because the value does not match any pattern

See

getPatternMatchCodec

Call Signature

function getPatternMatchEncoder<TFrom>(
    patterns,
): FixedSizeEncoder<TFrom>;

Returns an encoder that selects which variant encoder to use based on pattern matching.

This encoder evaluates the value against a series of predicate functions in order, and uses the first matching encoder to encode the value.

Type Parameters

Type ParameterDescription
TFromThe type of the value to encode.

Parameters

ParameterTypeDescription
patternsFixedSizePatternMatchEncoderEntry<TFrom, TFrom, number>[]An array of [predicate, encoder] pairs. Predicates are tested in order and the first matching encoder is used to encode the value. Note that predicates can be either type predicates that narrow the type of the value, or boolean predicates. If using type predicates, the encoder can be for the narrowed type.

Returns

FixedSizeEncoder<TFrom>

An encoder that selects the appropriate variant based on the matched pattern.

Throws

Throws a SOLANA_ERROR__CODECS__INVALID_PATTERN_MATCH_VALUE error if the value does not match any of the specified patterns.

Example

Encoding values using pattern matching.

const encoder = getPatternMatchEncoder([
  [(n: number) => n < 256, getU8Encoder()],
  [(n: number) => n < 2 ** 16, getU16Encoder()],
  [(n: number) => n < 2 ** 32, getU32Encoder()]
]);
 
encoder.encode(42);
// 0x2a
//  └── Small number encoded as u8
 
encoder.encode(1000);
// 0xe803
//   └── Medium number encoded as u16
 
encoder.encode(100_000);
// 0xa0860100
//   └── Large number encoded as u32
 
ender.encode(2 ** 32 + 1);
// Throws an error because the value does not match any pattern

See

getPatternMatchCodec

Call Signature

function getPatternMatchEncoder<TFrom>(
    patterns,
): VariableSizeEncoder<TFrom>;

Returns an encoder that selects which variant encoder to use based on pattern matching.

This encoder evaluates the value against a series of predicate functions in order, and uses the first matching encoder to encode the value.

Type Parameters

Type ParameterDescription
TFromThe type of the value to encode.

Parameters

ParameterTypeDescription
patternsVariableSizePatternMatchEncoderEntry<TFrom, TFrom>[]An array of [predicate, encoder] pairs. Predicates are tested in order and the first matching encoder is used to encode the value. Note that predicates can be either type predicates that narrow the type of the value, or boolean predicates. If using type predicates, the encoder can be for the narrowed type.

Returns

VariableSizeEncoder<TFrom>

An encoder that selects the appropriate variant based on the matched pattern.

Throws

Throws a SOLANA_ERROR__CODECS__INVALID_PATTERN_MATCH_VALUE error if the value does not match any of the specified patterns.

Example

Encoding values using pattern matching.

const encoder = getPatternMatchEncoder([
  [(n: number) => n < 256, getU8Encoder()],
  [(n: number) => n < 2 ** 16, getU16Encoder()],
  [(n: number) => n < 2 ** 32, getU32Encoder()]
]);
 
encoder.encode(42);
// 0x2a
//  └── Small number encoded as u8
 
encoder.encode(1000);
// 0xe803
//   └── Medium number encoded as u16
 
encoder.encode(100_000);
// 0xa0860100
//   └── Large number encoded as u32
 
ender.encode(2 ** 32 + 1);
// Throws an error because the value does not match any pattern

See

getPatternMatchCodec

Call Signature

function getPatternMatchEncoder<TFrom>(patterns): Encoder<TFrom>;

Returns an encoder that selects which variant encoder to use based on pattern matching.

This encoder evaluates the value against a series of predicate functions in order, and uses the first matching encoder to encode the value.

Type Parameters

Type ParameterDescription
TFromThe type of the value to encode.

Parameters

ParameterTypeDescription
patternsPatternMatchEncoderEntry<TFrom, TFrom>[]An array of [predicate, encoder] pairs. Predicates are tested in order and the first matching encoder is used to encode the value. Note that predicates can be either type predicates that narrow the type of the value, or boolean predicates. If using type predicates, the encoder can be for the narrowed type.

Returns

Encoder<TFrom>

An encoder that selects the appropriate variant based on the matched pattern.

Throws

Throws a SOLANA_ERROR__CODECS__INVALID_PATTERN_MATCH_VALUE error if the value does not match any of the specified patterns.

Example

Encoding values using pattern matching.

const encoder = getPatternMatchEncoder([
  [(n: number) => n < 256, getU8Encoder()],
  [(n: number) => n < 2 ** 16, getU16Encoder()],
  [(n: number) => n < 2 ** 32, getU32Encoder()]
]);
 
encoder.encode(42);
// 0x2a
//  └── Small number encoded as u8
 
encoder.encode(1000);
// 0xe803
//   └── Medium number encoded as u16
 
encoder.encode(100_000);
// 0xa0860100
//   └── Large number encoded as u32
 
ender.encode(2 ** 32 + 1);
// Throws an error because the value does not match any pattern

See

getPatternMatchCodec

On this page