detect indirect loops in tree
This commit is contained in:
parent
0961d0e47d
commit
daf41ab17e
1 changed files with 13 additions and 4 deletions
|
@ -39,18 +39,27 @@ function renderItems() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLocString(items, item) {
|
function getLocString(items, item) {
|
||||||
if (item.is_in == item.id) {
|
|
||||||
return ['in ↻', `${item.id} is in itself`];
|
|
||||||
}
|
|
||||||
let ancestors = [];
|
let ancestors = [];
|
||||||
let next = item;
|
let next = item;
|
||||||
|
let loop = false;
|
||||||
while (next) {
|
while (next) {
|
||||||
|
if (ancestors.includes(next)) {
|
||||||
|
loop = true;
|
||||||
|
if (ancestors.length == 1) {
|
||||||
|
ancestors.push(null);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
ancestors.unshift(next);
|
ancestors.unshift(next);
|
||||||
next = items[next.is_in];
|
next = items[next.is_in];
|
||||||
}
|
}
|
||||||
ancestors.pop();
|
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';
|
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}`];
|
return [loc, `${item.id} is in ${longloc}`];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue