Open ships opened 1 year ago
One function you might add, adapted from render-rev's stepsGenerator
Sorry it's a bit messy, shouldn't be too hard to make prettier.
function getStepsInOrder(docmap: DocmapT): StepT[] {
const visitedSteps: Set<string> = new Set(); // we keep track of visited steps for loop detection
let idNextStep: string | null | undefined = docmap["first-step"];
const stepsById = docmap.steps;
const orderedSteps: StepT[] = [];
if (!idNextStep || !stepsById) {
return [];
}
while (idNextStep && idNextStep in stepsById) {
if (visitedSteps.has(idNextStep)) {
console.log("loop detected, aborting step iterator at %s", idNextStep);
break;
}
visitedSteps.add(idNextStep);
const nextStep: StepT = stepsById[idNextStep];
orderedSteps.push(nextStep);
idNextStep = nextStep["next-step"];
}
return orderedSteps;
}
@source-data/render-rev has some general docmaps-management logic that would probably be of general use to Docmaps consumers. A good example is this function that converts a step map and first-step-id from a DocMap to an iterable/array of steps in order indexed by number.
cc @eidens, any other functions that are coming to mind?