0

my algorithm computes a path through the DOM. It starts with the given component and goes up the tree. Each time a parent component has a specific attribute, the algorithm adds its value to the path.

_computePath(aComponent) {
  let result = '';
  let parent = aComponent.parentNode;

  while (parent.tagName !== 'MY-ROOT-COMPONENT') {
    if (parent.hasAttribute('my-attribute')) {
      result = `/${parent.getAttribute('my-attribute')}${result}`;
    }
    parent = parent.parentNode;
  }

  return result;
}

In an other part of my application, i need a slightly different version.

_computePath(aComponent) {
  let result = '';
  let parent = aComponent.parentNode;

  while (parent.tagName !== 'MY-ROOT-COMPONENT') {
    if (parent.tagName === 'SPECIAL-COMPONENT') {
      result = null;
      break;
    }

    if (parent.condition === 'special') {
      result = null;
      break;
    }

    if (parent.hasAttribute('my-attribute')) {
      result = `/${parent.getAttribute('my-attribute')}${result}`;
    }
    parent = parent.parentNode;
  }

  return result;
}

How can i extend the algorithm of the first loop, without repeat the code?

There is probably a very simple solution, but somehow I can't figure it out.

Thank you

1 Answer 1

1

Have the function take a callback that tests for the conditions that cause it to break.

_computePath(aComponent, stopfun = parent => false) {
  let result = '';
  let parent = aComponent.parentNode;

  while (parent.tagName !== 'MY-ROOT-COMPONENT') {
    if (stopfun(parent)) {
      result = null;
      break;
    }

    if (parent.hasAttribute('my-attribute')) {
      result = `/${parent.getAttribute('my-attribute')}${result}`;
    }
    parent = parent.parentNode;
  }

  return result;
}

let result1 = obj1._computePath(component1); // no extra stop check
let result2 = obj2._computePath(component2, parent => parent.tagName === 'SPECIAL-COMPONENT' || parent.condition === 'special');

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. This was also my first thought. But i'm not sure if its too "dangerous" to pass a reference variable that is modified by a loop to a function?
The variable isn't modified by the function. That's the default value of the parameter, it doesn't modify the caller's variable.
JavaScript doesn't pass variables by reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.