All files / src/models/Data PrjBaseData.ts

0% Statements 0/21
0% Branches 0/14
0% Functions 0/6
0% Lines 0/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                                                                                                                                                                             
/* eslint-disable @typescript-eslint/no-explicit-any */
import { FieldConfigSymbol } from 'src/classes/decorators/FieldConfigDecorator';
import { ToStringFieldSymbol } from 'src/classes/decorators/ToStringFieldDecorator';
import { YamlKeyMap } from 'src/types/YamlKeyMap';
 
/**
 * An abstract base class that provides common functionality for data classes.
 */
export default abstract class PrjBaseData<T> {
    /**
     * Initializes a new instance of the BaseData class.
     * @param data The optional data to merge into the current instance.
     */
    constructor(data?: Partial<T>) {
        this.mergeData(data);
    }
 
    /**
     * The mapping of YAML keys to the corresponding properties.
     * @remarks This property should be overridden in
     * derived classes to provide the mapping if necessary.
     */
    public static yamlKeyMap: YamlKeyMap | undefined;
 
    /**
     * The field configuration for the data.
     * Only fields marked with the {@link FieldConfigSymbol|@fieldConfig} decorator will be included in the output.
     * @remarks Will be used to determine which fields to merge and their default values when calling {@link mergeData}.
     */
    protected get fieldConfig(): {
        key: keyof T | string | number | symbol;
        defaultValue?: any;
    }[] {
        return (this.constructor as any)[FieldConfigSymbol] || [];
    }
 
    /**
     * Merges the provided data into the current instance.
     * @param data The optional data to merge into the current instance.
     * @remarks - This method uses the {@link fieldConfig} property to determine which fields to merge and their default values.
     * - If a field is not present in the provided data or no data is provided, the default value is used.
     */
    protected mergeData(data?: Partial<T>): void {
        for (const config of this.fieldConfig) {
            const key = config.key as keyof T;
 
            if (data?.[key] !== undefined) {
                (this as unknown as T)[key] = data[key] as T[keyof T];
            } else Iif (config.defaultValue !== undefined) {
                (this as unknown as T)[key] = config.defaultValue;
            }
        }
    }
 
    /**
     * Gets the default data for the current data model `T`.
     */
    public get defaultData(): Partial<T> {
        const defaultData: Partial<T> = {};
 
        for (const config of this.fieldConfig) {
            const key = config.key as keyof T;
 
            Iif (config.defaultValue !== undefined) {
                defaultData[key] = config.defaultValue;
            }
        }
 
        return defaultData;
    }
 
    /**
     * Returns the metadata of the document as a string.
     * Only properties marked with the {@link ToStringFieldSymbol|@toStringField} decorator will be included in the output.
     * @returns A string containing the concatenated values of the marked properties.
     */
    public toString(): string {
        const fields = (this.constructor as any)[ToStringFieldSymbol] as (
            | string
            | symbol
        )[];
        const dataFields = fields.map((field) => (this as any)[field] ?? '');
 
        return dataFields.join(' ');
    }
}
 
Zur TypeDoc-Dokumentation