File tree Expand file tree Collapse file tree 3 files changed +22
-3
lines changed Expand file tree Collapse file tree 3 files changed +22
-3
lines changed Original file line number Diff line number Diff line change 46
46
"babel-core" : " ^5.6.18" ,
47
47
"babel-eslint" : " ^3.1.15" ,
48
48
"babel-loader" : " ^5.1.4" ,
49
+ "contextify" : " ^0.1.14" ,
49
50
"eslint" : " ^0.23" ,
50
51
"eslint-config-airbnb" : " 0.0.6" ,
51
52
"eslint-plugin-react" : " ^2.3.0" ,
Original file line number Diff line number Diff line change
1
+ var fnToString = ( fn ) => Function . prototype . toString . call ( fn ) ;
2
+
1
3
/**
2
4
* @param {any } obj The object to inspect.
3
5
* @returns {boolean } True if the argument appears to be a plain object.
4
6
*/
5
7
export default function isPlainObject ( obj ) {
6
- if ( ! obj ) {
8
+ if ( ! obj || typeof obj !== 'object' ) {
7
9
return false ;
8
10
}
9
11
10
- return typeof obj === 'object' &&
11
- Object . getPrototypeOf ( obj ) === Object . prototype ;
12
+ var proto = typeof obj . constructor === 'function' ? Object . getPrototypeOf ( obj ) : Object . prototype ;
13
+
14
+ if ( proto === null ) {
15
+ return true ;
16
+ }
17
+
18
+ var constructor = proto . constructor ;
19
+
20
+ return typeof constructor === 'function'
21
+ && constructor instanceof constructor
22
+ && fnToString ( constructor ) === fnToString ( Object ) ;
12
23
}
Original file line number Diff line number Diff line change 1
1
import expect from 'expect' ;
2
2
import isPlainObject from '../../src/utils/isPlainObject' ;
3
+ import contextify from 'contextify' ;
3
4
4
5
describe ( 'isPlainObject' , ( ) => {
5
6
it ( 'should return true only if plain object' , ( ) => {
6
7
function Test ( ) {
7
8
this . prop = 1 ;
8
9
}
9
10
11
+ const sandbox = contextify ( ) ;
12
+ sandbox . run ( 'var fromAnotherRealm = {};' ) ;
13
+
14
+ expect ( isPlainObject ( sandbox . fromAnotherRealm ) ) . toBe ( true ) ;
10
15
expect ( isPlainObject ( new Test ( ) ) ) . toBe ( false ) ;
11
16
expect ( isPlainObject ( new Date ( ) ) ) . toBe ( false ) ;
12
17
expect ( isPlainObject ( [ 1 , 2 , 3 ] ) ) . toBe ( false ) ;
13
18
expect ( isPlainObject ( null ) ) . toBe ( false ) ;
14
19
expect ( isPlainObject ( ) ) . toBe ( false ) ;
15
20
expect ( isPlainObject ( { 'x' : 1 , 'y' : 2 } ) ) . toBe ( true ) ;
21
+
22
+ sandbox . dispose ( ) ;
16
23
} ) ;
17
24
} ) ;
You can’t perform that action at this time.
0 commit comments