Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update isPlainObject method to account for 'constructor' key #24

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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