Skip to content

Commit 5030d3a

Browse files
committed
Addressed minor review comments
- Use `Lib.isPlainObject` - Renamed `data` -> `value` - Added `Uint8ClampedArray` - Committed updated package-lock.json No changes yet to the logical structure of where conversion happens
1 parent 636e644 commit 5030d3a

File tree

5 files changed

+42
-43
lines changed

5 files changed

+42
-43
lines changed

package-lock.json

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/coerce.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ var DESELECTDIM = require('../constants/interactions').DESELECTDIM;
2121
var wrap180 = require('./angles').wrap180;
2222
var isArray = require('./is_array');
2323
var isArrayOrTypedArray = isArray.isArrayOrTypedArray;
24-
var isTypedArray = isArray.isTypedArray;
2524
var isPrimitiveTypedArrayRepr = isArray.isPrimitiveTypedArrayRepr;
2625
var b64 = require('base64-arraybuffer');
2726

@@ -545,6 +544,7 @@ var dtypeStringToTypedarrayType = {
545544
int16: Int16Array,
546545
int32: Int32Array,
547546
uint8: Uint8Array,
547+
uint8_clamped: Uint8ClampedArray,
548548
uint16: Uint16Array,
549549
uint32: Uint32Array,
550550
float32: Float32Array,
@@ -566,20 +566,20 @@ function primitiveTypedArrayReprToTypedArray(v) {
566566

567567
// Process data
568568
var coercedV;
569-
var data = v.data;
570-
if(data instanceof ArrayBuffer) {
571-
// data is an ArrayBuffer
572-
coercedV = new TypeArrayType(data);
573-
} else if(data.constructor === DataView) {
574-
// data has a buffer property, where the buffer is an ArrayBuffer
575-
coercedV = new TypeArrayType(data.buffer);
576-
} else if(Array.isArray(data)) {
577-
// data is a primitive array
578-
coercedV = new TypeArrayType(data);
579-
} else if(typeof data === 'string' ||
580-
data instanceof String) {
581-
// data is a base64 encoded string
582-
var buffer = b64.decode(data);
569+
var value = v.value;
570+
if(value instanceof ArrayBuffer) {
571+
// value is an ArrayBuffer
572+
coercedV = new TypeArrayType(value);
573+
} else if(value.constructor === DataView) {
574+
// value has a buffer property, where the buffer is an ArrayBuffer
575+
coercedV = new TypeArrayType(value.buffer);
576+
} else if(Array.isArray(value)) {
577+
// value is a primitive array
578+
coercedV = new TypeArrayType(value);
579+
} else if(typeof value === 'string' ||
580+
value instanceof String) {
581+
// value is a base64 encoded string
582+
var buffer = b64.decode(value);
583583
coercedV = new TypeArrayType(buffer);
584584
}
585585
return coercedV;

src/lib/is_array.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* This source code is licensed under the MIT license found in the
66
* LICENSE file in the root directory of this source tree.
77
*/
8+
var Lib = require('../lib');
89

910
'use strict';
1011

@@ -39,9 +40,8 @@ function isArray1D(a) {
3940
}
4041

4142
function isPrimitiveTypedArrayRepr(a) {
42-
return (a !== undefined && a !== null &&
43-
typeof a === 'object' &&
44-
a.hasOwnProperty('dtype') && a.hasOwnProperty('data'));
43+
return (Lib.isPlainObject(a) &&
44+
a.hasOwnProperty('dtype') && a.hasOwnProperty('value'));
4545
}
4646

4747
module.exports = {

test/image/mocks/typed_array_repr_scatter.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"data": [{
33
"type": "scatter",
4-
"x": {"dtype": "float64", "data": [3, 2, 1]},
5-
"y": {"dtype": "float32", "data": "AABAQAAAAEAAAIA/"},
4+
"x": {"dtype": "float64", "value": [3, 2, 1]},
5+
"y": {"dtype": "float32", "value": "AABAQAAAAEAAAIA/"},
66
"marker": {
77
"color": {
88
"dtype": "uint16",
9-
"data": "AwACAAEA",
9+
"value": "AwACAAEA",
1010
},
1111
}
1212
}]

test/jasmine/tests/primitive_typed_array_repr_test.js

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
var Lib = require('@src/lib');
22
var supplyDefaults = require('../assets/supply_defaults');
3-
var isTypedArray = require('../../../src/lib/is_array').isTypedArray;
43
var b64 = require('base64-arraybuffer');
54
var mock1 = require('@mocks/typed_array_repr_scatter.json');
65

76
var typedArraySpecs = [
87
['int8', new Int8Array([-128, -34, 1, 127])],
98
['uint8', new Uint8Array([0, 1, 127, 255])],
9+
['uint8_clamped', new Uint8ClampedArray([0, 1, 127, 255])],
1010
['int16', new Int16Array([-32768, -123, 345, 32767])],
1111
['uint16', new Uint16Array([0, 345, 32767, 65535])],
1212
['int32', new Int32Array([-2147483648, -123, 345, 32767, 2147483647])],
@@ -21,13 +21,13 @@ describe('Test TypedArray representations', function() {
2121
describe('ArrayBuffer', function() {
2222
it('should accept representation as ArrayBuffer', function() {
2323
typedArraySpecs.forEach(function(arraySpec) {
24-
// Build data and confirm its type
25-
var data = arraySpec[1].buffer;
26-
expect(data.constructor).toEqual(ArrayBuffer);
24+
// Build value and confirm its type
25+
var value = arraySpec[1].buffer;
26+
expect(value.constructor).toEqual(ArrayBuffer);
2727

2828
var repr = {
2929
dtype: arraySpec[0],
30-
data: data
30+
value: value
3131
};
3232
var gd = {
3333
data: [{
@@ -46,13 +46,13 @@ describe('Test TypedArray representations', function() {
4646
describe('Array', function() {
4747
it('should accept representation as Array', function() {
4848
typedArraySpecs.forEach(function(arraySpec) {
49-
// Build data and confirm its type
50-
var data = Array.prototype.slice.call(arraySpec[1]);
51-
expect(Array.isArray(data)).toEqual(true);
49+
// Build value and confirm its type
50+
var value = Array.prototype.slice.call(arraySpec[1]);
51+
expect(Array.isArray(value)).toEqual(true);
5252

5353
var repr = {
5454
dtype: arraySpec[0],
55-
data: data
55+
value: value
5656
};
5757
var gd = {
5858
data: [{
@@ -71,13 +71,13 @@ describe('Test TypedArray representations', function() {
7171
describe('DataView', function() {
7272
it('should accept representation as DataView', function() {
7373
typedArraySpecs.forEach(function(arraySpec) {
74-
// Build data and confirm its type
75-
var data = new DataView(arraySpec[1].buffer);
76-
expect(data.constructor).toEqual(DataView);
74+
// Build value and confirm its type
75+
var value = new DataView(arraySpec[1].buffer);
76+
expect(value.constructor).toEqual(DataView);
7777

7878
var repr = {
7979
dtype: arraySpec[0],
80-
data: data
80+
value: value
8181
};
8282
var gd = {
8383
data: [{
@@ -96,13 +96,13 @@ describe('Test TypedArray representations', function() {
9696
describe('base64', function() {
9797
it('should accept representation as base 64 string', function() {
9898
typedArraySpecs.forEach(function(arraySpec) {
99-
// Build data and confirm its type
100-
var data = b64.encode(arraySpec[1].buffer);
101-
expect(typeof data).toEqual('string');
99+
// Build value and confirm its type
100+
var value = b64.encode(arraySpec[1].buffer);
101+
expect(typeof value).toEqual('string');
102102

103103
var repr = {
104104
dtype: arraySpec[0],
105-
data: data
105+
value: value
106106
};
107107
var gd = {
108108
data: [{
@@ -127,21 +127,21 @@ describe('Test TypedArray representations', function() {
127127
// data_array property
128128
expect(gd.data[0].x).toEqual({
129129
'dtype': 'float64',
130-
'data': [3, 2, 1]});
130+
'value': [3, 2, 1]});
131131
expect(gd._fullData[0].x).toEqual(new Float64Array([3, 2, 1]));
132132

133133
// Check y
134134
// data_array property
135135
expect(gd.data[0].y).toEqual({
136136
'dtype': 'float32',
137-
'data': 'AABAQAAAAEAAAIA/'});
137+
'value': 'AABAQAAAAEAAAIA/'});
138138
expect(gd._fullData[0].y).toEqual(new Float32Array([3, 2, 1]));
139139

140140
// Check marker.color
141141
// This is an arrayOk property not a data_array property
142142
expect(gd.data[0].marker.color).toEqual({
143143
'dtype': 'uint16',
144-
'data': 'AwACAAEA'});
144+
'value': 'AwACAAEA'});
145145
expect(gd._fullData[0].marker.color).toEqual(new Uint16Array([3, 2, 1]));
146146
});
147147
});

0 commit comments

Comments
 (0)