Skip to content

Commit 365f53a

Browse files
committed
nestedProperty to handle regular and typed arrays alike
(cherry picked from commit 31d66fe)
1 parent 3a343e2 commit 365f53a

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/lib/is_array.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
* Truncate a Float32Array to some length. A wrapper to support environments
13+
* (e.g. node-webkit) that do not implement Float32Array.prototype.slice
14+
*/
15+
module.exports = function isArray(a) {
16+
return Array.isArray(a) || ArrayBuffer.isView(a);
17+
}

src/lib/nested_property.js

Lines changed: 7 additions & 6 deletions
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

0 commit comments

Comments
 (0)