Skip to content

Commit

Permalink
4033 - improve Objects.isEmpty + unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
minotogna committed Oct 16, 2024
1 parent 7a62583 commit 84c2e4b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import './DataSources.scss'
import React from 'react'
import { useTranslation } from 'react-i18next'

import { Objects } from 'utils/objects'

import { CommentableDescriptionName } from 'meta/assessment'
import { NationalDataDescription } from 'meta/assessment/description'

Expand Down Expand Up @@ -39,7 +41,7 @@ export const DataSources: React.FC<Props> = (props: Props) => {
const editable = useIsDescriptionEditable({ sectionName, name })
const { empty } = useDescriptionErrorState({ name, sectionName })

const renderGrid = Boolean(dataSources?.length || dataSourcesLinked?.length || editable)
const renderGrid = Boolean(!Objects.isEmpty(dataSources) || !Objects.isEmpty(dataSources) || editable)
const keyPrefix = `${assessmentName}.${cycleName}.description.dataSource`

return (
Expand Down
3 changes: 1 addition & 2 deletions src/utils/objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import * as isEqual from 'lodash.isequal'
// @ts-ignore
import * as isFunction from 'lodash.isfunction'
// @ts-ignore
import * as isNil from 'lodash.isnil'
// @ts-ignore
import * as pick from 'lodash.pick'
// @ts-ignore
import * as unset from 'lodash.unset'
import { getInPath } from 'utils/objects/getInPath'

import { camelize } from './camelize'
import { isEmpty } from './isEmpty'
import { isNil } from './isNil'
import { propertyOf } from './propertyOf'
import { setInPath } from './setInPath'

Expand Down
30 changes: 30 additions & 0 deletions src/utils/objects/isEmpty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { isEmpty } from 'utils/objects/isEmpty'

const tests: Array<{ empty: boolean; value: any }> = [
{ empty: true, value: undefined },
{ empty: true, value: null },
{ empty: true, value: '' },
{ empty: true, value: ' ' },
{ empty: false, value: ' fdsafd a' },
{ empty: false, value: 'fdsafd ' },
{ empty: false, value: 'fdsafd' },
{ empty: false, value: { a: '' } },
{ empty: false, value: { a: '', b: '' } },
{ empty: false, value: { a: undefined } },
{ empty: true, value: {} },
{ empty: false, value: [1] },
{ empty: false, value: [1, 2, 3] },
{ empty: true, value: [] },
{ empty: false, value: [undefined] },
{ empty: false, value: [null] },
{ empty: false, value: [undefined, null] },
]

describe('isEmpty', () => {
tests.forEach(({ empty, value }) => {
test(`${JSON.stringify(value)}-${empty}`, () => {
const result = isEmpty(value)
expect(result).toEqual(empty)
})
})
})
10 changes: 5 additions & 5 deletions src/utils/objects/isEmpty.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { isNil } from 'utils/objects/isNil'

/**
* Determines if the specified value is null, empty string, NaN, empty object or empty array.
*
* @param {any} value - Value to
* @returns {boolean} True if the specified value is empty, false otherwise.
*/
export const isEmpty = (value: any): boolean =>
value === undefined ||
value === null ||
value === '' ||
isNil(value) ||
Number.isNaN(value) ||
(value instanceof Object && Object.entries(value).length === 0) ||
(Array.isArray(value) && value.length === 0)
(typeof value === 'object' && Object.keys(value).length === 0) ||
(typeof value === 'string' && value.trim().length === 0)
4 changes: 4 additions & 0 deletions src/utils/objects/isNil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @ts-expect-error
import * as isNil from 'lodash.isnil'

export { isNil }

0 comments on commit 84c2e4b

Please sign in to comment.