All files / src/custom-element/helpers findChild.ts

9.09% Statements 1/11
0% Branches 0/6
0% Functions 0/1
10% Lines 1/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 24 25 26 27 28 29 30            1x                                              
/**
 * Returns the first child whose evaluation by predicate returns true
 * @param children 
 * @param predicate 
 * @returns The first HTMLElement for which the predicate function returns true
 */
export default function findChild(children: HTMLCollection, predicate: Function) : Element {
 
    if (children.length == 0) {
 
        return null;
    }
 
    for (let i = 0; i < children.length; ++i) {
 
        let child = children[i];
 
        if (predicate(child) === true) {
 
            return child;
        }
 
        child = findChild(child.children, predicate)
 
        if (child !== null) {
 
            return child;
        }      
    }
}