Skip to content

Commit

Permalink
fix: update isPlainObject method to account for overridden 'construct…
Browse files Browse the repository at this point in the history
…or' key in plain objects (#24)

@Gidoont thank you !
  • Loading branch information
Rigidoont committed Dec 1, 2022
1 parent 07730c2 commit 4152a01
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export function isNull(payload: any): payload is null {
*/
export function isPlainObject(payload: any): payload is PlainObject {
if (getType(payload) !== 'Object') return false
return payload.constructor === Object && Object.getPrototypeOf(payload) === Object.prototype
const prototype = Object.getPrototypeOf(payload)
return prototype.constructor === Object && prototype === Object.prototype
}

/**
Expand Down
2 changes: 2 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,10 @@ test('isObject vs isAnyObject', () => {
// plain object
expect(isObject({})).toEqual(true)
expect(isObject(new Object())).toEqual(true)
expect(isObject({ constructor: '123' })).toEqual(true)
expect(isPlainObject({})).toEqual(true)
expect(isPlainObject(new Object())).toEqual(true)
expect(isPlainObject({ constructor: '123' })).toEqual(true)
// classes & prototypes
expect(isObject(myClass)).toEqual(false)
expect(isObject(myClass2)).toEqual(false)
Expand Down

0 comments on commit 4152a01

Please sign in to comment.