Skip to content

Commit c9a6549

Browse files
authored
Merge pull request #792 from plotly/enforce-is-plain-object
Enforce is-plain-object
2 parents 8fc47ef + e1f6818 commit c9a6549

File tree

6 files changed

+20
-14
lines changed

6 files changed

+20
-14
lines changed

src/components/colorbar/has_colorbar.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99

1010
'use strict';
1111

12+
var Lib = require('../../lib');
13+
1214

1315
module.exports = function hasColorbar(container) {
14-
return (
15-
typeof container.colorbar === 'object' &&
16-
container.colorbar !== null
17-
);
16+
return Lib.isPlainObject(container.colorbar);
1817
};

src/components/colorscale/has_colorscale.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ module.exports = function hasColorscale(trace, containerStr) {
3333
}
3434

3535
return (
36-
(typeof container === 'object' && container !== null) && (
36+
Lib.isPlainObject(container) && (
3737
isArrayWithOneNumber ||
3838
container.showscale === true ||
3939
(isNumeric(container.cmin) && isNumeric(container.cmax)) ||
4040
isValidScale(container.colorscale) ||
41-
(typeof container.colorbar === 'object' && container.colorbar !== null)
41+
Lib.isPlainObject(container.colorbar)
4242
)
4343
);
4444
};

src/components/rangeslider/defaults.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ var attributes = require('./attributes');
1313

1414

1515
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, axName, counterAxes) {
16-
1716
if(!layoutIn[axName].rangeslider) return;
1817

19-
var containerIn = typeof layoutIn[axName].rangeslider === 'object' ?
18+
var containerIn = Lib.isPlainObject(layoutIn[axName].rangeslider) ?
2019
layoutIn[axName].rangeslider : {},
2120
containerOut = layoutOut[axName].rangeslider = {};
2221

src/lib/is_plain_object.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111

1212
// more info: http://stackoverflow.com/questions/18531624/isplainobject-thing
1313
module.exports = function isPlainObject(obj) {
14+
15+
// We need to be a little less strict in the `imagetest` container because
16+
// of how async image requests are handled.
17+
//
18+
// N.B. isPlainObject(new Constructor()) will return true in `imagetest`
19+
if(window && window.process && window.process.versions) {
20+
return Object.prototype.toString.call(obj) === '[object Object]';
21+
}
22+
1423
return (
1524
Object.prototype.toString.call(obj) === '[object Object]' &&
1625
Object.getPrototypeOf(obj) === Object.prototype

src/plots/mapbox/layers.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,8 @@ proto.dispose = function dispose() {
131131
function isVisible(opts) {
132132
var source = opts.source;
133133

134-
// For some weird reason Lib.isPlainObject fails
135-
// to detect `source` as a plain object in nw.js 0.12.
136-
137134
return (
138-
typeof source === 'object' ||
135+
Lib.isPlainObject(source) ||
139136
(typeof source === 'string' && source.length > 0)
140137
);
141138
}

src/traces/scatter/subtypes.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
'use strict';
1111

12+
var Lib = require('../../lib');
13+
1214
module.exports = {
1315
hasLines: function(trace) {
1416
return trace.visible && trace.mode &&
@@ -26,7 +28,7 @@ module.exports = {
2628
},
2729

2830
isBubble: function(trace) {
29-
return (typeof trace.marker === 'object' &&
30-
Array.isArray(trace.marker.size));
31+
return Lib.isPlainObject(trace.marker) &&
32+
Array.isArray(trace.marker.size);
3133
}
3234
};

0 commit comments

Comments
 (0)