All files / app/codeCharta/util codeMapHelper.ts

89.33% Statements 67/75
67.44% Branches 29/43
66.66% Functions 10/15
91.78% Lines 67/73

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 132143x   143x   143x   143x         143x         143x 147x   147x 91x 56x 56x     91x     143x 3x 3x 2x 14x 9x       3x     143x 10x 10x     143x 21x   21x 21x 2x 2x   21x   21x 23x 23x 20x   23x     23x   21x     143x 3x     143x 92x 73x   19x 19x 29x 18x     19x     143x 394x   394x 2x       1x       394x 1x         143x         143x 2613x     143x 143x 143x 143x     143x 823x 823x   823x   2x   2x   819x      
import { hierarchy } from "d3-hierarchy"
import { BlacklistItem, BlacklistType, CodeMapNode, MarkedPackage } from "../codeCharta.model"
import ignore from "ignore"
import { FileState } from "../model/files/files"
import { getSelectedFilesSize } from "./fileHelper"
 
export function getAnyCodeMapNodeFromPath(path: string, root: CodeMapNode) {
    const matchingNode = hierarchy(root).find(({ data }) => data.path === path)
    return matchingNode?.data
}
 
export function getCodeMapNodeFromPath(path: string, nodeType: string, root: CodeMapNode) {
    const matchingNode = hierarchy(root).find(({ data }) => data.path === path && data.type === nodeType)
    return matchingNode?.data
}
 
export function transformPath(toTransform: string) {
    let removeNumberOfCharactersFromStart = 2
 
    if (toTransform.startsWith("/")) {
        removeNumberOfCharactersFromStart = 1
    } else if (!toTransform.startsWith("./")) {
        return toTransform
    }
 
    return toTransform.slice(removeNumberOfCharactersFromStart)
}
 
export function getAllNodes(root: CodeMapNode): CodeMapNode[] {
    const filtered = []
    if (root !== undefined) {
        for (const { data } of hierarchy(root)) {
            if (data.type !== "Folder") {
                filtered.push(data)
            }
        }
    }
    return filtered
}
 
export function isNodeExcludedOrFlattened(node: CodeMapNode, gitignorePath: string): boolean {
    const ignoreResults = returnIgnore(gitignorePath)
    return ignoreResults.ignoredNodePaths.ignores(transformPath(node.path)) === ignoreResults.condition
}
 
export function returnIgnore(gitignorePath: string) {
    gitignorePath = transformPath(gitignorePath.trimStart())
 
    let condition = true
    if (gitignorePath.startsWith("!")) {
        gitignorePath = gitignorePath.slice(1)
        condition = false
    }
    const ignoredNodePaths = ignore()
 
    for (let path of gitignorePath.split(",")) {
        path = path.trimStart()
        if (!path.startsWith("*") && !path.endsWith("*")) {
            path = path.startsWith('"') && path.endsWith('"') ? path.slice(1, -1) : `*${path}*`
        }
        Iif (path.length === 0) {
            continue
        }
        ignoredNodePaths.add(transformPath(path))
    }
    return { ignoredNodePaths, condition }
}
 
export function isPathHiddenOrExcluded(path: string, blacklist: Array<BlacklistItem>) {
    return isPathBlacklisted(path, blacklist, "exclude") || isPathBlacklisted(path, blacklist, "flatten")
}
 
export function isPathBlacklisted(path: string, blacklist: Array<BlacklistItem>, type: BlacklistType) {
    if (blacklist.length === 0) {
        return false
    }
    const ig = ignore()
    for (const entry of blacklist) {
        if (entry.type === type) {
            ig.add(transformPath(entry.path))
        }
    }
    return ig.ignores(transformPath(path))
}
 
export function getMarkingColor(node: CodeMapNode, markedPackages: MarkedPackage[]): string | void {
    if (markedPackages) {
        let longestPathParentPackage: MarkedPackage
        for (const markedPackage of markedPackages) {
            if (
                (!longestPathParentPackage || longestPathParentPackage.path.length < markedPackage.path.length) &&
                node.path.startsWith(markedPackage.path)
            ) {
                longestPathParentPackage = markedPackage
            }
        }
 
        if (longestPathParentPackage) {
            return longestPathParentPackage.color
        }
    }
}
 
export function isBlacklisted(node: CodeMapNode) {
    return node.isExcluded || node.isFlattened
}
 
export type MaybeLeaf = { children?: unknown[] }
export function isLeaf(node: MaybeLeaf) {
    return node.children === undefined || node.children.length === 0
}
 
export enum MAP_RESOLUTION_SCALE {
    SMALL_MAP = 1,
    MEDIUM_MAP = 0.5,
    BIG_MAP = 0.25
}
 
export function getMapResolutionScaleFactor(files: FileState[]) {
    const oneMB = 1024 * 1024
    const totalFilesSizeKB = getSelectedFilesSize(files)
 
    switch (true) {
        case totalFilesSizeKB >= 7 * oneMB:
            return MAP_RESOLUTION_SCALE.BIG_MAP
        case totalFilesSizeKB >= 2 * oneMB:
            return MAP_RESOLUTION_SCALE.MEDIUM_MAP
        default:
            return MAP_RESOLUTION_SCALE.SMALL_MAP
    }
}