All files / modules/10-common/utils utils.ts

81.48% Statements 44/54
53.13% Branches 17/32
70% Functions 7/10
76.74% Lines 33/43

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 87 88 89 90 91 92 93 94                381x 381x               381x 417x   231x   83x   19x   19x   65x         381x 19x   1x   1x   17x       381x 32x         381x 157x 157x 12x 12x 12x 12x 1x   11x   145x     381x   381x   381x             381x         381x                     381x 46x  
/*
 * Copyright 2022 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 type { IconName } from '@harness/uicore'
import { Scope } from '@common/interfaces/SecretsInterface'
import { ModuleName } from 'framework/types/ModuleName'
 
interface SetPageNumberProps {
  setPage: (value: React.SetStateAction<number>) => void
  pageItemsCount?: number
  page: number
}
 
export const getModuleIcon = (module: ModuleName): IconName => {
  switch (module) {
    case ModuleName.CD:
      return 'cd-main'
    case ModuleName.CV:
      return 'cv-main'
    case ModuleName.CI:
      return 'ci-main'
    case ModuleName.CE:
      return 'ce-main'
    case ModuleName.CF:
      return 'cf-main'
  }
  return 'nav-project'
}
 
export const getReference = (scope?: Scope, identifier?: string): string | undefined => {
  switch (scope) {
    case Scope.PROJECT:
      return identifier
    case Scope.ORG:
      return `org.${identifier}`
    case Scope.ACCOUNT:
      return `account.${identifier}`
  }
}
 
export const setPageNumber = ({ setPage, pageItemsCount, page }: SetPageNumberProps): void => {
  Iif (pageItemsCount === 0 && page > 0) {
    setPage(page - 1)
  }
}
 
export const formatCount = (num: number): string => {
  const min = 1e3
  if (num >= min) {
    const units = ['k', 'M', 'B', 'T']
    const order = Math.floor(Math.log(num) / Math.log(1000))
    const finalNum = Math.round(num / 1000 ** order)
    if (finalNum === min && order < units.length) {
      return 1 + units[order]
    }
    return finalNum + units[order - 1]
  }
  return num.toLocaleString()
}
 
export const isCommunityPlan = (): boolean => window.deploymentType === 'COMMUNITY'
 
export const isOnPrem = (): boolean => window.deploymentType === 'ON_PREM'
 
export const focusOnNode = (node: HTMLElement): void => {
  const oldTabIndex = node.tabIndex
  node.tabIndex = -1
  node.focus()
  node.tabIndex = oldTabIndex
}
 
const HOTJAR_SUPPRESSION_ATTR = 'data-hj-suppress'
 
// Utility to add `data-hj-suppress` into a collection of elements to
// suppress data from HotJar recording
// @see https://bit.ly/3rCgpOY
export const suppressHotJarRecording = (elements: Element[] | null | undefined): void => {
  if (window.hj) {
    elements?.forEach?.((e: Element) => {
      if (!e.hasAttribute(HOTJAR_SUPPRESSION_ATTR)) {
        e.setAttribute(HOTJAR_SUPPRESSION_ATTR, 'true')
      }
    })
  }
}
 
// Utility to generate { 'data-hj-suppress': true } attribute if HotJar is available
export const addHotJarSuppressionAttribute = (): { [HOTJAR_SUPPRESSION_ATTR]: boolean } | undefined =>
  window.hj ? { [HOTJAR_SUPPRESSION_ATTR]: true } : undefined