Skip to content

Commit 4e6a0aa

Browse files
committed
Add custom prop names to keyedContainer
1 parent 7a2316d commit 4e6a0aa

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

src/components/legend/draw.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ function drawTexts(g, gd) {
418418
}
419419
}
420420

421-
var carr = Lib.keyedContainer(fullInput, 'transforms[' + i + '].groupnames');
421+
var carr = Lib.keyedContainer(fullInput, 'transforms[' + i + '].groupnames', 'group', 'name');
422422

423423
if(BLANK_STRING_REGEX.test(origText)) {
424424
carr.remove(legendItem.trace._group);

src/lib/keyed_container.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ var NAME = 1;
1616
var VALUE = 2;
1717
var BOTH = 3;
1818

19-
module.exports = function keyedContainer(baseObj, path) {
19+
module.exports = function keyedContainer(baseObj, path, keyName, valueName) {
20+
21+
keyName = keyName || 'name';
22+
valueName = valueName || 'value';
2023
var i, arr;
2124
var changeTypes = {};
2225

@@ -31,7 +34,7 @@ module.exports = function keyedContainer(baseObj, path) {
3134
// Construct an index:
3235
var indexLookup = {};
3336
for(i = 0; i < arr.length; i++) {
34-
indexLookup[arr[i].name] = i;
37+
indexLookup[arr[i][keyName]] = i;
3538
}
3639

3740
var obj = {
@@ -43,18 +46,21 @@ module.exports = function keyedContainer(baseObj, path) {
4346
changeType = BOTH;
4447
idx = arr.length;
4548
indexLookup[name] = idx;
46-
} else if(value !== arr[idx].value) {
49+
} else if(value !== arr[idx][valueName]) {
4750
changeType = VALUE;
4851
}
49-
arr[idx] = {name: name, value: value};
52+
var newValue = {};
53+
newValue[keyName] = name;
54+
newValue[valueName] = value;
55+
arr[idx] = newValue;
5056

5157
changeTypes[idx] = changeTypes[idx] | changeType;
5258

5359
return obj;
5460
},
5561
get: function(name) {
5662
var idx = indexLookup[name];
57-
return idx === undefined ? undefined : arr[idx].value;
63+
return idx === undefined ? undefined : arr[idx][valueName];
5864
},
5965
rename: function(name, newName) {
6066
var idx = indexLookup[name];
@@ -65,7 +71,7 @@ module.exports = function keyedContainer(baseObj, path) {
6571
indexLookup[newName] = idx;
6672
delete indexLookup[name];
6773

68-
arr[idx].name = newName;
74+
arr[idx][keyName] = newName;
6975

7076
return obj;
7177
},
@@ -76,7 +82,7 @@ module.exports = function keyedContainer(baseObj, path) {
7682
changeTypes[i] = changeTypes[i] | BOTH;
7783
}
7884
for(i = idx; i < arr.length; i++) {
79-
indexLookup[arr[i].name]--;
85+
indexLookup[arr[i][keyName]]--;
8086
}
8187
arr.splice(idx, 1);
8288
delete(indexLookup[name]);
@@ -92,10 +98,10 @@ module.exports = function keyedContainer(baseObj, path) {
9298
astr = path + '[' + idx + ']';
9399
if(arr[idx]) {
94100
if(changeTypes[idx] & NAME) {
95-
update[astr + '.name'] = arr[idx].name;
101+
update[astr + '.' + keyName] = arr[idx][keyName];
96102
}
97103
if(changeTypes[idx] & VALUE) {
98-
update[astr + '.value'] = arr[idx].value;
104+
update[astr + '.' + valueName] = arr[idx][valueName];
99105
}
100106
} else {
101107
update[astr] = null;

src/transforms/groupby.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ function transformOne(trace, state) {
180180
}
181181

182182
if(opts.groupnames) {
183-
groupNameObj = Lib.keyedContainer(opts, 'groupnames');
183+
groupNameObj = Lib.keyedContainer(opts, 'groupnames', 'group', 'name');
184184
}
185185

186186
// An index to map group name --> expanded trace index

test/jasmine/tests/lib_test.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,7 @@ describe('Test lib.js:', function() {
16661666
});
16671667
});
16681668

1669-
describe('constrcting updates', function() {
1669+
describe('constructing updates', function() {
16701670
it('constructs updates for addition and modification', function() {
16711671
carr.set('foo', 'bar');
16721672
carr.set('name1', 'value3');
@@ -1700,6 +1700,33 @@ describe('Test lib.js:', function() {
17001700
});
17011701
});
17021702
});
1703+
1704+
describe('with custom named properties', function() {
1705+
it('performs standard operations', function() {
1706+
var container = {styles: [
1707+
{foo: 'name1', bar: 'value1'},
1708+
{foo: 'name2', bar: 'value2'}
1709+
]};
1710+
1711+
var carr = Lib.keyedContainer(container, 'styles', 'foo', 'bar');
1712+
1713+
carr.set('name3', 'value3');
1714+
carr.remove('name2');
1715+
carr.rename('name1', 'name2');
1716+
1717+
expect(container).toEqual({styles: [
1718+
{foo: 'name2', bar: 'value1'},
1719+
{foo: 'name3', bar: 'value3'}
1720+
]});
1721+
1722+
expect(carr.constructUpdate()).toEqual({
1723+
'styles[0].foo': 'name2',
1724+
'styles[1].foo': 'name3',
1725+
'styles[1].bar': 'value3',
1726+
'styles[2]': null
1727+
});
1728+
});
1729+
});
17031730
});
17041731
});
17051732

0 commit comments

Comments
 (0)