All files / app/codeCharta/state/store/fileSettings/markedPackages/util findIndexOfMarkedPackageOrParent.ts

100% Statements 13/13
100% Branches 5/5
100% Functions 2/2
100% Lines 10/10

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      49x 9x 6x 3x     3x 3x 5x 5x       2x       3x    
import { MarkedPackage } from "../../../../../codeCharta.model"
 
/** @return index of exact match if there is an exact match. Otherwise return index of parent or -1  */
export const findIndexOfMarkedPackageOrParent = (markedPackages: MarkedPackage[], nodePath: string) => {
    const indexOfNodePath = markedPackages.findIndex(mp => mp.path === nodePath)
    if (indexOfNodePath !== -1) {
        return indexOfNodePath
    }
 
    let indexOfNearestParent = -1
    for (let loopIndex = 0; loopIndex < markedPackages.length; loopIndex++) {
        const markedPackage = markedPackages[loopIndex]
        if (
            nodePath.startsWith(markedPackage.path) &&
            (indexOfNearestParent === -1 || markedPackages[indexOfNearestParent].path.length < markedPackage.path.length)
        ) {
            indexOfNearestParent = loopIndex
        }
    }
 
    return indexOfNearestParent
}