Created
October 7, 2018 20:10
-
-
Save avisek/60f37de3115f0ea5bed60007386cd135 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Return the first ancestor for which the {@code predicate} returns true. | |
* @param {Node} node The node to check. | |
* @param {function(Node):boolean} predicate The function that tests the | |
* nodes. | |
* @return {Node} The found ancestor or null if not found. | |
*/ | |
function findAncestor(node, predicate) { | |
var last = false; | |
while (node != null && !(last = predicate(node))) { | |
node = node.parentNode; | |
} | |
return last ? node : null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment