@giancosta86/omnicourse-core
Version:
Core model for OmniCourse
71 lines • 2.73 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.TaxonomyPath = void 0;
const list_1 = require("@rimbu/list");
const TaxonomyLevel_1 = require("./TaxonomyLevel");
class TaxonomyPath {
static fromTaxonomy(taxonomy) {
return new TaxonomyPath(list_1.List.from([taxonomy]));
}
constructor(levels) {
this.levels = levels;
this.currentLevel = this.levels.last();
this.previousLevels = this.levels.take(this.levels.length - 1);
this.topSubjectsInPreviousLevels = this.previousLevels
.stream()
.fold(0, (cumulatedCounter, level) => cumulatedCounter + level.items.size);
}
push(subject) {
if (!this.currentLevel.items.has(subject)) {
throw new Error(`Cannot push subject '${subject.name}' not belonging to the current level of the path`);
}
return new TaxonomyPath(this.levels.append(subject));
}
revertTo(levelInPath) {
const requestedLevelIndex = this.findLevelIndex(levelInPath);
const levelsUpToAndIncludingRequestedOne = this.levels
.slice({
start: 0,
amount: requestedLevelIndex + 1
})
.assumeNonEmpty();
return new TaxonomyPath(levelsUpToAndIncludingRequestedOne);
}
findLevelIndex(level) {
const levelIndex = this.levels.stream().indexOf(level);
if (levelIndex === undefined) {
throw new Error(`Cannot find level '${level.name}' not belonging to the path`);
}
return levelIndex;
}
toMeaningful() {
if (TaxonomyLevel_1.TaxonomyLevel.isMeaningful(this.currentLevel)) {
return this;
}
return this.push(this.currentLevel.items.stream().first()).toMeaningful();
}
navigateTaxonomy(taxonomy) {
const initialMatchingLevels = list_1.List.from([taxonomy]);
const matchingLevels = this.levels
.stream()
.drop(1)
.fold(initialMatchingLevels, (matchingLevels, oldLevel, _index, halt) => {
const levelToExplore = matchingLevels.last();
if (!levelToExplore.containsSubjects) {
halt();
return matchingLevels;
}
const matchingSubject = levelToExplore.items
.stream()
.find(subject => subject.name == oldLevel.name);
if (!matchingSubject) {
halt();
return matchingLevels;
}
return matchingLevels.append(matchingSubject);
});
return new TaxonomyPath(matchingLevels);
}
}
exports.TaxonomyPath = TaxonomyPath;
//# sourceMappingURL=TaxonomyPath.js.map
;