All files / ee/app/assets/javascripts/analytics/code_review_analytics utils.js

100% Statements 13/13
100% Branches 8/8
100% Functions 3/3
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                      1x           4x   4x 4x   4x 2x   2x       4x 2x 2x 1x     4x        
/**
 * Transforms a given filters object
 * into the following structure:
 * {
 *  selectedLabels: [{ value: 'foo', operator: ''}, { value: 'bar', operator: '!=' }],
 *  selectedMilestone: { value: 'milestone', operator: '='}
 * }
 *
 * @param {Object} filters
 * @returns {Object}
 */
const transformFilters = (filters) => {
  const {
    label_name: labelNames,
    milestone_title: milestoneTitle,
    'not[label_name]': notLabelNames,
    'not[milestone_title]': notMilestoneTitle,
  } = filters;
 
  let selectedLabels = labelNames?.map((label) => ({ value: label, operator: '=' })) || [];
  let selectedMilestone = null;
 
  if (notLabelNames) {
    selectedLabels = [
      ...selectedLabels,
      ...notLabelNames.map((label) => ({ value: label, operator: '!=' })),
    ];
  }
 
  if (milestoneTitle) {
    selectedMilestone = { value: milestoneTitle, operator: '=' };
  } else if (notMilestoneTitle) {
    selectedMilestone = { value: notMilestoneTitle, operator: '!=' };
  }
 
  return { selectedLabels, selectedMilestone };
};
 
export default transformFilters;