Skip to content

Commit

Permalink
Treat null as a valid plain object prototype in isPlainObject() (r…
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrove authored and timdorr committed Nov 10, 2018
1 parent dc70fb9 commit a18608c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/utils/isPlainObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
export default function isPlainObject(obj) {
if (typeof obj !== 'object' || obj === null) return false

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

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

return Object.getPrototypeOf(obj) === proto
return proto === baseProto
}
1 change: 1 addition & 0 deletions test/utils/isPlainObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ describe('isPlainObject', () => {
expect(isPlainObject(null)).toBe(false)
expect(isPlainObject()).toBe(false)
expect(isPlainObject({ x: 1, y: 2 })).toBe(true)
expect(isPlainObject(Object.create(null))).toBe(true)
})
})

0 comments on commit a18608c

Please sign in to comment.