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