findNodeUpToRoot.mjs 527 B

12345678910111213141516171819202122232425
  1. import eachNodeUpToRoot, { STOP } from './eachNodeUpToRoot.mjs';
  2. /** @import { Node } from 'postcss' */
  3. /**
  4. * Finds the node satisfying the specified predicate up to the root node.
  5. *
  6. * @param {Node} node
  7. * @param {(node: Node) => boolean} predicate
  8. * @returns {Node | undefined}
  9. */
  10. export default function findNodeUpToRoot(node, predicate) {
  11. /** @type {Node | undefined} */
  12. let found;
  13. eachNodeUpToRoot(node, (current) => {
  14. if (predicate(current)) {
  15. found = current;
  16. return STOP;
  17. }
  18. });
  19. return found;
  20. }