splitFullTrackIntoTrackSections.ts

 avatar
unknown
plain_text
a year ago
616 B
6
Indexable
export const splitFullTrackIntoTrackSections = (
  tracks: any[],
  stops: string[],
) => {
  const sectionGroups = [];
  let nextStopIdx = 1;
  let currentSection: any[] = [];

  tracks.forEach(point => {
    const {station_name} = point;
    if (station_name !== stops[nextStopIdx]) {
      currentSection.push(point);
    } else {
      const lastPoint = {...point, station_2: point.station_1};
      sectionGroups.push([...currentSection, lastPoint]);
      currentSection = [point];
      nextStopIdx++;
    }
  });

  sectionGroups.push(currentSection);
  sectionGroups.splice(-1);

  return sectionGroups;
};
Editor is loading...