All files / modules/85-cv/components/InputWithDynamicModalForJson InputWithDynamicModalForJson.utils.ts

83.33% Statements 20/24
16.67% Branches 2/12
75% Functions 3/4
80.95% Lines 17/21

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              36x       36x                 36x 2x 2x     2x 1x   1x     2x     36x 1x 1x   1x 1x   1x     36x 2x    
/*
 * 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 { isNumeric } from '@cv/utils/CommonUtils'
import type { UseStringsReturn } from 'framework/strings'
import type { NoRecordForm } from './InputWithDynamicModalForJson.types'
 
export function validate(value: NoRecordForm, getString: UseStringsReturn['getString']): { [key: string]: string } {
  const errors: { [key: string]: string } = {}
 
  if (!value?.name?.trim().length) {
    errors.name = getString('cv.onboarding.selectProductScreen.validationText.name')
  }
  return errors
}
 
export const formatJSONPath = (selectedValue: string): string => {
  let formattedJSONPath = ''
  const selectedValuePathElements = selectedValue.split('.')
 
  // replacing the array index in the path with [*]
  if (isNumeric(selectedValuePathElements[selectedValuePathElements.length - 1])) {
    formattedJSONPath = getJSONPathIfLastElementIsNum(selectedValuePathElements)
  } else {
    formattedJSONPath = replaceAllNum(selectedValue)
  }
 
  return `$.${formattedJSONPath}`
}
 
export const getJSONPathIfLastElementIsNum = (selectedValuePathElements: string[]): string => {
  const pathElementsExceptLastNumElement = selectedValuePathElements.slice(0, selectedValuePathElements.length - 1)
  const lastNumElement = selectedValuePathElements[selectedValuePathElements.length - 1]
 
  let path = pathElementsExceptLastNumElement.join('.')
  path = replaceAllNum(path)
 
  return `${path}.[${lastNumElement}]`
}
 
export const replaceAllNum = (path: string): string => {
  return path.replace(/\d/g, '[*]')
}