All files / modules/10-common/components/Filter/utils FilterUtils.tsx

95.83% Statements 46/48
82.35% Branches 28/34
100% Functions 12/12
94.59% Lines 35/37

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              25x 25x     25x       25x             25x 21x 6x 15x 3x       3x   2x     12x 1x 11x 2x   9x     25x       6x     5x 4x                       232x 25x 25x 25x   25x     355x 355x 36x 8x   28x     355x     25x 160x     25x 2x  
/*
 * 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 { isEmpty, isUndefined, omitBy } from 'lodash-es'
import React from 'react'
import type { FilterDTO } from 'services/cd-ng'
 
import css from './FilterUtils.module.scss'
 
type supportedTypes = string | number | boolean | Record<string, any>
 
const tagSeparator = ':'
 
interface NGTag {
  key: string
  value: string
}
 
export const renderItemByType = (data: supportedTypes | Array<supportedTypes> | Record<string, any>): string => {
  if (Array.isArray(data)) {
    return data?.map(item => renderItemByType(item)).join(', ')
  } else if (typeof data === 'object') {
    Iif (Object.prototype.hasOwnProperty.call(data, 'key') && Object.prototype.hasOwnProperty.call(data, 'value')) {
      const { key, value } = data as NGTag
      return key.toString().concat(value ? tagSeparator.concat(value.toString()) : '')
    }
    return Object.entries(data)
      .map(([key, value]) => {
        return key.toString().concat(value ? tagSeparator.concat(value.toString()) : '')
      })
      .join(', ')
  } else if (typeof data === 'number') {
    return data.toString()
  } else if (typeof data === 'boolean') {
    return data ? 'true' : 'false'
  }
  return typeof data === 'string' ? data : ''
}
 
export const getFilterSummary = (
  fieldToLabelMapping: Map<string, string>,
  fields: Record<string, any>
): JSX.Element => {
  return (
    <ol className={css.noStyleUl}>
      {Object.entries(fields as Record<string, any>).map(([key, value]) => {
        if (fieldToLabelMapping.has(key)) {
          return (
            <li key={key} className={css.summaryItem}>
              <span>{fieldToLabelMapping.get(key)} : </span>
              <span style={{ fontWeight: 'bold' }}>{renderItemByType(value)}</span>
            </li>
          )
        }
      })}
    </ol>
  )
}
 
export const removeNullAndEmpty = (object: Record<string, any>) => omitBy(omitBy(object, isUndefined), isEmpty)
export const isObjectEmpty = (arg: Record<string, any>): boolean => isEmpty(omitBy(arg, isEmpty))
export const UNSAVED_FILTER = 'Unsaved Filter'
export const MAX_FILTER_NAME_LENGTH = 25
 
export const flattenObject = (obj: Record<string, any>) => {
  const flattened: {
    [key: string]: string
  } = {}
  Object.keys(obj).forEach((key: string) => {
    if (Object.prototype.toString.call(obj[key]) === '[object Object]' && obj[key] !== null) {
      Object.assign(flattened, flattenObject(obj[key]))
    } else {
      flattened[key as string] = obj[key]
    }
  })
  return flattened
}
 
export const getFilterSize = (obj: Record<string, any>): number => {
  return Object.keys(flattenObject(obj)).length
}
 
export const getFilterByIdentifier = (filters: FilterDTO[], identifier: string): FilterDTO | undefined =>
  filters.find((filter: FilterDTO) => filter.identifier?.toLowerCase() === identifier.toLowerCase())