Skip to content

Commit

Permalink
fix(utils): the isObject method was not always correct (#745)
Browse files Browse the repository at this point in the history
* fix(utils): the `isObject` method was not always correct
  • Loading branch information
ghiscoding authored Aug 13, 2022
1 parent 72a6f24 commit 9b09e4a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions packages/utils/src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
hasData,
isEmptyObject,
isNumber,
isObject,
isObjectEmpty,
parseBoolean,
removeAccentFromText,
Expand Down Expand Up @@ -112,6 +113,40 @@ describe('Service/Utilies', () => {
});
});

describe('isObject method', () => {
it('should return false when input is undefined', () => {
expect(isObject(undefined)).toBeFalse();
});

it('should return false when input is null', () => {
expect(isObject(null)).toBeFalse();
});

it('should return false when input is empty string', () => {
expect(isObject('')).toBeFalse();
});

it('should return false when input is a string', () => {
expect(isObject('some text')).toBeFalse();
});

it('should return false when input is an empty array', () => {
expect(isObject([])).toBeFalse();
});

it('should return false when input a Date', () => {
expect(isObject(new Date())).toBeFalse();
});

it('should return true when input is an empty object', () => {
expect(isObject({})).toBeTrue();
});

it('should return true when input is an object', () => {
expect(isObject({ msg: 'hello workd' })).toBeTrue();
});
});

describe('isObjectEmpty method', () => {
it('should return True when input is undefined', () => {
const result = isObjectEmpty(undefined);
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export function isEmptyObject(obj: any): boolean {
* @returns {boolean}
*/
export function isObject(item: any) {
return (item && typeof item === 'object' && !Array.isArray(item));
return item !== null && typeof item === 'object' && !Array.isArray(item) && !(item instanceof Date);
}

/**
Expand Down

0 comments on commit 9b09e4a

Please sign in to comment.