All files / app/codeCharta/util fileHelper.ts

100% Statements 34/34
100% Branches 34/34
100% Functions 6/6
100% Lines 34/34

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 96146x   146x 146x 146x   146x 46x 46x                                             46x 44x           2x             46x       46x 2x 2x     46x     146x 824x 824x 12x 11x       824x     146x 15x   15x   15x       14x 8x 8x   8x     6x 4x 4x   6x         1x    
import { ExportBlacklistType, ExportCCFile, ExportWrappedCCFile, OldAttributeTypes } from "../codeCharta.api.model"
import { AttributeDescriptors, AttributeTypes, BlacklistItem, CCFile, NameDataPair } from "../codeCharta.model"
import { FileSelectionState, FileState } from "../model/files/files"
import md5 from "md5"
import { clone } from "./clone"
 
export function getCCFile(file: NameDataPair): CCFile {
    const fileContent = file.content
    return {
        fileMeta: {
            fileName: file.fileName,
            fileChecksum: fileContent.fileChecksum,
            projectName: fileContent.projectName,
            apiVersion: fileContent.apiVersion,
            exportedFileSize: file.fileSize,
            repoCreationDate: fileContent.repoCreationDate || ""
        },
        settings: {
            fileSettings: {
                edges: fileContent.edges || [],
                attributeTypes: getAttributeTypes(fileContent.attributeTypes),
                attributeDescriptors: getAttributeDescriptors(fileContent.attributeDescriptors),
                blacklist: potentiallyUpdateBlacklistTypes(fileContent.blacklist || []),
                markedPackages: fileContent.markedPackages || []
            }
        },
        map: clone(fileContent.nodes[0])
    }
}
 
function getAttributeTypes(attributeTypes: AttributeTypes | OldAttributeTypes): AttributeTypes {
    if (!attributeTypes || Array.isArray(attributeTypes.nodes) || Array.isArray(attributeTypes.edges)) {
        return {
            nodes: {},
            edges: {}
        }
    }
 
    return {
        nodes: attributeTypes.nodes ?? {},
        edges: attributeTypes.edges ?? {}
    }
}
 
function getAttributeDescriptors(attributeDescriptors: AttributeDescriptors): AttributeDescriptors {
    return attributeDescriptors || {}
}
 
function potentiallyUpdateBlacklistTypes(blacklist): BlacklistItem[] {
    for (const entry of blacklist) {
        if (entry.type === ExportBlacklistType.hide) {
            entry.type = "flatten"
        }
    }
    return blacklist
}
 
export function getSelectedFilesSize(files: FileState[]) {
    let totalFileSize = 0
    for (const file of files) {
        if (file.selectedAs !== FileSelectionState.None) {
            totalFileSize += file.file.fileMeta.exportedFileSize
        }
    }
 
    return totalFileSize
}
 
export function getCCFileAndDecorateFileChecksum(jsonInput: string | ExportWrappedCCFile | ExportCCFile): ExportCCFile | null {
    let mappedFile: ExportCCFile = null
 
    try {
        const fileContent: ExportCCFile | ExportWrappedCCFile =
            typeof jsonInput === "string"
                ? (JSON.parse(jsonInput) as ExportWrappedCCFile | ExportCCFile)
                : (jsonInput as ExportWrappedCCFile | ExportCCFile)
 
        if ("data" in fileContent && "checksum" in fileContent) {
            mappedFile = fileContent.data
            mappedFile.fileChecksum = fileContent.checksum || md5(JSON.stringify(fileContent.data))
 
            return mappedFile
        }
 
        if (!fileContent.fileChecksum) {
            const jsonInputString = typeof jsonInput === "string" ? jsonInput : JSON.stringify(jsonInput)
            fileContent.fileChecksum = md5(jsonInputString)
        }
        return fileContent
    } catch {
        // Explicitly ignored
    }
 
    return mappedFile
}