modules/search/query.js
function getTripsFromStop(db, stop, tripSearchDates, nextStops, callback) {

  // List of Trips to Return
  let rtn = [];

  // Counters
  let done = 0;
  let count = tripSearchDates.length;

  // Parse each TripSearchDate separately
  for ( let i = 0; i < tripSearchDates.length; i++ ) {
    _getTripsFromStop(db, stop, tripSearchDates[i], function(err, trips) {

      // Database Query Error
      if ( err ) {
        return callback(err);
      }

      // Check each of the Trips to add
      for ( let j = 0; j < trips.length; j++ ) {
        _addTrip(trips[j]);
      }

      // Finish the TSD
      _finish();

    });
  }


  /**
   * Add the specified Trip to the return list, if it is not
   * already present and it continues to one of the next stops
   * @param {Trip} trip Trip to add
   * @private
   */
  function _addTrip(trip) {

    // Make sure trip isn't already in list
    let found = false;
    for ( let i = 0; i < rtn.length; i++ ) {
      if ( rtn[i].id === trip.id ) {
        found = true;
      }
    }

    // Process new trip
    if ( !found ) {

      // Make sure the trip stops at any of the next stops, after the current stop
      let stopFound = false;
      let add = false;
      for ( let i = 0; i < trip.stopTimes.length; i++ ) {
        let stopId = trip.stopTimes[i].stop.id;
        if ( !stopFound && stopId === stop.id ) {
          stopFound = true;
        }
        else if ( stopFound ) {
          if ( nextStops.indexOf(stopId) > -1 ) {
            add = true;
            i = trip.stopTimes.length;
          }
        }
      }

      // Add Trip
      if ( add ) {
        rtn.push(trip);
      }

    }

  }


  /**
   * Finish processing the Trip Search Dates
   * @private
   */
  function _finish() {
    done++;
    if ( done === count ) {
      return callback(null, rtn);
    }
  }

}
function getTripsFromStop(db, stop, tripSearchDates, nextStops, callback) {

  // List of Trips to Return
  let rtn = [];

  // Counters
  let done = 0;
  let count = tripSearchDates.length;

  // Parse each TripSearchDate separately
  for ( let i = 0; i < tripSearchDates.length; i++ ) {
    _getTripsFromStop(db, stop, tripSearchDates[i], function(err, trips) {

      // Database Query Error
      if ( err ) {
        return callback(err);
      }

      // Check each of the Trips to add
      for ( let j = 0; j < trips.length; j++ ) {
        _addTrip(trips[j]);
      }

      // Finish the TSD
      _finish();

    });
  }


  /**
   * Add the specified Trip to the return list, if it is not
   * already present and it continues to one of the next stops
   * @param {Trip} trip Trip to add
   * @private
   */
  function _addTrip(trip) {

    // Make sure trip isn't already in list
    let found = false;
    for ( let i = 0; i < rtn.length; i++ ) {
      if ( rtn[i].id === trip.id ) {
        found = true;
      }
    }

    // Process new trip
    if ( !found ) {

      // Make sure the trip stops at any of the next stops, after the current stop
      let stopFound = false;
      let add = false;
      for ( let i = 0; i < trip.stopTimes.length; i++ ) {
        let stopId = trip.stopTimes[i].stop.id;
        if ( !stopFound && stopId === stop.id ) {
          stopFound = true;
        }
        else if ( stopFound ) {
          if ( nextStops.indexOf(stopId) > -1 ) {
            add = true;
            i = trip.stopTimes.length;
          }
        }
      }

      // Add Trip
      if ( add ) {
        rtn.push(trip);
      }

    }

  }


  /**
   * Finish processing the Trip Search Dates
   * @private
   */
  function _finish() {
    done++;
    if ( done === count ) {
      return callback(null, rtn);
    }
  }

}
function getTripsFromStop ( db , stop , tripSearchDates , nextStops , callback ) { // List of Trips to Return let rtn = [ ] ; // Counters let done = 0 ; let count = tripSearchDates . length ; // Parse each TripSearchDate separately for ( let i = 0 ; i < tripSearchDates . length ; i ++ ) { _getTripsFromStop ( db , stop , tripSearchDates [ i ] , function ( err , trips ) { // Database Query Error if ( err ) { return callback ( err ) ; } // Check each of the Trips to add for ( let j = 0 ; j < trips . length ; j ++ ) { _addTrip ( trips [ j ] ) ; } // Finish the TSD _finish ( ) ; } ) ; } /**
   * Add the specified Trip to the return list, if it is not
   * already present and it continues to one of the next stops
   * @param {Trip} trip Trip to add
   * @private
   */ function _addTrip ( trip ) { // Make sure trip isn't already in list let found = false ; for ( let i = 0 ; i < rtn . length ; i ++ ) { if ( rtn [ i ] . id === trip . id ) { found = true ; } } // Process new trip if ( ! found ) { // Make sure the trip stops at any of the next stops, after the current stop let stopFound = false ; let add = false ; for ( let i = 0 ; i < trip . stopTimes . length ; i ++ ) { let stopId = trip . stopTimes [ i ] . stop . id ; if ( ! stopFound && stopId === stop . id ) { stopFound = true ; } else if ( stopFound ) { if ( nextStops . indexOf ( stopId ) > - 1 ) { add = true ; i = trip . stopTimes . length ; } } } // Add Trip if ( add ) { rtn . push ( trip ) ; } } } /**
   * Finish processing the Trip Search Dates
   * @private
   */ function _finish ( ) { done ++ ; if ( done === count ) { return callback ( null , rtn ) ; } } }
Get a list of Trips that operate during the specified TripSearchDates and
run from the specified Stop to at least one of the provided following Stops
@param {RightTrackDB} db The Right Track DB to query
@param {Stop} stop The reference Stop
@param {TripSearchDate[]} tripSearchDates List of Trip Search Dates
@param {String[]} nextStops List of following Stops (Stop IDs)
@param {function} callback Callback function
@param {Error} callback.err Database Query Error
@param {Trip[]} [callback.trips] List of matching Trips
@private
Get a list of Trips that operate during the specified TripSearchDates and run from the specified Stop to at least one of the provided following Stops