All files / src/libs/FileType FileType.ts

86.36% Statements 19/22
76.92% Branches 20/26
75% Functions 9/12
85.71% Lines 18/21

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 1392x   2x             2x       2x                                         19x             32x                 12x                 103x         44x 59x 43x                               47x 7x       40x                 20x                             7x             9x               2x                        
import { ImplementsStatic } from 'src/classes/decorators/ImplementsStatic';
import { IFileType, FileTypes, IFileType_ } from './interfaces/IFileType';
import { Register } from '../DependencyInjection/decorators/Register';
 
/**
 * Represents the types used in the app for files.
 */
@ImplementsStatic<IFileType_>()
@Register('IFileType_')
export class FileType implements IFileType {
    /**
     * An array of valid file types.
     */
    private static readonly _fileTypes: FileTypes[] = [
        'Topic',
        'Project',
        'Task',
        'Metadata',
        'Note',
    ];
 
    /**
     * Gets the array of valid file types.
     */
    public static get types(): FileTypes[] {
        return FileType._fileTypes;
    }
 
    private _value: FileTypes | undefined;
 
    /**
     * Gets the file type.
     */
    public get value(): FileTypes | undefined {
        return this._value;
    }
 
    /**
     * Sets the file type.
     */
    public set value(value: unknown) {
        this._value = FileType.validate(value);
    }
 
    /**
     * Checks if the value is a valid file type.
     * @param value The value to check.
     * @returns Whether the value is a valid file type.
     */
    public static isValid(value: unknown): boolean {
        return this.validate(value) !== undefined;
    }
 
    /**
     * Validates the file type.
     * @param value The value to validate.
     * @returns The valid file type or undefined if the value is not valid.
     */
    public static validate(value: unknown): FileTypes | undefined {
        if (
            value === null ||
            value === undefined ||
            typeof value !== 'string'
        ) {
            return undefined;
        } else if (FileType._fileTypes.includes(value as FileTypes)) {
            return FileType._fileTypes.includes(value as FileTypes)
                ? (value as FileTypes)
                : undefined;
        }
    }
 
    /**
     * Validates the file type.
     * @param value The value to validate.
     * @param types The valid file types.
     * @returns True if the value is valid and of the correct type; otherwise, false.
     */
    public static isValidOf(
        value: unknown,
        types: FileTypes | FileTypes[],
    ): boolean {
        if (Array.isArray(types)) {
            return types.includes(
                FileType.validate(value) ?? ('' as FileTypes),
            );
        } else {
            return FileType.validate(value) === types;
        }
    }
 
    /**
     * Initializes a new instance of the FileType class.
     * @param value The value of the file type.
     */
    constructor(value: unknown) {
        this.value = value;
    }
 
    /**
     * Gets a frontmatter compatible string.
     * @returns The File Type as string.
     */
    getFrontmatterObject(): string {
        return this._value ?? '';
    }
    /**
     * Gets the File Type as a string.
     * @returns The File Type as string.
     */
    primitiveOf(): string {
        return this._value ?? '';
    }
    /**
     * Gets the File Type as a string.
     * @returns The File Type as string.
     */
    toString(): string {
        return this._value ?? '';
    }
    /**
     * Checks if the File Type is equal to another File Type.
     * @param other The other File Type to compare.
     * @returns Whether the File Type is equal to the other File Type.
     */
    equals(other: IFileType | FileTypes): boolean {
        return this._value === other.toString();
    }
 
    /**
     * 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 FileType;
    }
}
 
Zur TypeDoc-Dokumentation