All files / app/src/shared/helpers object.helpers.ts

45.65% Statements 21/46
87.5% Branches 7/8
40% Functions 2/5
45.65% Lines 21/46

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    1x   1x                             1x 22x 44x 44x 44x 44x       44x 44x 22x   1x                       1x 16x 16x 586x 10x 10x 16x 16x 16x  
import { LegacyRef, MutableRefObject, RefCallback } from 'react'
 
export const getDeepClone = <T>(data: T): T => JSON.parse(JSON.stringify(data))
 
export const getObjectWithoutEmptyProperty = <T extends Record<string, unknown>>(object?: T): Partial<T> =>
  object
    ? Object.keys(object).reduce((acc, key) => {
        const value = object[key]
        if (
          !value ||
          (Array.isArray(value) && !value.length) ||
          (typeof value === 'object' && !Object.keys(value!).length)
        ) {
          return acc
        }
        return { ...acc, [key]: value }
      }, {})
    : {}
 
export function mergeRefs<T = any>(refs: Array<MutableRefObject<T> | LegacyRef<T>>): RefCallback<T> {
  return value => {
    refs.forEach(ref => {
      if (typeof ref === 'function') {
        ref(value)
      } else if (ref != null) {
        // eslint-disable-next-line
        ;(ref as MutableRefObject<T | null>).current = value
      }
    })
  }
}
 
export const normalizeObjectForIri = (object: Record<string, any>) => {
  const result: Record<string, unknown> = {}
  Object.keys(object).forEach(key => {
    if (object[key] && object[key]['@id']) {
      result[key] = object[key]['@id']
    } else {
      result[key] = object[key]
    }
  })
  return result
}
 
export const removeEmptyProperties = <T extends Record<string, unknown>>(object: T) => {
  const result: Record<string, unknown> = {}
  Object.keys(object).forEach(key => {
    if (object[key] !== null && object[key] !== undefined) {
      result[key] = object[key]
    }
  })
  return result
}