All files / modules/75-cd/components/Services common.ts

100% Statements 27/27
100% Branches 12/12
100% Functions 4/4
100% Lines 23/23

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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86              28x 28x       28x 28x 28x                               28x           28x   28x                       28x         102x     95x       95x 74x 148x 4x   4x     4x       91x     28x 113x 91x   22x     28x  
/*
 * 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 { createContext, createRef, Dispatch, MutableRefObject, SetStateAction, useContext } from 'react'
import { noop } from 'lodash-es'
import type { DateRange } from '@blueprintjs/datetime'
import type { Module } from '@common/interfaces/RouteInterfaces'
 
export enum Views {
  LIST,
  INSIGHT
}
 
export type ParamsType = {
  orgIdentifier: string
  projectIdentifier: string
  accountId: string
  module: Module
}
 
export interface ServiceStore {
  view: Views
  setView: Dispatch<SetStateAction<Views>>
  fetchDeploymentList: MutableRefObject<(() => void) | unknown>
}
 
export const ServiceStoreContext = createContext({
  view: Views.INSIGHT,
  setView: noop,
  fetchDeploymentList: createRef()
})
 
export const useServiceStore = (): ServiceStore => useContext(ServiceStoreContext)
 
export const DeploymentsTimeRangeContext = createContext<{
  timeRange: { range: DateRange; label: string } | null
  setTimeRange: (timeRange: { range: DateRange; label: string }) => void
}>({
  timeRange: null,
  setTimeRange: noop
})
 
export interface NumberFormatterOptions {
  truncate?: boolean
}
 
export const numberFormatter: (value?: number, options?: NumberFormatterOptions) => string = (
  value?: number,
  options = { truncate: true }
) => {
  // istanbul ignore if
  if (value === undefined) {
    return ''
  }
  const truncateOptions = [
    { value: 1000000, suffix: 'm' },
    { value: 1000, suffix: 'k' }
  ]
  if (options.truncate) {
    for (const truncateOption of truncateOptions) {
      if (value >= truncateOption.value) {
        const truncatedValue = value / truncateOption.value
        // istanbul ignore if
        if (truncatedValue % 1 !== 0) {
          return `${truncatedValue.toFixed(1)}${truncateOption.suffix}`
        }
        return `${truncatedValue}${truncateOption.suffix}`
      }
    }
  }
  return `${getFixed(value)}`
}
 
export const getFixed = (value: number, places = 1): number => {
  if (value % 1 === 0) {
    return value
  }
  return parseFloat(value.toFixed(places))
}
 
export const INVALID_CHANGE_RATE = -10000