Skip to content

Commit b8a893a

Browse files
authored
Merge pull request #869 from monfera/nested-property-with-typed-arrays
Nested property with typed arrays
2 parents 3a343e2 + 3a5a5eb commit b8a893a

File tree

6 files changed

+74
-7
lines changed

6 files changed

+74
-7
lines changed

src/.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"globals": {
77
"Promise": true,
88
"Float32Array": true,
9-
"Uint8Array": true
9+
"Uint8Array": true,
10+
"ArrayBuffer": true
1011
},
1112
"rules": {
1213
"strict": [2, "global"],

src/lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var lib = module.exports = {};
1515

1616
lib.nestedProperty = require('./nested_property');
1717
lib.isPlainObject = require('./is_plain_object');
18+
lib.isArray = require('./is_array');
1819

1920
var coerceModule = require('./coerce');
2021
lib.valObjects = coerceModule.valObjects;

src/lib/is_array.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright 2012-2016, 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+
/**
12+
* Return true for arrays, whether they're untyped or not.
13+
*/
14+
module.exports = function isArray(a) {
15+
return Array.isArray(a) || ArrayBuffer.isView(a);
16+
};

src/lib/nested_property.js

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

1212
var isNumeric = require('fast-isnumeric');
13+
var isArray = require('./is_array');
1314

1415
/**
1516
* convert a string s (such as 'xaxis.range[0]')
@@ -93,7 +94,7 @@ function npGet(cont, parts) {
9394
}
9495
return allSame ? out[0] : out;
9596
}
96-
if(typeof curPart === 'number' && !Array.isArray(curCont)) {
97+
if(typeof curPart === 'number' && !isArray(curCont)) {
9798
return undefined;
9899
}
99100
curCont = curCont[curPart];
@@ -122,7 +123,7 @@ function isDataArray(val, key) {
122123
var containers = ['annotations', 'shapes', 'range', 'domain', 'buttons'],
123124
isNotAContainer = containers.indexOf(key) === -1;
124125

125-
return Array.isArray(val) && isNotAContainer;
126+
return isArray(val) && isNotAContainer;
126127
}
127128

128129
function npSet(cont, parts) {
@@ -136,7 +137,7 @@ function npSet(cont, parts) {
136137
for(i = 0; i < parts.length - 1; i++) {
137138
curPart = parts[i];
138139

139-
if(typeof curPart === 'number' && !Array.isArray(curCont)) {
140+
if(typeof curPart === 'number' && !isArray(curCont)) {
140141
throw 'array index but container is not an array';
141142
}
142143

@@ -170,7 +171,7 @@ function npSet(cont, parts) {
170171

171172
// handle special -1 array index
172173
function setArrayAll(containerArray, innerParts, val) {
173-
var arrayVal = Array.isArray(val),
174+
var arrayVal = isArray(val),
174175
allSet = true,
175176
thisVal = val,
176177
deleteThis = arrayVal ? false : emptyObj(val),
@@ -215,7 +216,7 @@ function pruneContainers(containerLevels) {
215216
for(i = containerLevels.length - 1; i >= 0; i--) {
216217
curCont = containerLevels[i];
217218
remainingKeys = false;
218-
if(Array.isArray(curCont)) {
219+
if(isArray(curCont)) {
219220
for(j = curCont.length - 1; j >= 0; j--) {
220221
if(emptyObj(curCont[j])) {
221222
if(remainingKeys) curCont[j] = undefined;
@@ -239,7 +240,7 @@ function pruneContainers(containerLevels) {
239240
function emptyObj(obj) {
240241
if(obj === undefined || obj === null) return true;
241242
if(typeof obj !== 'object') return false; // any plain value
242-
if(Array.isArray(obj)) return !obj.length; // []
243+
if(isArray(obj)) return !obj.length; // []
243244
return !Object.keys(obj).length; // {}
244245
}
245246

test/jasmine/tests/is_array_test.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var Lib = require('@src/lib');
2+
3+
describe('isArray', function() {
4+
'use strict';
5+
6+
var isArray = Lib.isArray;
7+
8+
function A() {}
9+
10+
var shouldPass = [
11+
[],
12+
new Array(10),
13+
new Float32Array(1),
14+
new Int32Array([1, 2, 3])
15+
];
16+
17+
var shouldFail = [
18+
A,
19+
new A(),
20+
document,
21+
window,
22+
null,
23+
undefined,
24+
'string',
25+
true,
26+
false,
27+
NaN,
28+
Infinity,
29+
/foo/,
30+
'\n',
31+
new Date(),
32+
new RegExp('foo'),
33+
new String('string')
34+
];
35+
36+
shouldPass.forEach(function(obj) {
37+
it('treats ' + JSON.stringify(obj) + ' as an array', function() {
38+
expect(isArray(obj)).toBe(true);
39+
});
40+
});
41+
42+
shouldFail.forEach(function(obj) {
43+
it('treats ' + JSON.stringify(obj !== window ? obj : 'window') + ' as NOT an array', function() {
44+
expect(isArray(obj)).toBe(false);
45+
});
46+
});
47+
});

test/jasmine/tests/is_plain_object_test.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('isPlainObject', function() {
2020
null,
2121
undefined,
2222
[],
23+
new Float32Array(1),
2324
'string',
2425
true,
2526
false,

0 commit comments

Comments
 (0)