All files / realm/blong-commander/branch getPath.ts

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

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 231x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 60x 58x 60x 87x 85x 85x 56x 56x  
/**
 * `getPath` — read a dot path off a row.
 *
 * A commander level names the value it displays as a dot path (`metadata.name`),
 * and the same path has to be read from two shapes: an already-flattened row, where
 * the path is a literal key, and the raw object a backend answered with. Both are
 * served by traversing the path segments, because a literal key is a one-segment
 * traversal of a flat object.
 *
 * Shared by the ordering (`sortRows`) and the whitelist/blacklist
 * (`levelRowsSelect`), because both ask the same question — *what does this level
 * display for this row* — and two readers would eventually disagree about it.
 */
export function getPath(obj: Record<string, unknown> | undefined, path: string): unknown {
    if (!obj) return undefined;
    let cur: unknown = obj;
    for (const part of path.split('.')) {
        if (cur === null || cur === undefined) return undefined;
        cur = (cur as Record<string, unknown>)[part];
    }
    return cur;
}