Skip to content

Commit 38a231b

Browse files
committed
Add missing files.
1 parent c521bde commit 38a231b

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/utils/isPlainObject.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {any} obj The object to inspect.
3+
* @returns {boolean} True if the argument appears to be a plain object.
4+
*/
5+
export default function isPlainObject(obj) {
6+
if (typeof obj !== 'object' || obj === null) return false
7+
8+
let prototypeOfObj = Object.getPrototypeOf(obj)
9+
let proto = obj
10+
while (Object.getPrototypeOf(proto) !== null) {
11+
proto = Object.getPrototypeOf(proto)
12+
13+
}
14+
15+
return prototypeOfObj === proto
16+
}

test/utils/isPlainObject.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import isPlainObject from '../../src/utils/isPlainObject'
2+
import vm from 'vm'
3+
describe('isPlainObject', () => {
4+
it('returns true only if plain object', () => {
5+
function Test() {
6+
this.prop = 1
7+
}
8+
9+
const sandbox = { fromAnotherRealm: false }
10+
vm.runInNewContext('fromAnotherRealm = {}', sandbox)
11+
12+
expect(isPlainObject(sandbox.fromAnotherRealm)).toBe(true)
13+
expect(isPlainObject(new Test())).toBe(false)
14+
expect(isPlainObject(new Date())).toBe(false)
15+
expect(isPlainObject([ 1, 2, 3 ])).toBe(false)
16+
expect(isPlainObject(null)).toBe(false)
17+
expect(isPlainObject()).toBe(false)
18+
expect(isPlainObject({ 'x': 1, 'y': 2 })).toBe(true)
19+
})
20+
})

0 commit comments

Comments
 (0)