getPredicateEncoder
Returns an encoder that selects between two encoders based on a predicate.
This encoder uses a boolean predicate function to determine which of two
encoders to use for a given value. If the predicate returns true, the
ifTrue encoder is used; otherwise, the ifFalse encoder is used.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
TFrom | any | The type of the value to encode. |
TIfTrue extends Encoder<TFrom> | Encoder<TFrom> | - |
TIfFalse extends Encoder<TFrom> | Encoder<TFrom> | - |
Parameters
| Parameter | Type | Description |
|---|---|---|
predicate | (value) => boolean | A function that returns true or false for a given value. |
ifTrue | TIfTrue | The encoder to use when the predicate returns true. |
ifFalse | SameType<TIfTrue extends Encoder<TFrom> ? TFrom : never, TIfFalse extends Encoder<TFrom> ? TFrom : never> & TIfFalse | The encoder to use when the predicate returns false. |
Returns
GetUnionEncoderType<readonly [TIfTrue, TIfFalse]>
An Encoder based on the provided encoders.
Example
Encoding small and large numbers differently.