utils/splitFullTrackIntoTrackSections.ts

mail@pastecode.io avatar
unknown
plain_text
7 months ago
610 B
1
Indexable
Never
export const splitFullTrackIntoTrackSections = (
  tracks: any[],
  stops: string[],
) => {
  const sectionGroups = [];
  let nextStopIdx = 1;
  let currentSection: any[] = [];

  tracks.forEach(point => {
    const {station_1} = point;
    if (station_1 !== 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;
};