A function that based on the current value of the decorated property and instance, returns a transformed value.
Optional
opt: Partial<TransformerOptions>Optional configuration.
The property decorator function.
The following example demonstrates how to use the @Transform
decorator to
apply custom logic during parsing. In this case, it decodes an array
into a UTF-8 string.
class Protocol {
@Transform((value: number[]) => {
const buf = new Uint8Array(value)
return new TextDecoder().decode(buf)
})
@Until(EOF)
@Relation(PrimitiveSymbol.u8)
decodedString: string;
}
To add support for writing, define a complementary transformer for the writing phase:
class Protocol {
@Transform((value: number[]) => {
const buf = new Uint8Array(value)
return new TextDecoder().decode(buf)
}, { scope: ExecutionScope.OnRead })
@Transform((value: string) => {
const buf = new TextEncoder().encode(value)
return Array.from(buf)
}, { scope: ExecutionScope.OnWrite })
@Until(EOF)
@Relation(PrimitiveSymbol.u8)
decodedString: string;
}
@Transform
decorator applies a custom transformation function to the decorated property value immediately after it is read or written during binary processing.The decorator enables dynamic transformations, such as decoding, scaling, or reformatting values, providing flexibility in how data is interpreted and manipulated during parsing.