Docmaps-Project / docmaps

Extensible protocol for document history metadata exchange, to enable trustworthy, rapid, open science, by and for preprint science communities.
MIT License
15 stars 1 forks source link

[ts-sdk] Consider adopting generic Docmaps utilities from render-rev #64

Open ships opened 1 year ago

ships commented 1 year ago

@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?

andrewedstrom commented 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;
}