Skip to content

Nested property with typed arrays #869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"globals": {
"Promise": true,
"Float32Array": true,
"Uint8Array": true
"Uint8Array": true,
"ArrayBuffer": true
},
"rules": {
"strict": [2, "global"],
Expand Down
1 change: 1 addition & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var lib = module.exports = {};

lib.nestedProperty = require('./nested_property');
lib.isPlainObject = require('./is_plain_object');
lib.isArray = require('./is_array');

var coerceModule = require('./coerce');
lib.valObjects = coerceModule.valObjects;
Expand Down
16 changes: 16 additions & 0 deletions src/lib/is_array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

/**
* Return true for arrays, whether they're untyped or not.
*/
module.exports = function isArray(a) {
return Array.isArray(a) || ArrayBuffer.isView(a);
};
13 changes: 7 additions & 6 deletions src/lib/nested_property.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'use strict';

var isNumeric = require('fast-isnumeric');
var isArray = require('./is_array');

/**
* convert a string s (such as 'xaxis.range[0]')
Expand Down Expand Up @@ -93,7 +94,7 @@ function npGet(cont, parts) {
}
return allSame ? out[0] : out;
}
if(typeof curPart === 'number' && !Array.isArray(curCont)) {
if(typeof curPart === 'number' && !isArray(curCont)) {
return undefined;
}
curCont = curCont[curPart];
Expand Down Expand Up @@ -122,7 +123,7 @@ function isDataArray(val, key) {
var containers = ['annotations', 'shapes', 'range', 'domain', 'buttons'],
isNotAContainer = containers.indexOf(key) === -1;

return Array.isArray(val) && isNotAContainer;
return isArray(val) && isNotAContainer;
}

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

if(typeof curPart === 'number' && !Array.isArray(curCont)) {
if(typeof curPart === 'number' && !isArray(curCont)) {
throw 'array index but container is not an array';
}

Expand Down Expand Up @@ -170,7 +171,7 @@ function npSet(cont, parts) {

// handle special -1 array index
function setArrayAll(containerArray, innerParts, val) {
var arrayVal = Array.isArray(val),
var arrayVal = isArray(val),
allSet = true,
thisVal = val,
deleteThis = arrayVal ? false : emptyObj(val),
Expand Down Expand Up @@ -215,7 +216,7 @@ function pruneContainers(containerLevels) {
for(i = containerLevels.length - 1; i >= 0; i--) {
curCont = containerLevels[i];
remainingKeys = false;
if(Array.isArray(curCont)) {
if(isArray(curCont)) {
for(j = curCont.length - 1; j >= 0; j--) {
if(emptyObj(curCont[j])) {
if(remainingKeys) curCont[j] = undefined;
Expand All @@ -239,7 +240,7 @@ function pruneContainers(containerLevels) {
function emptyObj(obj) {
if(obj === undefined || obj === null) return true;
if(typeof obj !== 'object') return false; // any plain value
if(Array.isArray(obj)) return !obj.length; // []
if(isArray(obj)) return !obj.length; // []
return !Object.keys(obj).length; // {}
}

Expand Down
47 changes: 47 additions & 0 deletions test/jasmine/tests/is_array_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var Lib = require('@src/lib');

describe('isArray', function() {
'use strict';

var isArray = Lib.isArray;

function A() {}

var shouldPass = [
[],
new Array(10),
new Float32Array(1),
new Int32Array([1, 2, 3])
];

var shouldFail = [
A,
new A(),
document,
window,
null,
undefined,
'string',
true,
false,
NaN,
Infinity,
/foo/,
'\n',
new Date(),
new RegExp('foo'),
new String('string')
];

shouldPass.forEach(function(obj) {
it('treats ' + JSON.stringify(obj) + ' as an array', function() {
expect(isArray(obj)).toBe(true);
});
});

shouldFail.forEach(function(obj) {
it('treats ' + JSON.stringify(obj !== window ? obj : 'window') + ' as NOT an array', function() {
expect(isArray(obj)).toBe(false);
});
});
});
1 change: 1 addition & 0 deletions test/jasmine/tests/is_plain_object_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('isPlainObject', function() {
null,
undefined,
[],
new Float32Array(1),
'string',
true,
false,
Expand Down