Skip to content

Commit 0fc5802

Browse files
committed
Merge pull request #306 from michalkvasnicak/is-plain-object-fix
fix isPlainObject
2 parents 941a2e1 + 5734779 commit 0fc5802

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"babel-core": "^5.6.18",
4747
"babel-eslint": "^3.1.15",
4848
"babel-loader": "^5.1.4",
49+
"contextify": "^0.1.14",
4950
"eslint": "^0.23",
5051
"eslint-config-airbnb": "0.0.6",
5152
"eslint-plugin-react": "^2.3.0",

src/utils/isPlainObject.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1+
var fnToString = (fn) => Function.prototype.toString.call(fn);
2+
13
/**
24
* @param {any} obj The object to inspect.
35
* @returns {boolean} True if the argument appears to be a plain object.
46
*/
57
export default function isPlainObject(obj) {
6-
if (!obj) {
8+
if (!obj || typeof obj !== 'object') {
79
return false;
810
}
911

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);
1223
}

test/utils/isPlainObject.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import expect from 'expect';
22
import isPlainObject from '../../src/utils/isPlainObject';
3+
import contextify from 'contextify';
34

45
describe('isPlainObject', () => {
56
it('should return true only if plain object', () => {
67
function Test() {
78
this.prop = 1;
89
}
910

11+
const sandbox = contextify();
12+
sandbox.run('var fromAnotherRealm = {};');
13+
14+
expect(isPlainObject(sandbox.fromAnotherRealm)).toBe(true);
1015
expect(isPlainObject(new Test())).toBe(false);
1116
expect(isPlainObject(new Date())).toBe(false);
1217
expect(isPlainObject([1, 2, 3])).toBe(false);
1318
expect(isPlainObject(null)).toBe(false);
1419
expect(isPlainObject()).toBe(false);
1520
expect(isPlainObject({ 'x': 1, 'y': 2 })).toBe(true);
21+
22+
sandbox.dispose();
1623
});
1724
});

0 commit comments

Comments
 (0)