All files / src/classes Path.ts

100% Statements 8/8
100% Branches 1/1
100% Functions 4/4
100% Lines 8/8

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      1x                 19x     46x     46x 28x     46x   46x                                 6x      
/**
 * A class that provides utility functions for working with paths and filenames.
 */
export class Path {
    /**
     * Joins all given path segments into a single path.
     * Replaces all backslashes with slashes, removes double slashes and leading/trailing slashes.
     * @param paths - An array of path segments
     * @returns The combined path
     * @remarks This function is used because Node.js's 'path' module is not available in Obsidian Mobile.
     */
    public static join(...paths: string[]): string {
        return paths
            .map((part, index) => {
                // Replaces all backslashes with slashes
                let normalizedPart = part.replace(/\\/g, '/');
 
                // Removes leading slashes from all parts except the first
                if (index > 0) {
                    normalizedPart = normalizedPart.replace(/^\//, '');
                }
 
                return normalizedPart;
            })
            .filter((part) => part.length > 0) // Removes empty parts
            .join('/') // Joins the parts using a slash as a separator
            .replace(/\/{2,}/g, '/'); // Removes double slashes
    }
 
    /**
     * Sanitizes a filename by removing any characters that are not
     * - alphanumeric,
     * - hyphen,
     * - underscore,
     * - period,
     * - space
     * - or umlauts.
     * @param filename - The filename to sanitize.
     * @returns The sanitized filename.
     */
    public static sanitizeFilename(filename: string): string {
        return filename.replace(/[^a-zA-Z0-9-_. äöüÄÖÜߧ]/g, '');
    }
}
 
Zur TypeDoc-Dokumentation