Skip to content

Commit 2d81bdc

Browse files
committed
replace Array.isArray -> Lib.isArrayOrTypedArray in various places
... that should accept typed array (mostly arrayOk attribute, with marker.size and marker.color being to most likely candidates for typed array inputs).
1 parent 45c2f35 commit 2d81bdc

28 files changed

+64
-56
lines changed

src/lib/coerce.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ exports.coerce = function(containerIn, containerOut, attributes, attribute, dflt
322322
* individual form (eg. some array vals can be numbers, even if the
323323
* single values must be color strings)
324324
*/
325-
if(opts.arrayOk && Array.isArray(v)) {
325+
if(opts.arrayOk && isArrayOrTypedArray(v)) {
326326
propOut.set(v);
327327
return v;
328328
}
@@ -419,7 +419,7 @@ exports.coerceSelectionMarkerOpacity = function(traceOut, coerce) {
419419
//
420420
// Only give [un]selected.marker.opacity a default value if you don't
421421
// set any other [un]selected attributes.
422-
if(!Array.isArray(mo) && !traceOut.selected && !traceOut.unselected) {
422+
if(!isArrayOrTypedArray(mo) && !traceOut.selected && !traceOut.unselected) {
423423
smoDflt = mo;
424424
usmoDflt = DESELECTDIM * mo;
425425
}
@@ -431,7 +431,7 @@ exports.coerceSelectionMarkerOpacity = function(traceOut, coerce) {
431431
exports.validate = function(value, opts) {
432432
var valObjectDef = exports.valObjectMeta[opts.valType];
433433

434-
if(opts.arrayOk && Array.isArray(value)) return true;
434+
if(opts.arrayOk && isArrayOrTypedArray(value)) return true;
435435

436436
if(valObjectDef.validateFunction) {
437437
return valObjectDef.validateFunction(value, opts);

src/lib/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ lib.noneOrAll = function(containerIn, containerOut, attrList) {
392392
* @param {string} cdAttr : calcdata key
393393
*/
394394
lib.mergeArray = function(traceAttr, cd, cdAttr) {
395-
if(Array.isArray(traceAttr)) {
395+
if(lib.isArrayOrTypedArray(traceAttr)) {
396396
var imax = Math.min(traceAttr.length, cd.length);
397397
for(var i = 0; i < imax; i++) cd[i][cdAttr] = traceAttr[i];
398398
}
@@ -411,7 +411,7 @@ lib.mergeArray = function(traceAttr, cd, cdAttr) {
411411
lib.fillArray = function(traceAttr, cd, cdAttr, fn) {
412412
fn = fn || lib.identity;
413413

414-
if(Array.isArray(traceAttr)) {
414+
if(lib.isArrayOrTypedArray(traceAttr)) {
415415
for(var i = 0; i < cd.length; i++) {
416416
cd[i][cdAttr] = fn(traceAttr[i]);
417417
}
@@ -432,7 +432,8 @@ lib.castOption = function(trace, ptNumber, astr, fn) {
432432

433433
var val = lib.nestedProperty(trace, astr).get();
434434

435-
if(Array.isArray(val)) {
435+
if(lib.isArrayOrTypedArray(val)) {
436+
// TODO what to do with 2d arrays?
436437
if(Array.isArray(ptNumber) && Array.isArray(val[ptNumber[0]])) {
437438
return fn(val[ptNumber[0]][ptNumber[1]]);
438439
} else {

src/plot_api/plot_schema.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ exports.findArrayAttributes = function(trace) {
191191

192192
var astr = toAttrString(stack);
193193
var val = Lib.nestedProperty(trace, astr).get();
194-
if(!Array.isArray(val)) return;
194+
if(!Lib.isArrayOrTypedArray(val)) return;
195195

196196
arrayAttributes.push(astr);
197197
}

src/plot_api/validate.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

12-
1311
var Lib = require('../lib');
1412
var Plots = require('../plots/plots');
1513
var PlotSchema = require('./plot_schema');
1614
var dfltConfig = require('./plot_config');
1715

1816
var isPlainObject = Lib.isPlainObject;
1917
var isArray = Array.isArray;
20-
18+
var isArrayOrTypedArray = Lib.isArrayOrTypedArray;
2119

2220
/**
2321
* Validate a data array and layout object.
@@ -211,7 +209,7 @@ function crawl(objIn, objOut, schema, list, base, path) {
211209
else if(!isPlainObject(valIn) && isPlainObject(valOut)) {
212210
list.push(format('object', base, p, valIn));
213211
}
214-
else if(!isArray(valIn) && isArray(valOut) && !isInfoArray && !isColorscale) {
212+
else if(!isArrayOrTypedArray(valIn) && isArrayOrTypedArray(valOut) && !isInfoArray && !isColorscale) {
215213
list.push(format('array', base, p, valIn));
216214
}
217215
else if(!(k in objOut)) {

src/plots/gl3d/scene.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,14 @@ var axisProperties = [ 'xaxis', 'yaxis', 'zaxis' ];
308308

309309
function coordinateBound(axis, coord, len, d, bounds, calendar) {
310310
var x;
311-
if(!Array.isArray(coord)) {
311+
if(!Lib.isArrayOrTypedArray(coord)) {
312312
bounds[0][d] = Math.min(bounds[0][d], 0);
313313
bounds[1][d] = Math.max(bounds[1][d], len - 1);
314314
return;
315315
}
316316

317317
for(var i = 0; i < (len || coord.length); ++i) {
318-
if(Array.isArray(coord[i])) {
318+
if(Lib.isArrayOrTypedArray(coord[i])) {
319319
for(var j = 0; j < coord[i].length; ++j) {
320320
x = axis.d2l(coord[i][j], 0, calendar);
321321
if(!isNaN(x) && isFinite(x)) {

src/plots/mapbox/convert_text_opts.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
var Lib = require('../../lib');
1313

14-
1514
/**
1615
* Convert plotly.js 'textposition' to mapbox-gl 'anchor' and 'offset'
1716
* (with the help of the icon size).
@@ -29,7 +28,7 @@ module.exports = function convertTextOpts(textposition, iconSize) {
2928
hPos = parts[1];
3029

3130
// ballpack values
32-
var factor = Array.isArray(iconSize) ? Lib.mean(iconSize) : iconSize,
31+
var factor = Lib.isArrayOrTypedArray(iconSize) ? Lib.mean(iconSize) : iconSize,
3332
xInc = 0.5 + (factor / 100),
3433
yInc = 1.5 + (factor / 100);
3534

src/traces/bar/calc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
var isNumeric = require('fast-isnumeric');
1313

14+
var Lib = require('../../lib');
1415
var Axes = require('../../plots/cartesian/axes');
1516
var hasColorscale = require('../../components/colorscale/has_colorscale');
1617
var colorscaleCalc = require('../../components/colorscale/calc');
@@ -63,7 +64,7 @@ module.exports = function calc(gd, trace) {
6364
var base = trace.base,
6465
b;
6566

66-
if(Array.isArray(base)) {
67+
if(Lib.isArrayOrTypedArray(base)) {
6768
for(i = 0; i < Math.min(base.length, cd.length); i++) {
6869
b = sa.d2c(base[i], 0, scalendar);
6970
if(isNumeric(b)) {

src/traces/bar/set_positions.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var isNumeric = require('fast-isnumeric');
1313
var BADNUM = require('../../constants/numerical').BADNUM;
1414

15+
var Lib = require('../../lib');
1516
var Registry = require('../../registry');
1617
var Axes = require('../../plots/cartesian/axes');
1718
var Sieve = require('./sieve.js');
@@ -311,7 +312,7 @@ function applyAttributes(sieve) {
311312
initialPoffset = t.poffset,
312313
newPoffset;
313314

314-
if(Array.isArray(offset)) {
315+
if(Lib.isArrayOrTypedArray(offset)) {
315316
// if offset is an array, then clone it into t.poffset.
316317
newPoffset = offset.slice(0, calcTrace.length);
317318

@@ -337,7 +338,7 @@ function applyAttributes(sieve) {
337338
var width = fullTrace.width,
338339
initialBarwidth = t.barwidth;
339340

340-
if(Array.isArray(width)) {
341+
if(Lib.isArrayOrTypedArray(width)) {
341342
// if width is an array, then clone it into t.barwidth.
342343
var newBarwidth = width.slice(0, calcTrace.length);
343344

src/traces/box/calc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ function arraysToCalcdata(pt, trace, i) {
220220
}
221221

222222
function calcSelection(cd, trace) {
223-
if(Array.isArray(trace.selectedpoints)) {
223+
if(Lib.isArrayOrTypedArray(trace.selectedpoints)) {
224224
for(var i = 0; i < cd.length; i++) {
225225
var pts = cd[i].pts || [];
226226
var ptNumber2cdIndex = {};

src/traces/carpet/array_minmax.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
'use strict';
1010

11+
var isArrayOrTypedArray = require('../../lib').isArrayOrTypedArray;
12+
1113
module.exports = function(a) {
1214
return minMax(a, 0);
1315
};
@@ -16,7 +18,7 @@ function minMax(a, depth) {
1618
// Limit to ten dimensional datasets. This seems *exceedingly* unlikely to
1719
// ever cause problems or even be a concern. It's include strictly so that
1820
// circular arrays could never cause this to loop.
19-
if(!Array.isArray(a) || depth >= 10) {
21+
if(!isArrayOrTypedArray(a) || depth >= 10) {
2022
return null;
2123
}
2224

@@ -26,7 +28,7 @@ function minMax(a, depth) {
2628
for(var i = 0; i < n; i++) {
2729
var datum = a[i];
2830

29-
if(Array.isArray(datum)) {
31+
if(isArrayOrTypedArray(datum)) {
3032
var result = minMax(datum, depth + 1);
3133

3234
if(result) {

src/traces/carpet/axis_aligned_line.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
'use strict';
1010

11+
var isArrayOrTypedArray = require('../../lib').isArrayOrTypedArray;
12+
1113
/* This function retrns a set of control points that define a curve aligned along
1214
* either the a or b axis. Exactly one of a or b must be an array defining the range
1315
* spanned.
@@ -19,7 +21,7 @@ module.exports = function(carpet, carpetcd, a, b) {
1921
var idx, tangent, tanIsoIdx, tanIsoPar, segment, refidx;
2022
var p0, p1, v0, v1, start, end, range;
2123

22-
var axis = Array.isArray(a) ? 'a' : 'b';
24+
var axis = isArrayOrTypedArray(a) ? 'a' : 'b';
2325
var ax = axis === 'a' ? carpet.aaxis : carpet.baxis;
2426
var smoothing = ax.smoothing;
2527
var toIdx = axis === 'a' ? carpet.a2i : carpet.b2j;

src/traces/carpet/has_columns.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

11+
var isArrayOrTypedArray = require('../../lib').isArrayOrTypedArray;
12+
1213
module.exports = function(data) {
13-
return Array.isArray(data[0]);
14+
return isArrayOrTypedArray(data[0]);
1415
};

src/traces/carpet/map_1d_array.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
'use strict';
1010

11+
var isArrayOrTypedArray = require('../../lib').isArrayOrTypedArray;
12+
1113
/*
1214
* Map an array of x or y coordinates (c) to screen-space pixel coordinates (p).
1315
* The output array is optional, but if provided, it will be reused without
@@ -16,7 +18,7 @@
1618
module.exports = function mapArray(out, data, func) {
1719
var i;
1820

19-
if(!Array.isArray(out)) {
21+
if(!isArrayOrTypedArray(out)) {
2022
// If not an array, make it an array:
2123
out = [];
2224
} else if(out.length > data.length) {

src/traces/carpet/map_2d_array.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
'use strict';
1010

11+
var isArrayOrTypedArray = require('../../lib').isArrayOrTypedArray;
12+
1113
/*
1214
* Map an array of x or y coordinates (c) to screen-space pixel coordinates (p).
1315
* The output array is optional, but if provided, it will be reused without
@@ -16,7 +18,7 @@
1618
module.exports = function mapArray(out, data, func) {
1719
var i, j;
1820

19-
if(!Array.isArray(out)) {
21+
if(!isArrayOrTypedArray(out)) {
2022
// If not an array, make it an array:
2123
out = [];
2224
} else if(out.length > data.length) {
@@ -26,7 +28,7 @@ module.exports = function mapArray(out, data, func) {
2628
}
2729

2830
for(i = 0; i < data.length; i++) {
29-
if(!Array.isArray(out[i])) {
31+
if(!isArrayOrTypedArray(out[i])) {
3032
// If not an array, make it an array:
3133
out[i] = [];
3234
} else if(out[i].length > data.length) {

src/traces/choropleth/defaults.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
2929
}
3030

3131
var z = coerce('z');
32-
if(!Array.isArray(z)) {
32+
if(!Lib.isArrayOrTypedArray(z)) {
3333
traceOut.visible = false;
3434
return;
3535
}

src/traces/histogram/calc.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ module.exports = function calc(gd, trace) {
7272
var pr2c = function(v) { return pa.r2c(v, 0, calendar); };
7373
var rawCounterData;
7474

75-
if(Array.isArray(trace[counterData]) && func !== 'count') {
75+
if(Lib.isArrayOrTypedArray(trace[counterData]) && func !== 'count') {
7676
rawCounterData = trace[counterData];
7777
isAvg = func === 'avg';
7878
binFunc = binFunctions[func];
@@ -199,7 +199,7 @@ module.exports = function calc(gd, trace) {
199199

200200
arraysToCalcdata(cd, trace);
201201

202-
if(Array.isArray(trace.selectedpoints)) {
202+
if(Lib.isArrayOrTypedArray(trace.selectedpoints)) {
203203
Lib.tagSelected(cd, trace, ptNumber2cdIndex);
204204
}
205205

src/traces/mesh3d/defaults.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
2424
var ret = array.map(function(attr) {
2525
var result = coerce(attr);
2626

27-
if(result && Array.isArray(result)) return result;
27+
if(result && Lib.isArrayOrTypedArray(result)) return result;
2828
return null;
2929
});
3030

src/traces/pie/calc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
var isNumeric = require('fast-isnumeric');
1212
var tinycolor = require('tinycolor2');
1313

14+
var Lib = require('../../lib');
1415
var Color = require('../../components/color');
1516
var helpers = require('./helpers');
1617

1718
module.exports = function calc(gd, trace) {
1819
var vals = trace.values;
19-
var hasVals = Array.isArray(vals) && vals.length;
20+
var hasVals = Lib.isArrayOrTypedArray(vals) && vals.length;
2021
var labels = trace.labels;
2122
var colors = trace.marker.colors || [];
2223
var cd = [];

src/traces/pie/defaults.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
2121
var vals = coerce('values');
2222
var labels = coerce('labels');
2323
if(!Array.isArray(labels)) {
24-
if(!Array.isArray(vals) || !vals.length) {
24+
if(!Lib.isArrayOrTypedArray(vals) || !vals.length) {
2525
// must have at least one of vals or labels
2626
traceOut.visible = false;
2727
return;

src/traces/scatter/calc.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var isNumeric = require('fast-isnumeric');
1312

13+
var Lib = require('../../lib');
1414
var Axes = require('../../plots/cartesian/axes');
1515
var BADNUM = require('../../constants/numerical').BADNUM;
1616

@@ -111,7 +111,7 @@ function calcMarkerSize(trace, serieslen) {
111111
};
112112
}
113113

114-
if(Array.isArray(marker.size)) {
114+
if(Lib.isArrayOrTypedArray(marker.size)) {
115115
// I tried auto-type but category and dates dont make much sense.
116116
var ax = {type: 'linear'};
117117
Axes.setConvert(ax);

src/traces/scatter/calc_selection.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
var Lib = require('../../lib');
1212

1313
module.exports = function calcSelection(cd, trace) {
14-
if(Array.isArray(trace.selectedpoints)) {
14+
if(Lib.isArrayOrTypedArray(trace.selectedpoints)) {
1515
Lib.tagSelected(cd, trace);
1616
}
1717
};

src/traces/scatter/fillcolor_defaults.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
var Color = require('../../components/color');
13-
13+
var Lib = require('../../lib');
1414

1515
module.exports = function fillColorDefaults(traceIn, traceOut, defaultColor, coerce) {
1616
var inheritColorFromMarker = false;
@@ -20,10 +20,10 @@ module.exports = function fillColorDefaults(traceIn, traceOut, defaultColor, coe
2020
var markerColor = traceOut.marker.color,
2121
markerLineColor = (traceOut.marker.line || {}).color;
2222

23-
if(markerColor && !Array.isArray(markerColor)) {
23+
if(markerColor && !Lib.isArrayOrTypedArray(markerColor)) {
2424
inheritColorFromMarker = markerColor;
2525
}
26-
else if(markerLineColor && !Array.isArray(markerLineColor)) {
26+
else if(markerLineColor && !Lib.isArrayOrTypedArray(markerLineColor)) {
2727
inheritColorFromMarker = markerLineColor;
2828
}
2929
}

0 commit comments

Comments
 (0)