Skip to content

Commit

Permalink
fix: isObject utils fn should return only boolean value (hyperlane-xy…
Browse files Browse the repository at this point in the history
…z#4756)

### Description
When `isObject` pass with param `null/undefined` it returns
`null/undefined`

We should strict return boolean value
<!--
What's included in this PR?
-->

### Drive-by changes
```diff
export function isObject(item: any): boolean {
- return item && typeof item === 'object' && !Array.isArray(item);
+ return !!item && typeof item === 'object' && !Array.isArray(item);
}
```
<!--
Are there any minor or drive-by changes also included?
-->

### Related issues

<!--
- Fixes #[issue number here]
-->

### Backward compatibility

<!--
Are these changes backward compatible? Are there any infrastructure
implications, e.g. changes that would prohibit deploying older commits
using this infra tooling?

Yes/No
-->

### Testing
Has updated with test `object.test.ts`
<!--
What kind of testing have these changes undergone?

None/Manual/Unit Tests
-->
  • Loading branch information
tiendn authored Oct 25, 2024
1 parent d5bdb2c commit a36fc5f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-spoons-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperlane-xyz/utils': patch
---

fix: isObject utils fn should return only boolean value
9 changes: 9 additions & 0 deletions typescript/utils/src/objects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
deepCopy,
deepEquals,
diffObjMerge,
isObject,
objMerge,
objOmit,
} from './objects.js';
Expand Down Expand Up @@ -74,6 +75,14 @@ describe('Object utilities', () => {
expect(omitted1_2).to.eql({ a: 1, b: { d: 'string' } });
});

it('isObject', () => {
expect(isObject({})).to.be.true;
expect(isObject([])).to.be.false;
expect(isObject(null)).to.be.false;
expect(isObject(undefined)).to.be.false;
expect(isObject(42)).to.be.false;
});

describe('diffObjMerge', () => {
it('should merge objects with equal values', () => {
const actual = { a: 1, b: 2 };
Expand Down
4 changes: 2 additions & 2 deletions typescript/utils/src/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { ethersBigNumberSerializer } from './logging.js';
import { isNullish } from './typeof.js';
import { assert } from './validation.js';

export function isObject(item: any) {
return item && typeof item === 'object' && !Array.isArray(item);
export function isObject(item: any): boolean {
return !!item && typeof item === 'object' && !Array.isArray(item);
}

export function deepEquals(v1: any, v2: any) {
Expand Down

0 comments on commit a36fc5f

Please sign in to comment.