function filterSegmentEfforts(segmentEfforts, potentialLocations = []) {
  const allRelevantSegmentIds = getAllSegmentIdsForAllLocations(
    potentialLocations,
  );
  const searchLocNames = potentialLocations.length
    ? potentialLocations
    : Object.keys(allLocations);

  // Set up object for each location to receive segment efforts
  const filteredEfforts = searchLocNames.reduce((acc, locName) => {
    acc[locName] = {
      relevantSegmentEfforts: [],
      canonicalSegmentEfforts: [],
      lapDefinitionsSegmentEfforts: [],
    };
    return acc;
  }, {});

  // Loop through all efforts
  segmentEfforts.forEach((effort) => {
    const { segment } = effort;
    const { id: segmentIdForEffort } = segment;

    // Skip all segments efforts that aren't relevant to any of our locations
    if (allRelevantSegmentIds.indexOf(segmentIdForEffort) === -1) {
      return;
    }

    // Now we know that this segment effort is relevant to one of our locations.
    // Let's figure out which one.
    searchLocNames.forEach((locName) => {
      const { canonicalSegmentId } = allLocations[locName];
      const {
        relevantSegmentEfforts,
        lapDefinitionsSegmentEfforts,
        canonicalSegmentEfforts,
      } = filteredEfforts[locName];

      if (getAllSegmentIdsForLocation(locName)
        .indexOf(segmentIdForEffort) === -1
        || isDuplicateEffort(effort, relevantSegmentEfforts)
      ) {
        // Skip if not relevant to this location or is a duplicate
        return;
      }

      // Add everything to relevant segments
      relevantSegmentEfforts.push(effort);

      const definitionIds = getlapDefinitionsIds(locName);
      // Add lap definition efforts if using lap definitions method
      if (definitionIds.length
        && definitionIds.indexOf(segmentIdForEffort) !== -1
      ) {
        lapDefinitionsSegmentEfforts.push(effort);
      }

      if (segmentIdForEffort === canonicalSegmentId) {
        canonicalSegmentEfforts.push(effort);
      }
    });
  });
  // Now each location has:
  // relevantSegmentEfforts: Deduped array of all segment efforts for canonical lap, lap sections (v1), and lap definitions (v2)
  // lapDefinitionsSegmentEfforts: Deduped array of all segment efforts for lap definition segments (v2)
  // canonicalSegmentEfforts: Deduped array of all segment efforts for the canonical lap
  return filteredEfforts;
}
/**
 * Filter and dedupe all segment efforts by canonicalSegmentId, sectionSegmentIds, lapDefinitions
 *
 * @param {Array} segmentEfforts
 * @param {Array} potentialLocations Optional. Specify location names to look for
 * @returns {Object} locations
 * @returns {Array} locations[locName].canonicalSegmentEfforts
 * @returns {Array} locations[locName].lapDefinitionsSegmentEfforts
 * @returns {Array} locations[locName].relevantSegmentEfforts
 */
Filter and dedupe all segment efforts by canonicalSegmentId, sectionSegmentIds, lapDefinitions
@param {Array} segmentEfforts
@param {Array} potentialLocations Optional. Specify location names to look for
@returns {Object} locations
@returns {Array} locations[locName].canonicalSegmentEfforts
@returns {Array} locations[locName].lapDefinitionsSegmentEfforts
@returns {Array} locations[locName].relevantSegmentEfforts
Filter and dedupe all segment efforts by canonicalSegmentId sectionSegmentIds lapDefinitions @param { Array } segmentEfforts @param { Array } potentialLocations Optional . Specify location names to look for @returns { Object } locations @returns { Array } locations [ locName ] . canonicalSegmentEfforts @returns { Array } locations [ locName ] . lapDefinitionsSegmentEfforts @returns { Array } locations [ locName ] . relevantSegmentEfforts
function filterSegmentEfforts(segmentEfforts, potentialLocations = []) {
  const allRelevantSegmentIds = getAllSegmentIdsForAllLocations(
    potentialLocations,
  );
  const searchLocNames = potentialLocations.length
    ? potentialLocations
    : Object.keys(allLocations);
  const filteredEfforts = searchLocNames.reduce((acc, locName) => {
    acc[locName] = {
      relevantSegmentEfforts: [],
      canonicalSegmentEfforts: [],
      lapDefinitionsSegmentEfforts: [],
    };
    return acc;
  }, {});
  segmentEfforts.forEach((effort) => {
    const { segment } = effort;
    const { id: segmentIdForEffort } = segment;
    if (allRelevantSegmentIds.indexOf(segmentIdForEffort) === -1) {
      return;
    }
    searchLocNames.forEach((locName) => {
      const { canonicalSegmentId } = allLocations[locName];
      const {
        relevantSegmentEfforts,
        lapDefinitionsSegmentEfforts,
        canonicalSegmentEfforts,
      } = filteredEfforts[locName];
      if (getAllSegmentIdsForLocation(locName)
        .indexOf(segmentIdForEffort) === -1
        || isDuplicateEffort(effort, relevantSegmentEfforts)
      ) {
        return;
      }
      relevantSegmentEfforts.push(effort);
      const definitionIds = getlapDefinitionsIds(locName);
      if (definitionIds.length
        && definitionIds.indexOf(segmentIdForEffort) !== -1
      ) {
        lapDefinitionsSegmentEfforts.push(effort);
      }
      if (segmentIdForEffort === canonicalSegmentId) {
        canonicalSegmentEfforts.push(effort);
      }
    });
  });
  return filteredEfforts;
}
function filterSegmentEfforts ( segmentEfforts , potentialLocations = [ ] ) { const allRelevantSegmentIds = getAllSegmentIdsForAllLocations ( potentialLocations , ) ; const searchLocNames = potentialLocations . length ? potentialLocations : Object . keys ( allLocations ) ; const filteredEfforts = searchLocNames . reduce ( ( acc , locName ) => { acc [ locName ] = { relevantSegmentEfforts : [ ] , canonicalSegmentEfforts : [ ] , lapDefinitionsSegmentEfforts : [ ] , } ; return acc ; } , { } ) ; segmentEfforts . forEach ( ( effort ) => { const { segment } = effort ; const { id : segmentIdForEffort } = segment ; if ( allRelevantSegmentIds . indexOf ( segmentIdForEffort ) === - 1 ) { return ; } searchLocNames . forEach ( ( locName ) => { const { canonicalSegmentId } = allLocations [ locName ] ; const { relevantSegmentEfforts , lapDefinitionsSegmentEfforts , canonicalSegmentEfforts , } = filteredEfforts [ locName ] ; if ( getAllSegmentIdsForLocation ( locName ) . indexOf ( segmentIdForEffort ) === - 1 || isDuplicateEffort ( effort , relevantSegmentEfforts ) ) { return ; } relevantSegmentEfforts . push ( effort ) ; const definitionIds = getlapDefinitionsIds ( locName ) ; if ( definitionIds . length && definitionIds . indexOf ( segmentIdForEffort ) !== - 1 ) { lapDefinitionsSegmentEfforts . push ( effort ) ; } if ( segmentIdForEffort === canonicalSegmentId ) { canonicalSegmentEfforts . push ( effort ) ; } } ) ; } ) ; return filteredEfforts ; }
Filter and dedupe all segment efforts by canonicalSegmentId, sectionSegmentIds, lapDefinitions
 @param {Array} segmentEfforts
 @param {Array} potentialLocations Optional.
Filter and dedupe all segment efforts by canonicalSegmentId sectionSegmentIds lapDefinitions @param { Array } segmentEfforts @param { Array } potentialLocations Optional .
// Set up object for each location to receive segment efforts // Loop through all efforts // Skip all segments efforts that aren't relevant to any of our locations // Now we know that this segment effort is relevant to one of our locations. // Let's figure out which one. // Skip if not relevant to this location or is a duplicate // Add everything to relevant segments // Add lap definition efforts if using lap definitions method // Now each location has: // relevantSegmentEfforts: Deduped array of all segment efforts for canonical lap, lap sections (v1), and lap definitions (v2) // lapDefinitionsSegmentEfforts: Deduped array of all segment efforts for lap definition segments (v2) // canonicalSegmentEfforts: Deduped array of all segment efforts for the canonical lap