Skip to content

Instantly share code, notes, and snippets.

@avisek
Created October 7, 2018 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avisek/60f37de3115f0ea5bed60007386cd135 to your computer and use it in GitHub Desktop.
Save avisek/60f37de3115f0ea5bed60007386cd135 to your computer and use it in GitHub Desktop.
/**
* 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