Skip to content

Commit 5fb462f

Browse files
committed
comments and tests for plot_api/helpers.hasParent
1 parent 70c882b commit 5fb462f

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

Diff for: src/plot_api/helpers.js

+17
Original file line numberDiff line numberDiff line change
@@ -485,13 +485,30 @@ exports.manageArrayContainers = function(np, newVal, undoit) {
485485
}
486486
};
487487

488+
/*
489+
* Match the part to strip off to turn an attribute into its parent
490+
* really it should be either '.some_characters' or '[number]'
491+
* but we're a little more permissive here and match either
492+
* '.not_brackets_or_dot' or '[not_brackets_or_dot]'
493+
*/
488494
var ATTR_TAIL_RE = /(\.[^\[\]\.]+|\[[^\[\]\.]+\])$/;
489495

490496
function getParent(attr) {
491497
var tail = attr.search(ATTR_TAIL_RE);
492498
if(tail > 0) return attr.substr(0, tail);
493499
}
494500

501+
/*
502+
* hasParent: does an attribute object contain a parent of the given attribute?
503+
* for example, given 'images[2].x' do we also have 'images' or 'images[2]'?
504+
*
505+
* @param {Object} aobj
506+
* update object, whose keys are attribute strings and values are their new settings
507+
* @param {string} attr
508+
* the attribute string to test against
509+
* @returns {Boolean}
510+
* is a parent of attr present in aobj?
511+
*/
495512
exports.hasParent = function(aobj, attr) {
496513
var attrParent = getParent(attr);
497514
while(attrParent) {

Diff for: test/jasmine/tests/plot_api_test.js

+50
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var Bar = require('@src/traces/bar');
77
var Legend = require('@src/components/legend');
88
var pkg = require('../../../package.json');
99
var subroutines = require('@src/plot_api/subroutines');
10+
var helpers = require('@src/plot_api/helpers');
1011

1112
var d3 = require('d3');
1213
var createGraphDiv = require('../assets/create_graph_div');
@@ -1309,3 +1310,52 @@ describe('Test plot api', function() {
13091310
});
13101311
});
13111312
});
1313+
1314+
describe('plot_api helpers', function() {
1315+
describe('hasParent', function() {
1316+
var attr = 'annotations[2].xref';
1317+
var attr2 = 'marker.line.width';
1318+
1319+
it('does not match the attribute itself or other related non-parent attributes', function() {
1320+
var aobj = {
1321+
// '' wouldn't be valid as an attribute in our framework, but tested
1322+
// just in case this would count as a parent.
1323+
'': true,
1324+
'annotations[1]': {}, // parent structure, just a different array element
1325+
'xref': 1, // another substring
1326+
'annotations[2].x': 0.5, // substring of the attribute, but not a parent
1327+
'annotations[2].xref': 'x2' // the attribute we're testing - not its own parent
1328+
};
1329+
1330+
expect(helpers.hasParent(aobj, attr)).toBe(false);
1331+
1332+
var aobj2 = {
1333+
'marker.line.color': 'red',
1334+
'marker.line.width': 2,
1335+
'marker.color': 'blue',
1336+
'line': {}
1337+
};
1338+
1339+
expect(helpers.hasParent(aobj2, attr2)).toBe(false);
1340+
});
1341+
1342+
it('is false when called on a top-level attribute', function() {
1343+
var aobj = {
1344+
'': true,
1345+
'width': 100
1346+
};
1347+
1348+
expect(helpers.hasParent(aobj, 'width')).toBe(false);
1349+
});
1350+
1351+
it('matches any kind of parent', function() {
1352+
expect(helpers.hasParent({'annotations': []}, attr)).toBe(true);
1353+
expect(helpers.hasParent({'annotations[2]': {}}, attr)).toBe(true);
1354+
1355+
expect(helpers.hasParent({'marker': {}}, attr2)).toBe(true);
1356+
// this one wouldn't actually make sense: marker.line needs to be an object...
1357+
// but hasParent doesn't look at the values in aobj, just its keys.
1358+
expect(helpers.hasParent({'marker.line': 1}, attr2)).toBe(true);
1359+
});
1360+
});
1361+
});

0 commit comments

Comments
 (0)