File tree 2 files changed +36
-0
lines changed
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } )
You can’t perform that action at this time.
0 commit comments