trm-core
Version:
TRM (Transport Request Manager) Core
33 lines (32 loc) • 933 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPackageHierarchy = getPackageHierarchy;
function getPackageHierarchy(input, ignoreMultiples) {
const map = new Map();
input.forEach(item => {
map.set(item.devclass, {
devclass: item.devclass,
sub: []
});
});
const roots = [];
input.forEach(item => {
const child = map.get(item.devclass);
const parent = map.get(item.parentcl);
if (child && parent) {
parent.sub.push(child);
}
else if (child && !parent) {
roots.push(child);
}
});
if (roots.length === 0) {
throw new Error(`No root found in package hierarchy.`);
}
else if (roots.length > 1) {
if (!ignoreMultiples) {
throw new Error(`Multiple roots found in package hierarchy.`);
}
}
return roots[0];
}