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

Treat null as a valid plain object prototype in isPlainObject() in order to sync the util across reduxjs/* repositories #1734

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/toolkit/src/isPlainObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
export default function isPlainObject(value: unknown): value is object {
if (typeof value !== 'object' || value === null) return false

let proto = value
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto)
let proto = Object.getPrototypeOf(value)
if (proto === null) return true

let baseProto = proto
while (Object.getPrototypeOf(baseProto) !== null) {
baseProto = Object.getPrototypeOf(baseProto)
}

return Object.getPrototypeOf(value) === proto
return proto === baseProto
}
1 change: 1 addition & 0 deletions packages/toolkit/src/tests/isPlainObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ describe('isPlainObject', () => {
expect(isPlainObject(null)).toBe(false)
expect(isPlainObject(undefined)).toBe(false)
expect(isPlainObject({ x: 1, y: 2 })).toBe(true)
expect(isPlainObject(Object.create(null))).toBe(true)
})
})