-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
4033 - improve Objects.isEmpty + unit test
- Loading branch information
Showing
5 changed files
with
43 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |