Skip to content

Commit c9d214e

Browse files
rgrovetimdorr
authored andcommitted
Treat null as a valid plain object prototype in isPlainObject() (#1075)
Fixes #1074
1 parent 574ecc5 commit c9d214e

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/utils/isPlainObject.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
export default function isPlainObject(obj) {
66
if (typeof obj !== 'object' || obj === null) return false
77

8-
let proto = obj
9-
while (Object.getPrototypeOf(proto) !== null) {
10-
proto = Object.getPrototypeOf(proto)
8+
let proto = Object.getPrototypeOf(obj)
9+
if (proto === null) return true
10+
11+
let baseProto = proto
12+
while (Object.getPrototypeOf(baseProto) !== null) {
13+
baseProto = Object.getPrototypeOf(baseProto)
1114
}
1215

13-
return Object.getPrototypeOf(obj) === proto
16+
return proto === baseProto
1417
}

test/utils/isPlainObject.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ describe('isPlainObject', () => {
1717
expect(isPlainObject(null)).toBe(false)
1818
expect(isPlainObject()).toBe(false)
1919
expect(isPlainObject({ x: 1, y: 2 })).toBe(true)
20+
expect(isPlainObject(Object.create(null))).toBe(true)
2021
})
2122
})

0 commit comments

Comments
 (0)