All files / api/trimet-client/src nearbyGrouping.ts

100% Statements 14/14
100% Branches 0/0
100% Functions 11/11
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48                          30x 29x   30x   29x     30x           30x 28x 28x 28x       28x                 68x 69x      
import { flatten, groupBy, map, mapKeys, mapValues } from "lodash";
import type {
  Direction,
  StopData,
  StopLocation,
  TrimetRoute
} from "./interfaces/types";
 
type RoutesById = Record<string, TrimetRoute[]>;
export type NearbyRoutesDictionary = Record<number, { directions: number[] }>;
export type StopLocationsDictionary = Record<number, StopLocation>;
 
export function processRoutes(stopData: StopData): RoutesById {
  const routes = stopData.location.map((location: StopLocation) => {
    return location.route.map((route: TrimetRoute) => route);
  });
  const mappedKeys = mapKeys(
    flatten(routes),
    (item: TrimetRoute) => `${item.route}-${item.dir[0].dir}`
  );
 
  return groupBy(mappedKeys, (key: TrimetRoute) => key.route);
}
 
export function getNearbyRouteIds(
  nearbyRoutes: RoutesById
): NearbyRoutesDictionary {
  return mapValues(nearbyRoutes, (routes: TrimetRoute[]) => {
    const directions = map(routes, (route: TrimetRoute) => {
      return map(route.dir, (dir: Direction) => {
        return dir.dir;
      });
    });
 
    return {
      directions: flatten(directions)
    };
  });
}
 
export function getStopLocations(
  nearbyStops: StopData
): StopLocationsDictionary {
  return mapKeys(nearbyStops.location, (location: StopLocation) => {
    return location.locid;
  });
}