async function compileStatsForActivities(
  activities,
  initial = {
    allTime: 0,
    single: 0,
  },
) {
  if (!activities || !activities.length) {
    return initial;
  }

  const updatedStats = activities.reduce((acc, activity) => {
    const activityLaps = activity.get('laps');
    const startDate = activity.get('start_date_local');

    // Increment allTime total
    acc.allTime += activityLaps;

    // Increment year and month allTimes
    const matches = /^(\d{4,4})-(\d{2,2})-/.exec(startDate);
    if (matches) {
      const yearKey = `_${matches[1]}`;
      const monthKey = `${yearKey}_${matches[2]}`;
      acc[yearKey] = (acc[yearKey] || 0) + activityLaps;
      acc[monthKey] = (acc[monthKey] || 0) + activityLaps;
    }

    // Most laps in a single ride?
    if (activityLaps > acc.single) {
      acc.single = activityLaps;
    }

    return acc;
  }, initial);

  // Handle async special stats outside of Array.reduce()
  // let updatedSpecialStats = initial.special ?
  //   Object.assign({}, initial.special) : {};
  // for (let i = 0; i < activities.length; i += 1) {
  //   const activity = activities[i];
  //   updatedSpecialStats = await compileSpecialStats(
  //     activity,
  //     activity.get('start_date_local'),
  //     updatedSpecialStats
  //   );
  // }

  // updatedStats.special = updatedSpecialStats;
  return updatedStats;
}
/**
 * Update athlete stats from array of new activity documents
 * Could be synchronous but don't want to break anything
 *
 * @param {Array} activities Array of Activity documents
 * @param {Object} initial Optional initial stats value
 * @return {Object}
 */
Update athlete stats from array of new activity documents
Could be synchronous but don't want to break anything

@param {Array} activities Array of Activity documents
@param {Object} initial Optional initial stats value
@return {Object}
Update athlete stats from array of new activity documents Could be synchronous but don ' t want to break anything @param { Array } activities Array of Activity documents @param { Object } initial Optional initial stats value @return { Object }
async function compileStatsForActivities(
  activities,
  initial = {
    allTime: 0,
    single: 0,
  },
) {
  if (!activities || !activities.length) {
    return initial;
  }
  const updatedStats = activities.reduce((acc, activity) => {
    const activityLaps = activity.get('laps');
    const startDate = activity.get('start_date_local');
    acc.allTime += activityLaps;
    const matches = /^(\d{4,4})-(\d{2,2})-/.exec(startDate);
    if (matches) {
      const yearKey = `_${matches[1]}`;
      const monthKey = `${yearKey}_${matches[2]}`;
      acc[yearKey] = (acc[yearKey] || 0) + activityLaps;
      acc[monthKey] = (acc[monthKey] || 0) + activityLaps;
    }
    if (activityLaps > acc.single) {
      acc.single = activityLaps;
    }
    return acc;
  }, initial);
  return updatedStats;
}
async function compileStatsForActivities ( activities , initial = { allTime : 0 , single : 0 , } , ) { if ( ! activities || ! activities . length ) { return initial ; } const updatedStats = activities . reduce ( ( acc , activity ) => { const activityLaps = activity . get ( 'laps' ) ; const startDate = activity . get ( 'start_date_local' ) ; acc . allTime += activityLaps ; const matches = / ^(\d{4,4})-(\d{2,2})- / . exec ( startDate ) ; if ( matches ) { const yearKey = ` ${ matches [ 1 ] } ` ; const monthKey = ` ${ yearKey } ${ matches [ 2 ] } ` ; acc [ yearKey ] = ( acc [ yearKey ] || 0 ) + activityLaps ; acc [ monthKey ] = ( acc [ monthKey ] || 0 ) + activityLaps ; } if ( activityLaps > acc . single ) { acc . single = activityLaps ; } return acc ; } , initial ) ; return updatedStats ; }
Update athlete stats from array of new activity documents
 Could be synchronous but don't want to break anything
Update athlete stats from array of new activity documents Could be synchronous but don ' t want to break anything
// Increment allTime total // Increment year and month allTimes // Most laps in a single ride? // Handle async special stats outside of Array.reduce() // let updatedSpecialStats = initial.special ? //   Object.assign({}, initial.special) : {}; // for (let i = 0; i < activities.length; i += 1) { //   const activity = activities[i]; //   updatedSpecialStats = await compileSpecialStats( //     activity, //     activity.get('start_date_local'), //     updatedSpecialStats //   ); // } // updatedStats.special = updatedSpecialStats;