detect indirect loops in tree

This commit is contained in:
jomo 2022-08-05 02:08:51 +02:00
parent 0961d0e47d
commit daf41ab17e

View file

@ -39,18 +39,27 @@ function renderItems() {
}
function getLocString(items, item) {
if (item.is_in == item.id) {
return ['in ↻', `${item.id} is in itself`];
}
let ancestors = [];
let next = item;
let loop = false;
while (next) {
if (ancestors.includes(next)) {
loop = true;
if (ancestors.length == 1) {
ancestors.push(null);
}
break;
}
ancestors.unshift(next);
next = items[next.is_in];
}
ancestors.pop();
const loc = ancestors.map(i => i.id).join(' ➜ ') || '⬚';
let loc = ancestors.map(i => i.id).join(' ➜ ') || '⬚';
let longloc = ancestors.map(i => `${i.type || ''} ${i.id} ${i.name && `(${i.name})` || ''}`).join(' ➜ ') || 'universe';
if (loop) {
loc += ' ↻';
longloc += ' ↻';
}
return [loc, `${item.id} is in ${longloc}`];
}