• @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.

    Type Parameters

    • This

      The type of the class the decorator is applied to.

    • Value

      The type of the decorated property.

    Parameters

    • transformFunction: TransformerFunction<This>

      A function that based on the current value of the decorated property and instance, returns a transformed value.

    • Optionalopt: Partial<TransformerOptions>

      Optional configuration.

    Returns DecoratorType<This, Value>

    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;
    }

    By default, the @Transform decorator applies the transformation only during the reading phase. To enable the reverse transformation during the writing phase, define a separate transformer with the appropriate execution scope.