All files / modules/75-ce/components/BudgetDetailsChart budgetCategoryUtil.ts

60% Statements 15/25
12.5% Branches 1/8
100% Functions 3/3
58.33% Lines 14/24

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59              1x 1x   1x       1x 12x 12x 12x     1x     1x       1x 24x   24x                                             24x     24x    
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import moment from 'moment'
import { BudgetCostData, BudgetPeriod } from 'services/ce/services'
 
export const computeCategories: (chartData: BudgetCostData[], budgetPeriod: BudgetPeriod) => string[] = (
  chartData,
  budgetPeriod
) => {
  const cat = chartData.map(item => {
    const startTime = moment.utc(item.time)
    const endTime = moment.utc(item.endTime)
    return getTimeRangeExpression(budgetPeriod, startTime, endTime)
  })
 
  return cat
}
 
export const getTimeRangeExpression: (
  budgetPeriod: BudgetPeriod,
  startTime: moment.Moment,
  endTime: moment.Moment
) => string = (budgetPeriod, startTime, endTime) => {
  let rangeTxt = ''
 
  switch (budgetPeriod) {
    case BudgetPeriod.Monthly:
      if (startTime.get('month') === endTime.get('month')) {
        rangeTxt = startTime.format('MMM YYYY')
      } else {
        rangeTxt = `${startTime.format('D MMM YY')} -  ${endTime.format('D MMM YY')}`
      }
      break
 
    case BudgetPeriod.Quarterly:
    case BudgetPeriod.Yearly:
      rangeTxt = `${startTime.format('D MMM YYYY')} - ${endTime.format('D MMM YYYY')}`
      break
 
    case BudgetPeriod.Weekly:
      rangeTxt = `${startTime.format('D MMM')} - ${endTime.format('D MMM')}`
      break
 
    case BudgetPeriod.Daily:
      rangeTxt = `${startTime.format('D MMM')}`
      break
 
    default:
      rangeTxt = startTime.format('MMM YYYY')
  }
 
  return rangeTxt
}