Home > Enterprise >  How to reduce the the number of lines in this code
How to reduce the the number of lines in this code

Time:12-06

the following if loop is queried same as other. but how do i reduce the code to minimum number of lines. I need to reduce the complexity of this code, or reduce the number of lines but how could I do that?? I need to reduce the complexity of this code, or reduce the number of lines but how could I do that??

            if (location_id == '') {
          if (filter_by === 'Day') {
            //12 hrs in a day
            // graph_data = graph_data_of_day;
            query = {
              created_at: {
                gt: current_date,
              },
              ref: headerData?.ref,
            };
          } else if (filter_by === 'Week') {
            // 7 days a week
            // graph_data = graph_data_of_weeks;
            query = {
              created_at: {
                gte: startofweek_date,
                lte: endofweek_date,
              },
              ref: headerData?.ref,
            };
          } else if (filter_by === 'Month') {
            // 4 weeks in a month
            // graph_data = graph_/data_of_months;
            query = {
              created_at: {
                gte: startOfMonth_date,
                lte: endOfMonth_date,
              },
              ref: headerData?.ref,
            };
          } else if (filter_by === 'Year') {
            // 12 months_for_year in a year
            // graph_data = graph_data_of_year;
            query = {
              created_at: {
                gte: startOfYear_date,
                lte: endOftheYear_date,
              },
              ref: headerData?.ref,
            };
          } else if (filter_by === 'custom') {
            graph_data = [1, 2, 3, 4, 5, 7, 3, 12, 4, 2, 5, 6];
            query = {
              created_at: {
                gte: custom_start_date,
                lt: custom_end_date,
              },
            };
          }
        } else {
          //if there is location id
          if (filter_by === 'Day') {
            //12 hrs in a day
            // graph_data = graph_data_of_day;
            query = {
              created_at: {
                gt: current_date,
              },
              ref: headerData?.ref,
              location_id: location_id,
            };
          } else if (filter_by === 'Week') {
            // 7 days a week
            // graph_data = graph_data_of_weeks;
            query = {
              created_at: {
                gte: startofweek_date,
                lte: endofweek_date,
              },
              ref: headerData?.ref,
              location_id: location_id,
            };
          } else if (filter_by === 'Month') {
            // 4 weeks in a month
            // graph_data = graph_data_of_months;
            query = {
              created_at: {
                gte: startOfMonth_date,
                lte: endOfMonth_date,
              },
              ref: headerData?.ref,
              location_id: location_id,
            };
          } else if (filter_by === 'Year') {
            // 12 months_for_year in a year
            // graph_data = graph_data_of_year;
            query = {
              created_at: {
                gte: startOfYear_date,
                lte: endOftheYear_date,
              },
              ref: headerData?.ref,
              location_id: location_id,
            };
          } else if (filter_by === 'custom') {
            // graph_data = [1, 2, 3, 4, 5, 7, 3, 12, 4, 2, 5, 6];
            query = {
              created_at: {
                gte: custom_start_date,
                lt: custom_end_date,
              },
              ref: headerData?.ref,
              location_id: location_id,
            };
          }
        }

CodePudding user response:

Maybe this will work for you or give you an idea for your need.

var query = {}
var filter_by = 'Day'
var graph_data = [1, 2, 3, 4, 5, 7, 3, 12, 4, 2, 5, 6]
if (location_id == '') {
  switch(filter_by) {
    case 'Day': 
      query.created_at= {
        gt: current_date
      }
      query.ref= headerData?.ref
    break;
    case 'Week':
     query.created_at= {
        gte: startofweek_date,
        lte: endofweek_date
      }
      query.ref= headerData?.ref
     break;
     case 'Month' :
       query.created_at= {
         gte: startOfMonth_date,
         lte: endOfMonth_date
       }
       query.ref= headerData?.ref
      break;
      case 'Year': 
        query.created_at= {
          gte: startOfYear_date,
          lte: endOftheYear_date
        }
        query.ref= headerData?.ref
      break;
      default: 
        query.created_at= {
          gte: custom_start_date,
          lt: custom_end_date
        }
      break;
  }
}
  • Related