Skip to content

Commit

Permalink
fix Object.create(null), add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lioeaet committed Apr 1, 2023
1 parent 7981ccf commit 80c44be
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/utils/isPlainObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* @returns True if the argument appears to be a plain object.
*/
export default function isPlainObject(obj: any): boolean {
if (typeof obj !== 'object' || obj === null) return false
if (!obj) return false

const proto = Object.getPrototypeOf(obj)
return Boolean(proto && !Object.getPrototypeOf(proto))
const proto = obj.__proto__
return !proto || !Object.getPrototypeOf(proto)
}
4 changes: 4 additions & 0 deletions test/utils/isPlainObject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ describe('isPlainObject', () => {
expect(isPlainObject(new Date())).toBe(false)
expect(isPlainObject([1, 2, 3])).toBe(false)
expect(isPlainObject(null)).toBe(false)
expect(isPlainObject('')).toBe(false)
expect(isPlainObject(true)).toBe(false)
expect(isPlainObject(undefined)).toBe(false)
expect(isPlainObject(false)).toBe(false)
expect(isPlainObject({ x: 1, y: 2 })).toBe(true)
expect(isPlainObject(Object.create(null))).toBe(true)
})
})

0 comments on commit 80c44be

Please sign in to comment.