Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2a9548d

Browse filesBrowse files
committed
rearrange lib array functions and add type-safe concat
1 parent ceb119f commit 2a9548d
Copy full SHA for 2a9548d

11 files changed

+205
-104
lines changed

‎package-lock.json

Copy file name to clipboardExpand all lines: package-lock.json
+7-21Lines changed: 7 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/lib/array.js

Copy file name to clipboard
+134Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/**
2+
* Copyright 2012-2018, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var isArray = Array.isArray;
12+
13+
// IE9 fallbacks
14+
15+
var ab = (typeof ArrayBuffer === 'undefined' || !ArrayBuffer.isView) ?
16+
{isView: function() { return false; }} :
17+
ArrayBuffer;
18+
19+
var dv = (typeof DataView === 'undefined') ?
20+
function() {} :
21+
DataView;
22+
23+
function isTypedArray(a) {
24+
return ab.isView(a) && !(a instanceof dv);
25+
}
26+
exports.isTypedArray = isTypedArray;
27+
28+
function isArrayOrTypedArray(a) {
29+
return isArray(a) || isTypedArray(a);
30+
}
31+
exports.isArrayOrTypedArray = isArrayOrTypedArray;
32+
33+
/*
34+
* Test whether an input object is 1D.
35+
*
36+
* Assumes we already know the object is an array.
37+
*
38+
* Looks only at the first element, if the dimensionality is
39+
* not consistent we won't figure that out here.
40+
*/
41+
function isArray1D(a) {
42+
return !isArrayOrTypedArray(a[0]);
43+
}
44+
exports.isArray1D = isArray1D;
45+
46+
/*
47+
* Ensures an array has the right amount of storage space. If it doesn't
48+
* exist, it creates an array. If it does exist, it returns it if too
49+
* short or truncates it in-place.
50+
*
51+
* The goal is to just reuse memory to avoid a bit of excessive garbage
52+
* collection.
53+
*/
54+
exports.ensureArray = function(out, n) {
55+
// TODO: typed array support here? This is only used in
56+
// traces/carpet/compute_control_points
57+
if(!isArray(out)) out = [];
58+
59+
// If too long, truncate. (If too short, it will grow
60+
// automatically so we don't care about that case)
61+
out.length = n;
62+
63+
return out;
64+
};
65+
66+
/*
67+
* TypedArray-compatible concatenation of n arrays
68+
* if all arrays are the same type it will preserve that type,
69+
* otherwise it falls back on Array.
70+
* Also tries to avoid copying, in case one array has zero length
71+
* But never mutates an existing array
72+
*/
73+
exports.concat = function() {
74+
var args = [];
75+
var allArray = true;
76+
var totalLen = 0;
77+
78+
var _constructor, arg0, i, argi, posi, leni, out, j;
79+
80+
for(i = 0; i < arguments.length; i++) {
81+
argi = arguments[i];
82+
leni = argi.length;
83+
if(leni) {
84+
if(arg0) args.push(argi);
85+
else {
86+
arg0 = argi;
87+
posi = leni;
88+
}
89+
90+
if(isArray(argi)) {
91+
_constructor = false;
92+
}
93+
else {
94+
allArray = false;
95+
if(!totalLen) {
96+
_constructor = argi.constructor;
97+
}
98+
else if(_constructor !== argi.constructor) {
99+
// TODO: in principle we could upgrade here,
100+
// ie keep typed array but convert all to Float64Array?
101+
_constructor = false;
102+
}
103+
}
104+
105+
totalLen += leni;
106+
}
107+
}
108+
109+
if(!totalLen) return [];
110+
if(!args.length) return arg0;
111+
112+
if(allArray) return arg0.concat.apply(arg0, args);
113+
if(_constructor) {
114+
// matching typed arrays
115+
out = new _constructor(totalLen);
116+
out.set(arg0);
117+
for(i = 0; i < args.length; i++) {
118+
argi = args[i];
119+
out.set(argi, posi);
120+
posi += argi.length;
121+
}
122+
return out;
123+
}
124+
125+
// mismatched types or Array + typed
126+
out = new Array(totalLen);
127+
for(j = 0; j < arg0.length; j++) out[j] = arg0[j];
128+
for(i = 0; i < args.length; i++) {
129+
argi = args[i];
130+
for(j = 0; j < argi.length; j++) out[posi + j] = argi[j];
131+
posi += j;
132+
}
133+
return out;
134+
};

‎src/lib/coerce.js

Copy file name to clipboardExpand all lines: src/lib/coerce.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var nestedProperty = require('./nested_property');
1919
var counterRegex = require('./regex').counter;
2020
var DESELECTDIM = require('../constants/interactions').DESELECTDIM;
2121
var modHalf = require('./mod').modHalf;
22-
var isArrayOrTypedArray = require('./is_array').isArrayOrTypedArray;
22+
var isArrayOrTypedArray = require('./array').isArrayOrTypedArray;
2323

2424
exports.valObjectMeta = {
2525
data_array: {

‎src/lib/ensure_array.js

Copy file name to clipboardExpand all lines: src/lib/ensure_array.js
-27Lines changed: 0 additions & 27 deletions
This file was deleted.

‎src/lib/gl_format_color.js

Copy file name to clipboardExpand all lines: src/lib/gl_format_color.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var rgba = require('color-normalize');
1515

1616
var Colorscale = require('../components/colorscale');
1717
var colorDflt = require('../components/color/attributes').defaultLine;
18-
var isArrayOrTypedArray = require('./is_array').isArrayOrTypedArray;
18+
var isArrayOrTypedArray = require('./array').isArrayOrTypedArray;
1919

2020
var colorDfltRgba = rgba(colorDflt);
2121
var opacityDflt = 1;

‎src/lib/index.js

Copy file name to clipboardExpand all lines: src/lib/index.js
+7-6Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@ lib.relativeAttr = require('./relative_attr');
2424
lib.isPlainObject = require('./is_plain_object');
2525
lib.toLogRange = require('./to_log_range');
2626
lib.relinkPrivateKeys = require('./relink_private');
27-
lib.ensureArray = require('./ensure_array');
27+
28+
var arrayModule = require('./array');
29+
lib.isTypedArray = arrayModule.isTypedArray;
30+
lib.isArrayOrTypedArray = arrayModule.isArrayOrTypedArray;
31+
lib.isArray1D = arrayModule.isArray1D;
32+
lib.ensureArray = arrayModule.ensureArray;
33+
lib.concat = arrayModule.concat;
2834

2935
var modModule = require('./mod');
3036
lib.mod = modModule.mod;
3137
lib.modHalf = modModule.modHalf;
3238

33-
var isArrayModule = require('./is_array');
34-
lib.isTypedArray = isArrayModule.isTypedArray;
35-
lib.isArrayOrTypedArray = isArrayModule.isArrayOrTypedArray;
36-
lib.isArray1D = isArrayModule.isArray1D;
37-
3839
var coerceModule = require('./coerce');
3940
lib.valObjectMeta = coerceModule.valObjectMeta;
4041
lib.coerce = coerceModule.coerce;

‎src/lib/is_array.js

Copy file name to clipboardExpand all lines: src/lib/is_array.js
-45Lines changed: 0 additions & 45 deletions
This file was deleted.

‎src/lib/nested_property.js

Copy file name to clipboardExpand all lines: src/lib/nested_property.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
var isNumeric = require('fast-isnumeric');
13-
var isArrayOrTypedArray = require('./is_array').isArrayOrTypedArray;
13+
var isArrayOrTypedArray = require('./array').isArrayOrTypedArray;
1414

1515
/**
1616
* convert a string s (such as 'xaxis.range[0]')

‎src/lib/relink_private.js

Copy file name to clipboardExpand all lines: src/lib/relink_private.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
'use strict';
1111

12-
var isArrayOrTypedArray = require('./is_array').isArrayOrTypedArray;
12+
var isArrayOrTypedArray = require('./array').isArrayOrTypedArray;
1313
var isPlainObject = require('./is_plain_object');
1414

1515
/**

‎src/lib/stats.js

Copy file name to clipboardExpand all lines: src/lib/stats.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
var isNumeric = require('fast-isnumeric');
13-
var isArrayOrTypedArray = require('./is_array').isArrayOrTypedArray;
13+
var isArrayOrTypedArray = require('./array').isArrayOrTypedArray;
1414

1515
/**
1616
* aggNums() returns the result of an aggregate function applied to an array of

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.