All files / src/libs/StatusType StatusType.ts

95.55% Statements 43/45
86.84% Branches 33/38
93.33% Functions 14/15
95.45% Lines 42/44

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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 1931x 1x             1x 1x               1x   1x           1x 1x   1x                       1x                 3x             4x               32x                 10x                     52x   52x         2x 50x 24x     52x 28x             52x                           5x 25x       25x 50x 5x         5x             6x 5x       5x       5x 3x 2x 1x   1x     1x               3x 2x   1x               2x             8x                        
import { ImplementsStatic } from 'src/classes/decorators/ImplementsStatic';
import { isIStringConvertible } from 'src/interfaces/DataType/IStringifiable';
import type { ILogger_, ILogger } from 'src/interfaces/ILogger';
import {
    IStatusType,
    IStatusType_,
    StatusTypes,
} from './interfaces/IStatusType';
import { Inject } from '../DependencyInjection/decorators/Inject';
import { Register } from '../DependencyInjection/decorators/Register';
import type ITranslationService from '../TranslationService/interfaces/ITranslationService';
 
/**
 * Represents a status type.
 */
@Register('IStatusType_')
@ImplementsStatic<IStatusType_>()
export class StatusType implements IStatusType {
    @Inject('ITranslationService')
    private static readonly _ITranslationService: ITranslationService;
 
    /**
     * The logger to use for logging messages.
     * If not provided, no messages will be logged.
     */
    @Inject('ILogger_', (x: ILogger_) => x.getLogger('StatusType'), false)
    private static readonly _logger?: ILogger;
 
    private static readonly _statusTypes: StatusTypes[] = [
        'Active',
        'Waiting',
        'Later',
        'Someday',
        'Done',
    ];
 
    /**
     * Gets the array of valid Status types.
     */
    public static get types(): StatusTypes[] {
        return StatusType._statusTypes;
    }
 
    private _value: StatusTypes | undefined;
 
    /**
     * @inheritdoc
     */
    get value(): StatusTypes | undefined {
        return this._value;
    }
 
    /**
     * @inheritdoc
     */
    set value(value: unknown) {
        this._value = StatusType.validate(value);
    }
 
    /**
     * Initializes a new instance of a StatusType class.
     * @param value The value of the Status.
     */
    constructor(value: unknown) {
        this._value = StatusType.validate(value);
    }
 
    /**
     * Checks if the object is an {@link StatusTypes|Status Type}.
     * @param value The object to check.
     * @returns Whether the object is an {@link StatusTypes|Status Type}.
     */
    public static isValid(value: unknown): boolean {
        return this.validate(value) !== undefined;
    }
 
    /**
     * Validates the status type.
     * @param value The value to validate.
     * @returns The valid status type or undefined if the value is not valid.
     */
    public static validate(value: unknown): StatusTypes | undefined {
        let status: StatusTypes | undefined;
 
        Iif (value instanceof StatusType) {
            status = value.value;
        } else if (
            value === null ||
            value === undefined ||
            typeof value !== 'string'
        ) {
            status = undefined;
        } else if (StatusType._statusTypes.includes(value as StatusTypes)) {
            status = value as StatusTypes;
        }
 
        if (status === undefined) {
            this._logger?.warn(
                `The value is not a valid status type:`,
                value,
                `Setting the value to undefined.`,
            );
        }
 
        return status;
    }
 
    /**
     * Gets the valid status from a translation.
     * E.g. if the translation is 'Aktiv', the valid status is 'Active'.
     * @param status The translation to check.
     * @returns The valid status or undefined if the translation is not valid.
     */
    public static getValidStatusFromTranslation(
        status: string,
    ): StatusTypes | undefined {
        let validStatus: StatusTypes | undefined;
 
        this._statusTypes.forEach((statusFromValidStatuses) => {
            const translation = this._ITranslationService.getAll(
                `Status${statusFromValidStatuses}`,
            );
 
            translation.forEach((translationFromStatus) => {
                if (translationFromStatus === status) {
                    validStatus = statusFromValidStatuses;
                }
            });
        });
 
        return validStatus;
    }
 
    /**
     * @inheritdoc
     */
    compareTo(other: IStatusType): number {
        if (isIStringConvertible(other)) {
            const thisIndex = StatusType._statusTypes.indexOf(
                this._value ?? 'Done',
            );
 
            const otherIndex = StatusType._statusTypes.indexOf(
                (other.toString() ?? 'Done') as StatusTypes,
            );
 
            if (thisIndex > otherIndex) {
                return -1;
            } else if (thisIndex < otherIndex) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return -1;
        }
    }
 
    /**
     * @inheritdoc
     */
    equals(other: unknown): boolean {
        if (isIStringConvertible(other)) {
            return this._value === other.toString();
        } else {
            return this._value === other;
        }
    }
 
    /**
     * @inheritdoc
     */
    primitiveOf(): string {
        return this._value ?? '';
    }
 
    /**
     * @inheritdoc
     */
    toString(): string {
        return this._value ?? '';
    }
 
    /**
     * Checks if the object is an instance of the type.
     * @param obj The object to check.
     * @returns Whether the object is an instance of the type.
     */
    [Symbol.hasInstance](obj: unknown): boolean {
        return obj instanceof StatusType;
    }
}
 
Zur TypeDoc-Dokumentation