All files / app/codeCharta/util fileNameHelper.ts

94.44% Statements 17/18
100% Branches 4/4
100% Functions 5/5
94.44% Lines 17/18

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 4199x   99x   99x 99x     4x       6x 6x       7x       4x 4x 4x 2x   2x 1x   1x 1x             38x      
import { LoadFileService } from "../services/loadFile/loadFile.service"
 
const dateRegex = /_\d{4}(?:-\d{1,2}){2}_\d{1,2}-\d{1,2}\./
 
export class FileNameHelper {
    private static JSON_EXTENSION = ".json"
 
    static getNewFileName(fileName: string, isDeltaState: boolean) {
        return `${this.getFileNameWithoutTimestamp(fileName, isDeltaState)}_${this.getNewTimestamp()}`
    }
 
    static getNewTimestamp() {
        const date = new Date()
        return FileNameHelper.getFormattedTimestamp(date)
    }
 
    static getFormattedTimestamp(date: Date) {
        return date.toISOString().slice(0, 16).replace("T", "_").replace(":", "-")
    }
 
    private static getFileNameWithoutTimestamp(fileName: string, isDeltaState: boolean) {
        if (!isDeltaState) {
            const dateMatch = dateRegex.exec(fileName)
            if (dateMatch) {
                return fileName.slice(0, dateMatch.index)
            }
            if (fileName.endsWith(LoadFileService.CC_FILE_EXTENSION)) {
                return fileName.slice(0, -LoadFileService.CC_FILE_EXTENSION.length)
            }
            if (fileName.endsWith(FileNameHelper.JSON_EXTENSION)) {
                return fileName.slice(0, -FileNameHelper.JSON_EXTENSION.length)
            }
        }
        return fileName
    }
 
    static withoutCCExtension(fileName: string) {
        return fileName.replace(/(\.cc)?(\.json)?(\.gz)?$/, "")
    }
}